Start the Appium service and listen for a port command:
Command command==> appium-a {IP}-p {port}-u {devicename}-G {log}
Execute the command as a shell command, and enter the log file to: Import with subprocess
subprocess. Popen (Command, Stdout=open (Log_path, ' A + '), stderr=subprocess. PIPE, shell=true) #相当于再cmd窗口输入上面的命令command
Query whether the service has started based on the port number:
Subprocess.getoutput (' Netstat-ano | findstr%s '%self.port) #如果找到以启动端口包含port, the corresponding information is returned,
Netstat-ano | findstr 1111 Query The process information of port number 1111, from which PID can be obtained
To start the emulator or phone, access the port on which the service listens:
Webdriver. Remote (' Http://127.0.0.1:4723/wd/hub ', dic) #dic是模拟器或手机的设备信息和app信息
According to the PID query process information, the first column is the process name
tasklist | Findstr 2472
Kill all processes that are renamed to correspond to the process name:
taskkill/f/im node.exe/t #node. exe is the process name
Kill process based on PID
Taskkill/pid 2472-t-F #2472是pid
Example:
The Yaml file format is as follows: The content is Mobile information and app information
device_type:androidtester: resetkeyboard:true appactivity: Com.tencent.mobileqq.activity.SplashActivity appPackage:com.tencent.mobileqq # AppPackage:com.android.browser# appactivity:. Browseractivity noreset:false unicodekeyboard:truedevices: android: -Port: 9009 platformname:android udid:emulator-5554 5.1.1 name: Thunder A 127.0.0.2 devicename:emulator-5554
Start the service, test whether the service is started, start the app
From Lib.tool Import Tool
From conf.setting import Log_path
From Lib.log Import Logger
From Appium import Webdriver
Import subprocess
Class Controller (object):
def __init__ (self): #初始化函数主要是对设备和app信息的获取
Self.tool=tool () #tool类中写的有获取设备文件yaml数据的方法
self.yml=self.tool.app_data# obtaining device information in a YAML file
Self.device_type=self.yml.get (' Device_type ')
Self.device_name=self.yml.get (' devices '). Get (Self.device_type) [0].get (' devicename ')
Self.devices=self.yml.get (' devices ')
Self.port=self.devices.get (Self.device_type) [0].get (' Port ')
Self.tester=self.yml.get (' tester ')
# Print (self.device_name)
#taskkill-pid 4152-f
# kill all the processes before you start taskkill/f/im node.exe/t
def kill_server (self):
#taskkill/F/im node.exe/t We open the Appium process name is Node.exe
Logger.debug (' Execute [KILL SERVER] operation:%s '% subprocess.getoutput ("taskkill/f/im node.exe/t")
Logger.debug (' Restart ADB service! %s '% subprocess.getoutput ("adb kill-server"))
def start_server (self): #启动server
Device=self.devices.get (Self.device_type) [0]
#命令字符串
Command= "appium-a {IP}-p {port}-u {devicename}-G {log}". Format (
Ip=device.get (' IP '),
Port=device.get (' Port '),
Devicename=device.get (' devicename '),
Log=log_path.replace (' App_log.log ', Device.get (' name ') + '. LOG ')
)
Print (command)
Logger.debug (' service start command:%s '%command)
#执行命令
Subprocess. Popen (Command, Stdout=open (Log_path, ' A + '), stderr=subprocess. PIPE, Shell=true)
def test_server (self):
While True:
#查询是否端口已启动, which is service startup
C=subprocess.getoutput (' Netstat-ano | findstr%s '%self.port)
If C:
Logger.debug (' Start success .... ‘)
Break
Else
Logger.debug (' Failed to start, retry after 5s ')
Import time
Time.sleep (5)
Return True
def start_driver (self):
Device=self.devices.get (Self.device_type) [0]
Self.tester.update (device)
# Print (Self.tester)
Driver=webdriver. Remote (' Http://{ip}:{port}/wd/hub '. Format (
Ip=device.get (' IP '),
Port=device.get (' Port ')
), Self.tester)
If __name__== ' __main__ ':
Controller=controller ()
Print (Controller.device_name)
Controller.kill_server ()
Controller.start_server ()
If Controller.test_server ():
Controller.start_driver ()
Start and stop Appium services and apps in a command way