当前位置:   article > 正文

使用Python启动appium_python启动appium服务

python启动appium服务

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()

  1. """

  1. appium启动/关闭处理类

  1. """

  1. import subprocess

  1. import os,sys

  1. import time

  1. def stop_appium(port):

  1. mac_cmd = f"lsof -i tcp:{port}"

  1. win_cmd = f"netstat -ano | findstr {port}"

  1. # 判断操作系统

  1. os_platform = sys.platform

  1. print('操作系统:',os_platform)

  1. # #windows 系统

  1. if os_platform == "win32":

  1. win_p = subprocess.Popen(win_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

  1. for line in win_p.stdout.readlines():

  1. if line:

  1. line = line.decode('utf8')

  1. if "LISTENING" in line:

  1. win_pid = line.split("LISTENING")[1].strip()

  1. os.system(f"taskkill -f -pid {win_pid}")

  1. else:

  1. # unix系统

  1. p = subprocess.Popen(mac_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

  1. for line in p.stdout.readlines():

  1. line = line.decode('utf8')

  1. if "node" in line:

  1. stdoutline = line.split(" ")

  1. # print(stdoutline)

  1. pid = stdoutline[4]

  1. os.system(f"kill {pid}")

  1. def start_appium(port):

  1. """

  1. 启动appium 服务

  1. :param port: 服务的端口号

  1. :return:

  1. """

  1. stop_appium(port)

  1. cmd = f"appium -p {port}"

  1. logsdir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "logs")

  1. appium_logs = os.path.join(logsdir,"appium-logs")

  1. if not os.path.exists(appium_logs):

  1. os.mkdir(appium_logs)

  1. log_name = str(port) + '-' + time.strftime('%Y_%m_%d') +".log"

  1. appium_logs_dirName = os.path.join(appium_logs,log_name)

  1. subprocess.Popen(cmd, shell=True, stdout=open(appium_logs_dirName, mode='a', encoding="utf8"),

  1. stderr=subprocess.PIPE)

  1. # # 单个方法调试代码

  1. if __name__ == '__main__':

  1. start_appium(4723)

  1. stop_appium(4723)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/187042
推荐阅读
相关标签
  

闽ICP备14008679号