在做手机app 自动化过程中,通常为了兼容测试,需要自动化操作多台设备,本实例通过自动化代码自动操作打开连接在PC上多台手机设备。

启动代码

启动appium 时 指定要运行的设备
-p 端口
-bp bootstrap 端口
-U 指定设备串口 自动化操作多台设备 - 图1

启动appium

  1. appium -p 4702 -bp 4703 -g log2.log -U 127.0.0.1:21523
  1. appium -p 4700 -bp 4701 -g log1.log -U 127.0.0.1:21513
  1. from appium import webdriver
  2. import multiprocessing
  3. def getdriver(devicedId,port,version):
  4. desired_caps = {
  5. "automationName": "UiAutomator2",
  6. "platformName": "Android",
  7. "platformVersion": version,
  8. "deviceName": devicedId,
  9. "appActivity": "org.cnodejs.android.md.ui.activity.LaunchActivity",
  10. "appPackage": "org.cnodejs.android.md",
  11. "fullReset": False, # 不重置应用数据
  12. "noReset": True
  13. }
  14. print(f'http://127.0.0.1:{port}/wd/hub: {devicedId}')
  15. driver = webdriver.Remote(f'http://127.0.0.1:{port}/wd/hub', desired_caps)
  16. driver.implicitly_wait(20)
  17. return driver
  18. def new_topic(driver):
  19. driver.find_element_by_id("org.cnodejs.android.md:id/fab_create_topic").click()
  20. driver.find_element_by_xpath("//android.widget.Spinner[@resource-id='org.cnodejs.android.md:id/spn_tab']").click()
  21. driver.find_element_by_android_uiautomator(f'new UiSelector().className("android.widget.CheckedTextView").text("问答")').click()
  22. driver.find_element_by_id('org.cnodejs.android.md:id/edt_title').send_keys("1111122223344");
  23. driver.find_element_by_id('org.cnodejs.android.md:id/edt_content').send_keys("2223231231312312312");
  24. driver.find_element_by_accessibility_id("发送").click()
  25. def run(deviceid,port,version):
  26. driver = getdriver(deviceid,port,version)
  27. new_topic(driver)
  28. if __name__ == '__main__':
  29. processes=[]
  30. devices=["emulator-5554","emulator-5556"] # 注意: 4700 端口对应的设备为127.0.0.1:21513
  31. ports =['4700',"4702"]
  32. versions = ['9','10']
  33. for x in range(len(devices)):
  34. print(devices[x],ports[x])
  35. t = multiprocessing.Process(target=run,args=(devices[x],ports[x],versions[x]))
  36. processes.append(t)
  37. t.start()
  38. for p in processes:
  39. p.join()

视频讲解

01-多进程的方式启动多台设备.mp4 (37.35MB)