Objective
When doing Android automation, start a appium service that only matches one phone to automate execution. Sometimes you want the same set of code that can be executed on different phones, measuring the app's compatibility on different phones.
This requires multiple Appium services to be started, and the Android device and the Appium must correspond.
Start multiple phones
1. In this case, the night God Simulator and lightning Simulator, for example, start these 2 devices first
2.ADB devices View device name, multiple devices display multiple lines of data
Start multiple Appium Services
1. Start the Appium service, you can use the command line mode, the cmd inside the start, you can refer to the previous Appium command line parameters
For example, the first Appium service, you can specify a port of 4730, and then specify a device name "Thunderbolt simulator", which is the-u parameter (ADB devices can be viewed)
Appium-a 127.0.0.1-p 4730-u emulator-5554--no-reset
2. The second Appium service specifies the device name "Night God simulator"
Appium-a 127.0.0.1-p 4740-u 127.0.0.1:62001--no-reset
YAML Management Configuration
1. When running different mobile phones, the parameter configuration of desired_caps must also need multiple, how to manage multiple desired_caps configurations efficiently?
Here I use Yaml file to manage, more convenient to view
desired_caps = { 'platformName': 'Android', # 手机是Android还是ios 'deviceName': 'emulator-5554', 'platformVersion': '5.1.1', # android版本号 'noReset': True, # apk包名 # aapt工具查看 'appPackage': 'com.taobao.taobao', # apk的launcherActivity 'appActivity': 'com.taobao.tao.welcome.Welcome' }
The 2.yaml file is configured as follows:
- desc: 设备名称_雷电,appium启动服务端口号_4723 port: 4730 desired_caps: platformName: Android deviceName: emulator-5554 appPackage: com.taobao.taobao noReset: !!bool True udid: emulator-5554 appActivity: com.taobao.tao.welcome.Welcome- desc: 设备名称_夜神,appium启动服务端口号_4724 port: 4740 desired_caps: platformName: Android deviceName: 127.0.0.1:62001 appPackage: com.taobao.taobao noReset: !!bool True udid: 127.0.0.1:62001 appActivity: com.taobao.tao.welcome.Welcome
Read YAML configuration
1. read out multiple configurations are list type, desc is the description of the device, can be described to find the corresponding device name, such as: Lightning
2. Return the Desired_caps configuration information and port port number, which will be used by the following code
# coding=utf-8from appium import webdriverimport timeimport yamlimport osdef get_desired_caps(devicesName='雷电'): ''' 从yaml读取desired_caps配置信息 参数name:设备名称,如:夜神/雷电 :return: desired_caps字典格式 和port ''' curpath = os.path.dirname(os.path.realpath(__file__)) yamlpath = os.path.join(curpath, "taobao.yaml") print("配置地址:%s" % yamlpath) f = open(yamlpath, "r", encoding="utf-8") a = f.read() f.close() # 把yaml文件转字典 d = yaml.load(a) for i in d: if devicesName in i["desc"]: print(i) # 启动服务 start_appium(port=i['port']) return (i['desired_caps'], i['port'])
Run the app code
def run_app(devicesName): # 配置参数 desired_caps = get_desired_caps(devicesName) print(desired_caps) # 执行代码 driver = webdriver.Remote('http://127.0.0.1:%s/wd/hub' % desired_caps[1], desired_caps[0]) time.sleep(10) # 点注册登陆 driver.find_element_by_xpath("//*[@text='注册/登录']").click() time.sleep(6) # content-desc driver.find_element_by_xpath("//*[@text='请输入手机号码']").send_keys("15001234000") driver.find_element_by_xpath("//*[@text='请输入验证码']").send_keys("1111")
Python launches Appium service
1. If the manual Port cmd window to start the service trouble, you can use Python to start the Appium service, release your hands
Start by judging if the service is started or not, then execute the cmd command.
# coding=utf-8from appium import webdriverimport timeimport yamlimport osfrom tomorrow import threads# 上海-悠悠 QQ交流群:330467341def start_appium(port=4723, udid=""): a = os.popen('netstat -ano | findstr "%s" '% port) time.sleep(2) t1 = a.read() if "LISTENING" in t1: print("appium服务已经启动:%s" % t1) # s = t1.split(" ") # s1 = [i for i in s if i != ''] # pip = s1[-1].replace("\n", "") else: # 启动appium服务 # appium -a 127.0.0.1 -p 4740 -U emulator-5554 127.0.0.1:62001 --no-reset os.system("appium -a 127.0.0.1 -p %s -U %s --no-reset" % (port, udid))
Multithreaded operation
1. Multithreading with a very simple tomorrow framework on the line
# coding=utf-8from Appium Import webdriverimport timeimport yamlimport osfrom tomorrow import threads# Shanghai-yo QQ Exchange Group: 3304673 41def Start_appium (port=4723, udid= ""): A = Os.popen (' Netstat-ano | findstr '%s "'% port) time.sleep (2) t1 = A.R EAD () If "LISTENING" in T1:print ("Appium service has started:%s"% t1) # s = T1.split ("") # S1 = [I for I in S If I! = '] # pip = s1[-1].replace ("\ n", "") Else: # Start Appium Service # appium-a 127.0.0.1-p 4740-u emulator-5554 127.0.0.1:62001--no-reset os.system ("Appium-a 127.0.0.1-p%s-u%s--no-reset"% (port, Udid)) def Get_desired_caps (devicesname= ' thunderbolt '): ' read Desired_caps configuration information parameter from YAML name: device name, such as: Night God/Thunder: Return:desired_caps Dictionary format "' Curpath = Os.path.dirname (Os.path.realpath (__file__)) Yamlpath = Os.path.join (Curpath," Taobao.yaml ") prin T ("Config address:%s"% yamlpath) F = open (Yamlpath, "R", encoding= "Utf-8") A = F.read () f.close () # Turn Yaml file to dictionary d = y Aml.load (a) for I inD:if devicesname in i["desc"]: print (i) # start service devicesname = i[' desired_caps ' [' u Did '] Print (devicesname) start_appium (port=i[' Port '), Udid=devicesname) return (i[' desire D_caps '], i[' Port ') @threads (2) def Run_app (devicesname): # configuration parameters desired_caps = Get_desired_caps (devicesname) prin T (desired_caps) # execute code driver = webdriver. Remote (' Http://127.0.0.1:%s/wd/hub '% desired_caps[1], desired_caps[0]) Time.sleep (10) # Sign up for login Driver.find_elemen T_by_xpath ("//*[@text = ' Register/login ']"). Click () time.sleep (6) # Content-desc Driver.find_element_by_xpath ("//*[@text = ' please lose Send_keys ("15001234000") Driver.find_element_by_xpath ("//*[@text = ' Please enter the verification Code ']"). Send_keys ("1111") # DRIVER.FIND_ELEMENT_BY_ACCESSIBILITY_ID ("Help"). Click () if __name__ = = "__main__": # Shanghai-long QQ Exchange Group: 330467341 devices = [" Night God "," thunder "] for I in Devices:run_app (devicesname=i)
Appium+python automated 60-windows to launch multiple Appium services simultaneously, allowing multiple Android machines to run in parallel