移动端自动化,没有用过appium,接触用的uiautomator2
最开始用的java + uiautomator一代。
移动自动化问题比较难解决:
1、webview无法识别控件怎么办 (比如百度小程序,用了动态组件,直接渲染的h5页面)
2、相册无法识别怎么办,比如多手机时候,每个相册都判断一下机型,写对应的坐标点击吗。。。不可靠
做的比较普通的安卓自动化,判断网页打电话功能是否正常。之前用的夜神模拟器,跑的不好,经常崩溃。
后面用了雷电模拟器,感觉还行,不过经常 offline.
1.启动雷电模拟
2.执行自动化用例 python + uiautomator2 +雷电模拟器连接
3.关闭dnp
一些是启动的命令,看起来很简单,就做个笔记。
# -*- coding: utf-8 -*-
# @Time : 2020/5/29 19:27
# @Author : gh
# @Software: PyCharm
import subprocess
from subprocess import Popen, PIPE
import re
import psutil
import time
import os
import shutil
class CheckDnplayer():
def check_dnp_start(self):
is_found = False
dir_path = r"C:\Users\admin\.LdVirtualBox"
pl = psutil.pids()
for pid in pl:
try:
# 更新,文件夹会被线程占用
if psutil.Process(pid).name() == "LdBoxSVC.exe":
ps = subprocess.Popen("taskkill /im LdBoxSVC.exe /f", shell=True, stdout=PIPE, stderr=PIPE)
print("杀死文件夹占用 LdBoxSVC.exe")
ps.stdout.close()
except psutil.NoSuchProcess:
pass
if os.path.exists(dir_path):
shutil.rmtree(dir_path)
print("删除 %s" % dir_path)
try:
# 5037 是adb 的端口,可能被其他应用占了,先查询,杀掉
ps = subprocess.Popen("netstat -aon|findstr 5037", shell=True, stdout=PIPE, stderr=PIPE)
out = ps.stdout.read().decode('utf-8')
split_a = out.split("\n")
for line in split_a:
print(line)
if "127.0.0.1:5037" in line:
result = re.findall(r"\d+", line)
if result:
target_pip = result[(len(result) - 1)]
print("有该线程 %s" % target_pip)
subprocess.Popen("taskkill /pid %s /f" % target_pip)
print("杀死线程 %s" % target_pip)
break
ps.stdout.close()
subprocess.Popen("adb kill-server")
subprocess.Popen("adb start-server")
except psutil.NoSuchProcess:
pass
pl = psutil.pids()
for pid in pl:
try:
if psutil.Process(pid).name().lower() == "dnplayer.exe":
is_found = True
print(">>>雷电模拟器已经打开了")
except psutil.NoSuchProcess:
pass
if not is_found:
# cmd 打开夜神
subprocess.Popen(r"D:\ChangZhi\dnplayer2\dnplayer.exe")
time.sleep(5)
# subprocess.Popen(r"D:\ChangZhi\dnplayer2\dnplayer.exe index=1")
print("执行cmd打开程序")
else:
print("雷电模拟器已经有进程启动了")
def close_dnp(self):
is_found = True
if is_found:
# 杀掉进程
ps = subprocess.Popen("taskkill /im dnplayer.exe /f", shell=True, stdout=PIPE, stderr=PIPE)
ps.stdout.close()
if __name__ == "__main__":
c = CheckDnplayer()
c.check_dnp_start()