pocoui:https://poco.readthedocs.io/zh_CN/latest/source/README.html
aritest:https://airtest.doc.io.netease.com/IDEdocs/airtest_framework/8_airtest_using/
环境
MUMU模拟器:2.6.30.0 python:3.8.5 adb:添加进环境变量,adb的环境变量可以添加IDE中adb.exe的路径 ide:下载airtest IDE
1、utils/connect.py
__author__ = 'xiao'
from airtest.core.api import *
from airtest.core.error import AdbError, DeviceConnectionError
# use url to connect application
def connect(url, name):
try:
connect_device(url)
start_app(name)
except [DeviceConnectionError, AdbError]:
print('connect error')
else:
print('connect success')
2、utils/login_wechat.py
__author__ = 'xiao'
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from poco.exceptions import PocoNoSuchNodeException
from utils import resolve_yaml
from utils.wait_node import *
poco = AndroidUiautomationPoco()
config = resolve_yaml.resolve('./config/setting.yaml')
# password login
def login_with_password(password):
flag = True
try:
password_text = poco(config['password_name'])
login_button = poco(config['login']['name'], text=config['login']['text'])
password_text.set_text(password)
login_button.click()
node = wait_node(poco(textMatches='^微信.*'), 2)
node1 = wait_node(poco(textMatches='^登录失败'), 2)
if node1:
print('login failed')
flag = False
return flag
elif node:
print('login success')
return flag
else:
raise PocoNoSuchNodeException('No such this node')
except PocoNoSuchNodeException as e:
flag = False
return flag
# QR code login
def login_with_qr():
flag = True
try:
button = poco('com.tencent.mm:id/g5q')
button.click()
except PocoNoSuchNodeException as e:
print(e)
3、utils/node_filter.py
__author__ = 'xiao'
import re
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from utils import resolve_yaml
poco = AndroidUiautomationPoco()
config = resolve_yaml.resolve('./config/setting.yaml')
# filter node
def node_filter(node, temp=None):
if temp is None:
temp = []
if not node.child():
# Add filtered node to the array
if (node.attr('text') is not None) and re.search(config['reg'], node.attr('text')) is not None:
temp.append(node)
else:
# Recursively filter child elements
for i in node.child():
node_filter(i, temp)
return temp
4、utils/resolve_yaml.py
__author__ = 'xiao'
import yaml
# parsing yaml files
def resolve(path):
with open(path, encoding='UTF-8') as file:
result = yaml.safe_load(file.read())
return result
5、utils/send_msg.py
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from utils.wait_node import *
from utils import resolve_yaml
poco = AndroidUiautomationPoco()
config = resolve_yaml.resolve('./config/setting.yaml')
# send text message
def send_text(node, times):
print(config['send_button'])
for i in range(times):
try:
wait_node(node, 1).click()
wait_node(poco(config['edit_text'], type=config['edit_text_type']), 1).set_text(config['send_text'])
wait_node(poco(config['send_button'], type=config['button_type']), 1).click()
wait_node(poco(config['back'], type=config['back_type']), 1).click()
break
except Exception as e:
wait_node(poco(config['back'], type=config['back_type']), 1).click()
continue
6、utils/wait_node.py
__author__ = 'xiao'
import time
# Cycle waiting for nodes within the timeout period
def wait_node(node: object, interval: int, timeout=12):
start_time = time.time()
end_time = start_time + timeout
while time.time() <= end_time:
if node:
return node
else:
time.sleep(interval)
if node:
return node
else:
return None
7、config/setting.yaml
adb_connect: 127.0.0.1:7555
url: android://127.0.0.1:5037/127.0.0.1:7555
app: com.tencent.mm
password: wxwcd08080927
password_name: com.tencent.mm:id/cd6
login:
name: com.tencent.mm:id/g5v
text: 登录
send_text: 秘密
wechat_list: com.tencent.mm:id/gkw
back: com.tencent.mm:id/fz
back_type: android.widget.LinearLayout
send_button: com.tencent.mm:id/b8k
button_type: android.widget.Button
edit_text: com.tencent.mm:id/b4a
edit_text_type: android.widget.EditText
stop_node: com.tencent.mm:id/b7f
reg: ^微信测试.*
8、main.py
__author__ = 'xiao'
import os
import re
from utils import resolve_yaml
from utils.connect import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
from utils.wait_node import *
config = resolve_yaml.resolve('config/setting.yaml')
if __name__ == '__main__':
# connect devices
if len(os.popen('adb devices -l').readlines()) == 2:
os.system(f"adb connect {config['adb_connect']}")
from utils.login_wechat import *
from utils.node_filter import *
from utils.send_msg import *
poco = AndroidUiautomationPoco()
connect(config['url'], config['app'])
login_with_password(config['password'])
dist = []
switch = False
while True:
frozen_poco = poco.freeze()
items = wait_node(frozen_poco(config['wechat_list']).child(), 1)
count = 0
for i in items:
try:
count += 1
# Determine whether there is duplicate data
if len(node_filter(i)) != 0 and dist.count(node_filter(i)[0].attr('text')) != 1:
send_text(i, 3)
# If there is no repetition, it is added to the array
dist.append(node_filter(i)[0].attr('text'))
if count == 9 and items[11].attr('name') != config['stop_node']:
position = i.get_position()
frozen_poco.swipe(position, [i.get_position()[0], 0.0865], duration=0.5)
break
else:
continue
elif i.attr('name') == config['stop_node']:
switch = True
continue
except Exception as e:
break
if switch is False:
continue
else:
# Back to the start position
for i in range(count):
frozen_poco.swipe([0.49, 0.1], [0.49, 0.8], duration=0.5)
break
9、效果如下,自动发信息,自动翻页