appium多线程
Posted autointerface
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了appium多线程相关的知识,希望对你有一定的参考价值。
1函数级别
import time
import threading
def sing(nums):
for i in range(nums):
print("唱歌")
time.sleep(1)
def dance():
for i in range(5):
print("跳舞")
time.sleep(1)
t1 = threading.Thread(target=sing, args=(3,))
t2 = threading.Thread(target=dance)
t1.start()
t2.start()
2类级别
class Work1(threading.Thread):
def __init__(self, nums):
super().__init__()
self.nums = nums
def run(self):
for i in range(self.nums):
print("haha")
def main():
w = Work1(2)
w.start()
if __name__ == "__main__":
main()
能够将同一个脚本按顺序运行在不同的手机上。也就是一次运行可以同时测试多台手机。
写的 python 代码,比如 driver.swipe(100, 2000, 100, 1000),我们调用的代码会将这个动作转化成 http 请求
发送给 appium,appium 将接收到的指令通过 bootstrap 转化为 "java" 代码,并交给手机的 bootstarp 进行执行对
应的动作

1想要开启多台手机,就需要让模拟器有多台手机才行。我们需要再一次使用 virtual box 导入虚拟机进行
1. 开启多个 appium
window 直接再次双击桌面图标即可。
mac 需要使用命令的形式,进入到 "应用程序" 的文件夹,然后使用 open -n Appium.app
2. 设置 bootstarp 端口
1. 进入到 appium 的 advanced 界面,进行设置。
2. 一个 appium 设置 server port 为 4723, bootstrap port 为 4724
3. 另一个 appium 设置 server port 为 4725, bootstrap port 为 4726
4. 点击 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" 手机运行脚本
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()
以上是关于appium多线程的主要内容,如果未能解决你的问题,请参考以下文章