1.startAppiumserver.py
# -*- coding: utf-8 -*-
import random
import subprocess
from utils.LogUtil import my_log
class Start:
def __init__(self, device=None):
self.device = device
self.log = my_log(__file__)
def __start_driver(self, aport, bpport,udid):
"""
清理logcat与appium所有进程
:return:
"""
# appium = subprocess.Popen("appium -p %s -bp %s -U %s" %
# (aport, bpport, self.device)) # 启动appium
self.log.info("appium -p %s -bp %s -U %s --session-override" %
(aport, bpport,udid))
appium = subprocess.Popen("appium -p %s -bp %s -U %s --session-override" %
(aport, bpport,udid),shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,bufsize=1,close_fds=True) # 启动appium
while True:
appium_line = str(appium.stdout.readline().strip(),"utf-8")
# U.Logging.debug(appium_line)
# U.sleep(1)
print(appium_line)
if 'Error: listen' in appium_line :
message = '--------启动port端口为%s,bport端口为%s,设备udid为%s的appium服务失败!!!---------\n' \
'error_message:%s' % (aport, bpport, udid,appium_line.stderr)
self.log.info(message)
break
elif 'listener started' in appium_line:
message = '--------启动port端口为%s,bport端口为%s,设备udid为的appium服务成功!!!---------\n' \
% (aport, bpport,udid)
self.log.info(message)
break
def start_appium(self,udid):
"""
启动appium
p:appium port
bp:bootstrap port
:return: 返回appium端口参数
"""
aport = random.randint(4700, 4900)
bpport = random.randint(4700, 4900)
self.__start_driver(aport, bpport,udid)
return aport
def main(self,udid):
"""
:return: 启动appium
"""
return self.start_appium(udid)
def stop_appium(self):
res = subprocess.Popen("killall node" , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
bufsize=1, close_fds=True)
if __name__ == '__main__':
s = Start()
s.main(udid)
- run.py
- 通过自动获取设备方法,获取设备列表/自定义设备列表
- 按获取设备,顺序执行脚本
import time
from common.StartAppium import Start
from common.desire_caps import appium_desired
from utils.AdbUtil import get_devices
def do(port, udid):
driver = appium_desired(port, udid)
driver.find_element_by_xpath("//*[@text='更多']").click()
time.sleep(1)
driver.find_element_by_xpath("//*[@text='移动网络']").click()
if __name__ == '__main__':
s = Start()
devices_list = get_devices()
for uuid in devices_list:
port = s.main(uuid)
appium_desired(udid=uuid, appserver_port=port)
print(devices_list)
```