在做手机app 自动化过程中,通常为了兼容测试,需要自动化操作多台设备,本实例通过自动化代码自动操作打开连接在PC上多台手机设备。
启动代码
启动appium 时 指定要运行的设备
-p 端口
-bp bootstrap 端口
-U 指定设备串口
启动appium
appium -p 4702 -bp 4703 -g log2.log -U 127.0.0.1:21523
appium -p 4700 -bp 4701 -g log1.log -U 127.0.0.1:21513
from appium import webdriver
import multiprocessing
def getdriver(devicedId,port,version):
desired_caps = {
"automationName": "UiAutomator2",
"platformName": "Android",
"platformVersion": version,
"deviceName": devicedId,
"appActivity": "org.cnodejs.android.md.ui.activity.LaunchActivity",
"appPackage": "org.cnodejs.android.md",
"fullReset": False, # 不重置应用数据
"noReset": True
}
print(f'http://127.0.0.1:{port}/wd/hub: {devicedId}')
driver = webdriver.Remote(f'http://127.0.0.1:{port}/wd/hub', desired_caps)
driver.implicitly_wait(20)
return driver
def new_topic(driver):
driver.find_element_by_id("org.cnodejs.android.md:id/fab_create_topic").click()
driver.find_element_by_xpath("//android.widget.Spinner[@resource-id='org.cnodejs.android.md:id/spn_tab']").click()
driver.find_element_by_android_uiautomator(f'new UiSelector().className("android.widget.CheckedTextView").text("问答")').click()
driver.find_element_by_id('org.cnodejs.android.md:id/edt_title').send_keys("1111122223344");
driver.find_element_by_id('org.cnodejs.android.md:id/edt_content').send_keys("2223231231312312312");
driver.find_element_by_accessibility_id("发送").click()
def run(deviceid,port,version):
driver = getdriver(deviceid,port,version)
new_topic(driver)
if __name__ == '__main__':
processes=[]
devices=["emulator-5554","emulator-5556"] # 注意: 4700 端口对应的设备为127.0.0.1:21513
ports =['4700',"4702"]
versions = ['9','10']
for x in range(len(devices)):
print(devices[x],ports[x])
t = multiprocessing.Process(target=run,args=(devices[x],ports[x],versions[x]))
processes.append(t)
t.start()
for p in processes:
p.join()