赞
踩
import os
import subprocess
import multiprocessing
import time
import pytest
from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep
# 关于appium的启动
# 1、桌面版(咱们现在用的):
# 运行方式一:点击软件图标
# 运行方式二:
# cmd命令行运行appium:
# appium的路径:C:/Users/jeff.xie/AppData/Roaming/npm/node_modules/appium/build/lib
# ①CD切换上的路径
# ②dir(列出文件)
# ③找到main.js后执行node main.js
# 参数 -p:指定端口
# 参数 -g:指定保存appium日志文件的路径
# node main.js -g D:\appium_log\log.log
# 2、服务版(命令行启动)
# cmd中输入appium命令即可启动
# 参数 -p:指定端口
# 参数 -g:指定保存appium日志文件的路径
# 3、通过python代码启动appium
# ①main.js路径下执行命令:node main.js
# ②通过端口查找进行id:netstat -ano|findstr端口
# ③关闭进程:taskkill /F /PID进程id
# 切换到appium的main.js所在路径
main_js_path= r"C:/Users/jeff.xie/AppData/Roaming/npm/node_modules/appium/build/lib";
os.chdir(main_js_path)
# 执行cmd命令
# os.system('node main.js')
os.system(r'node main.js -p 7890 -g D:\appium_log\log')
# 注意点:os.system会堵塞代码继续往下执行
# 执行测试代码(直接这样运行测试时不行的)
# pytest.main()
print("aaaa")
def start_appium():
"""启动appium"""
# 使用另外的模块
# appium_server_path = r'C:\Users\Admin\AppData\Local\Programs\Appium' \
# r'\resources\app\node_modules\appium\build\lib\main.js'
os.chdir(main_js_path)
port = 4723
appium_log_path = r'D:\appium_log\log{}.log'.format(port)
subprocess.Popen('node main.js -p {} -g {}'.format(port, appium_log_path),
stdout=subprocess.STDOUT,
stderr=subprocess.PIPE,
shell=True).communicate()
sleep(10)
print("KKKKKKK")
desired_caps = {}
# 系统
desired_caps['platformName'] = 'Android'
#手机版本,在手机中:设置--关于手机 #命令行获取手机的版本号:adb -s da79fc70 shell getprop ro.build.version.release
desired_caps['platformVersion'] = '10'
# 设备号 adb devices
desired_caps['deviceName'] = 'emulator-5554'
# 包名 命令行获取包名和启动名:adb shell dumpsys window windows | findstr mFocusedApp
desired_caps['appPackage'] = 'com.android.settings'
# 启动名
desired_caps['appActivity'] = 'com.android.settings.Settings'
desired_caps["resetKeyboard"] = "True"#程序结束时重置原来的输入法
desired_caps["noReset"] = "True"#不初始化手机app信息(类似不清除缓存)
# 声明手机驱动对象
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
WebDriverWait(driver,60)
sleep(2)
print("Open app")
sleep(10)
driver.update_settings({"getMatchedImageResult": True})
el = driver.find_element_by_image('D:/Battery.png')
el.click();
if __name__ == '__main__':
# 创建一个进程去启动appium
p = multiprocessing.Process(target=start_appium)
p.start()
time.sleep(10)
# 运行测试用例
pytest.main()
"""
appium启动/关闭处理类
"""
import subprocess
import os,sys
import time
def stop_appium(port):
mac_cmd = f"lsof -i tcp:{port}"
win_cmd = f"netstat -ano | findstr {port}"
# 判断操作系统
os_platform = sys.platform
print('操作系统:',os_platform)
# #windows 系统
if os_platform == "win32":
win_p = subprocess.Popen(win_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
for line in win_p.stdout.readlines():
if line:
line = line.decode('utf8')
if "LISTENING" in line:
win_pid = line.split("LISTENING")[1].strip()
os.system(f"taskkill -f -pid {win_pid}")
else:
# unix系统
p = subprocess.Popen(mac_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
for line in p.stdout.readlines():
line = line.decode('utf8')
if "node" in line:
stdoutline = line.split(" ")
# print(stdoutline)
pid = stdoutline[4]
os.system(f"kill {pid}")
def start_appium(port):
"""
启动appium 服务
:param port: 服务的端口号
:return:
"""
stop_appium(port)
cmd = f"appium -p {port}"
appium_logs = os.path.join(logsdir,"appium-logs")
if not os.path.exists(appium_logs):
os.mkdir(appium_logs)
log_name = str(port) + '-' + time.strftime('%Y_%m_%d') +".log"
appium_logs_dirName = os.path.join(appium_logs,log_name)
subprocess.Popen(cmd, shell=True, stdout=open(appium_logs_dirName, mode='a', encoding="utf8"),
stderr=subprocess.PIPE)
if __name__ == '__main__':
start_appium(4723)
stop_appium(4723)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。