一、Appium原理 及 开启对应的工具
原理
我们写的 python 代码,比如 driver.swipe(100, 2000, 100, 1000),我们调用的代码会将这个动作转化成 http 请求发送给 appium,appium 将接收到的指令通过 bootstrap 转化为"java" 代码,并交给手机的 bootstarp 进行执行对应的动作。也就是说,如果我们想要达到一个脚本跑两台手机的效果,需要开启两个 appium,并将 appium 的 bootstarp 端口
设置成两个,手机也需要两个。
步骤
- 开启多个 appium
window 直接再次双击桌面图标即可。
mac 需要使用命令的形式,进入到 “应用程序” 的文件夹,然后使用 open -n Appium.app - 设置 bootstarp 端口
- 进入到 appium 的 advanced 界面,进行设置。
- 一个 appium 设置 server port 为 4723, bootstrap port 为 4724
- 另一个 appium 设置 server port 为 4725, bootstrap port 为 4726
- 点击 start sever
- 将 “前置代码” 中的 udid 参数设置为需要开启的手机
如果 udid 为 “192.168.57.101:5555” 并且 获取 driver 时的端口号为 4723,那么,4723 的 appium 的日志会进行输出,并且使用 “192.168.57.101:5555” 手机运行脚本同理,如果 udid 为 “192.168.57.102:5555” 并且 获取 driver 时的端口号为 4725,那么,4725 的
appium 的日志会进行输出,并且使用 “192.168.57.102:5555” 手机运行脚本
二、多台手机按顺序运行脚本
1.编写一个 init_driver 的函数,将 appium 端口号 和 udid 作为参数,并返回 driver 对象
2.编写一个 do 函数,执行想要做的脚本
3.将 port 和 udid 包装成一个字典,并将多台手机 port 和 udid 对应的字典放到一个列表中
4. 使用 for 循环的形式,调用 do 函数
from appium import webdriver
import time
def init_driver(port, udid):
# server 启动参数
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = 'genymotion'
desired_caps['udid'] = udid
# app信息
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
return webdriver.Remote('http://localhost:' + port + '/wd/hub', desired_caps)
def do(port, udid):
driver = init_driver(port, udid)
driver.find_element_by_xpath("//*[@text='更多']").click()
time.sleep(1)
driver.find_element_by_xpath("//*[@text='移动网络']").click()
devices = [{"port": "4723", "udid": "192.168.57.101:5555"}, {"port": "4725", "udid":
"192.168.57.102:5555"}]
for device in devices:
do(device["port"], device["udid"])
import threading
from appium import webdriver
import time
def init_driver(port, udid):
# server 启动参数
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = 'genymotion'
desired_caps['udid'] = udid
# app信息
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
三、多台手机同时运行脚本
- 编写一个 init_driver 的函数,将 appium 端口号 和 udid 作为参数,并返回 driver 对象
- 编写一个 do 函数,执行想要做的脚本
- 将 port 和 udid 包装成一个字典,并将多台手机 port 和 udid 对应的字典放到一个列表中
- 使用 for 循环的形式,创建线程并调用 do 函数
import threading
from appium import webdriver
import time
def init_driver(port, udid):
# server 启动参数
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = 'genymotion'
desired_caps['udid'] = udid
# app信息
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
return webdriver.Remote('http://localhost:' + port + '/wd/hub', desired_caps)
def do(port, udid):
driver = init_driver(port, udid)
driver.find_element_by_xpath("//*[@text='更多']").click()
time.sleep(1)
driver.find_element_by_xpath("//*[@text='移动网络']").click()
devices = [{"port": "4723", "udid": "192.168.57.101:5555"}, {"port": "4725", "udid":
"192.168.57.102:5555"}]
for device in devices:
threading.Thread(target=do, args=(device["port"], device["udid"])).start()