# coding=utf-8
from appium import webdriver
import time
import yaml
import os
from tomorrow import threads
def start_appium(port, bootstrap, 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("", "")
else:
# 启动appium服务
# appium -a 127.0.0.1 -p 4740 -U emulator-5554 127.0.0.1:62001 --no-reset
# os.system("start /b appium -a 127.0.0.1 -p %s -U %s --no-reset" % (port, udid))
# appium -a 127.0.0.1 -p 4724 -bp 4725 -U 127.0.0.1:62001
os.system("start /b appium -a 127.0.0.1 -p %s -bp %s -U %s" % (port, bootstrap, udid))
def stop_appium(): # 关闭所有的appium进程
os.system("start /b taskkill /F /t /IM node.exe")
def get_desired_caps(devices_name):
"""
从yaml读取desired_caps配置信息
参数name:设备名称,如:夜神/真机1
:return: desired_caps字典格式
"""
curpath = os.path.dirname(os.path.realpath(__file__))
print("路径为:"+curpath)
yamlpath = os.path.join(curpath, "yaml_appium.yaml")
print("配置地址:%s" % yamlpath)
f = open(yamlpath, "r", encoding="utf-8")
a = f.read()
f.close()
# 把yaml文件转字典
d = yaml.load(a)
print(d)
for ii in d:
print(ii)
if devices_name in ii["desc"]: # 判断输入的设备名称是否存在
# 启动服务
devicesname = ii["desired_caps"]["udid"]
print("devicesName: %s" % devicesname)
print("port: %s" %ii["port"])
print("bootstrap: %s" % ii["bootstrap-port"])
print("udid: %s" % ii["desired_caps"]["udid"])
start_appium(port=ii["port"], bootstrap=ii["bootstrap-port"], udid=ii["desired_caps"]["udid"])
# start_appium(port=ii["port"], udid=devicesName)
return ii["desired_caps"], ii["port"]
@threads(2)
def run_app(devices_name):
# 配置参数
desired_caps = get_desired_caps(devices_name)
#app测试用例操作
# 执行代码
driver = webdriver.Remote("http://127.0.0.1:%s/wd/hub" % desired_caps[1], desired_caps[0])
if __name__ == "__main__":
devices = ["夜神", "真机1"]
for i in devices:
run_app(devices_name=i)
stop_appium()
yaml 文件内容如下:
- desc: 设备名称_真机1,appium启动服务端口号_4723
port: 4723
desired_caps:
platformName: Android
deviceName: 192.168.93.210:5555
appPackage: com.jifen.qukan.debug
app: C:UsersadminPycharmProjectslocust_testappsqutoutiao_debug.apk
appActivity: com.jifen.qkbase.main.MainActivity
clearSystemFiles: true
noSign: True
noReset: !!bool False
platformVersion: 4.4.2
udid: 192.168.93.210:5555
newCommandTimeout: 120
bootstrap-port: 4726
- desc: 设备名称_夜神,appium启动服务端口号_4724
port: 4724
desired_caps:
platformName: Android
noSign: True
deviceName: 127.0.0.1:62001
appPackage: com.jifen.qukan.debug
app: C:UsersadminPycharmProjectslocust_testappsqutoutiao_debug.apk
appActivity: com.jifen.qkbase.main.MainActivity
clearSystemFiles: true
noReset: !!bool False
platformVersion: 4.4.2
udid: 127.0.0.1:62001
newCommandTimeout: 120
bootstrap-port: 4725
================================================
第一台设备:adb tcpip 端口(5555) adb connect 手机ip:端口(5555)
第二台设备:adb devices 查看手机的udid ,然后adb -s udid tcpip 端口(5556) adb connect 手机ip:端口(5556)