python-windows命令行启动appium及杀掉对应接口进程

windows命令行启动appium及杀掉对应接口进程

本人环境前置条件:手机设备为真机一加7 Pro,使用的应用为一加计算器

一.环境配置

1.安装命令行版appium

npm可以使用淘宝镜像下载更快,如下命令为全局安装

npm --registry http://registry.npm.taobao.org install appium -g

2.安装appium-doctor检测

npm --registry http://registry.npm.taobao.org install appium-doctor -g

验证:cmd输入appium-doctor

3.python安装Appium-Python-Client:

pip install Appium-Python-Client

验证:cmd输入 appium -v

4.定位uiautomatorviewer.bat

使用定位工具uiautomatorviewer.bat,如果遇到报错请看我这篇博客

编写一个uiautomatorview截屏获取.uix工具_梦无矶的博客-CSDN博客

也可以使用appium的定位,还有weditor。

关于python版uiautomator2中的weditor
python语言实现的一个app自动化测试框架

安装uiautomator2:

pip install -U uiautomator2

初始化命令(往手机上推送apk包):

python -m uiautomator2 init

安装定位工具weditor:

pip install -U weditor

安装完成之后,命令行运行:weditor --help确认是否安装成功。

命令行输入:weditor 会自动打开一个浏览器 进行连接手机设备定位。

init出错的话可以看我这篇博客:

关于uiautomator2-ATX安装失败教程

5.查看主包名主类名Activity

可以查看我这篇博客:

查看主包名主类名Activity

二.python-appium启动app

1.appium启动一加计算器相关参数:

d = {}
d['platformName'] = 'Android'
d['deviceName'] = udid #填写你的设备id,adb devices显示的那个
d['appPackage'] = 'com.oneplus.calculator'
d['appActivity'] = 'com.oneplus.calculator.Calculator'
 subprocess.Popen('appium -p 4723 -bp 5723',shell=True)

端口可以根据自己的需求进行更改,-bp自己领悟是啥,这里不教。

import subprocess
from appium import webdriver
subprocess.Popen('appium -p 4723 -bp 5723',shell=True)  
d = {}
d['platformName'] = 'Android'
d['deviceName'] = udid #填写你的设备id,adb devices显示的那个
d['appPackage'] = 'com.oneplus.calculator'
d['appActivity'] = 'com.oneplus.calculator.Calculator'
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',d)

2.windows查看端口

例子

netstat -ano | findstr 4723  #查询端口号1111的进程信息,从中可以获得pid

启动模拟器或手机,访问服务监听的那个端口:

webdriver.Remote('http://127.0.0.1:4723/wd/hub',dic)  #dic是模拟器或手机的设备信息和app信息
#根据pid查询进程信息,第一列就是进程名称
tasklist | findstr 2472
#根据pid杀死进程
taskkill /pid 2472 -t -f  #2472是pid

我们启动一下一加计算器

请添加图片描述

看到200,则是启动成功,这时候我们去查看端口

请添加图片描述

三.windows杀掉对应进程

杀死上述的4723端口

其实很简单,我们获取控制台输出的第一行,也就是带有LISTENING的这一行显示的PID号,对他进行处理拿到PID号,再进行taskkill即可

def killAppiumPid():
    #windows写法如下
    appium_port = 4723
    print(appium_port)
    cmd_find = 'netstat -aon | findstr %s' % appium_port
    print(cmd_find)

    result = os.popen(cmd_find)
    text = result.read()
    print("result:", text)
    if text != "":
        pid = text.split("LISTENING")[1].strip()[0:5]
        print(f"--------------{pid}------------------")

        # 执行被占用端口的pid
        cmd_kill = 'taskkill -f -pid %s' % pid
        print(cmd_kill)
        subprocess.call('taskkill /T /F /PID %s' % pid, shell=True)
        print("apppium-server 进程已杀掉")
    else:
        print("appiun-server 端口不存在")

运行killAppiumPid函数

请添加图片描述

请添加图片描述

如上图所示LISTENING已经被杀掉了。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值