# -*- encoding=utf8 -*-
__author__ = "admin、"
from airtest.core.api import *
from airtest.report.report import simple_report
from airtest.report.report import LogToHtml
from airtest.core.helper import G
from threading import Thread
from random import randint
from random import choice
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
import logging
import traceback,sys
import time
auto_setup(__file__,devices=["Android://127.0.0.1:5037/A123456"]) #logdir默认为None
"""auto_setup(__file__,devices=["Android://127.0.0.1:5037/123","Android://127.0.0.1:5037/456"])多台设备的示例"""
"""https://mp.weixin.qq.com/s?__biz=MzUxMDc4NTkwMA==&mid=2247484283&idx=1&sn=a82976cfe898711cc560592942c52d74&scene=21#wechat_redirect"""
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
"""多机协作
poco2=AndroidUiautomationPoco(device=G.DEVICE_LIST[1],use_airtest_input=True,screenshot_each_action=False)
这里不能直接将设备号传入,否则AttributeError: 'str' object has no attribute 'adb'。G.DEVICE_LIST传入的是内存地址
实例化Poco的顺序 必须和auto_setup的顺序相一致"""
logger=logging.getLogger("airtest")
logger.setLevel(logging.ERROR)
class Case(object):
def __init__(self):
#初始化设备
size = poco.get_screen_size() #(720,1560)
self.width = size[0] #屏宽
self.height = size[1] #屏高
def contacts(self): #联系人
os.system("adb shell am start -n com.google.android.contacts/com.android.contacts.activities.PeopleActivity")
try:
if poco("android.widget.LinearLayout").offspring("android:id/button2").exists:
poco(text="跳过").click()
sleep(1)
except Exception as E:
print("不需要跳过:%s" %(E))
if poco(text="搜索联系人").exists():
poco(text="搜索联系人").click()
os.system("adb shell input text b")
name = poco("com.google.android.contacts:id/cliv_name_textview")[0].get_text()
Judgment_value=isinstance(name,str) #True
print(Judgment_value)
if Judgment_value==True:
pass
else:
print("请检查是否有联系人")
sleep(1)
keyevent("home")
# def chrome_position(self):
"""守护线程"""
# try:
# if poco(text="www.baidu.com 想使用您设备的位置信息",name="com.android.chrome:id/text").wait(300).exists:
# poco(text="允许",name="com.android.chrome:id/positive_button").click()
# except:
# pass
# sleep(5)
#
# a_Thread=Thread(target=chrome_position)
# a_Thread.setDaemon(True)
# a_Thread.start()
def chrome(self): #chorme浏览器
# size = poco.get_screen_size() # 获取屏幕长宽比例
# width = size[0]
# height = size[1]
start_pos = (0.5 * self.width,0.8 * self.height)
end_pos = (0.5 * self.width, 0.2 * self.height)
start_app("com.android.chrome")
#os.system("adb shell am start com.android.chrome")
try:
poco(text="接受并继续").wait(3)
poco(text="接受并继续").click()
poco(text="不用了,谢谢").click()
poco(text="搜索或输入网址", name="com.android.chrome:id/search_box_text").wait_for_appearance(4)
poco(text="搜索或输入网址", name="com.android.chrome:id/search_box_text").click()
text("www.baidu.com")
sleep(5)
except Exception as e:
log(e,snapshot=True)
try:
sleep(6)
poco("index-kw").click() # 点击搜索
text("爱奇艺")
try:
if poco(text="m.baidu.com 想使用您设备的位置信息", name="com.android.chrome:id/text").wait(300).exists:
poco(text="允许", name="com.android.chrome:id/positive_button").click()
except:
pass
# poco("android.widget.LinearLayout").offspring("com.android.chrome:id/compositor_view_holder").offspring("android.webkit.WebView")[0].offspring("index-form").child("android.view.View").click()
poco(text="爱奇艺", name="kw").wait_for_appearance(30) # 超时30s
except Exception as E:
print("检查错误:%s" % (E))
while True:
if poco(text="下一页", name="android.widget.TextView").exists(): # 一直往下滑动,直到最底页
break
else:
swipe(start_pos, end_pos, duration=0.5)
sleep(0.5)
for i in range(10):
# duration 滑动时长 steps步长 fingers手指头数目
swipe(end_pos, start_pos, duration=1, steps=6, fingers=1) # 从继续往上滑动
sleep(5)
"""滑动失败,待改动"""
#swipe((width,0.5*height),(0,0.5*height)) #最左端的滑动
poco("android.widget.LinearLayout").offspring("com.android.chrome:id/menu_button").click() #添加书签
sleep(2)
touch(Template(r"收藏网址.png", record_pos=(-0.018, -0.915), resolution=(720, 1560))) #书签星标 """书签星标元素无法定位,改用touch"""
poco(text="爱奇艺", name="kw").click() #定位爱奇艺输入框,删除,并重新输入
for i in range(5):
keyevent("67")
text("腾讯视频")
sleep(5)
touch(Template(r"点击腾讯视频官网.png"))
tengxun_click = (0.5 * self.width, 0.35 * self.height)
touch(tengxun_click)
poco(text="播放", name="android.widget.Button").click()
for i in range(1, 8):
keyevent("24") #加大音量
for i in range(1, 12):
keyevent("25") #减少音量
poco("android.widget.LinearLayout").offspring("com.android.chrome:id/menu_button").click() # 添加书签
sleep(2)
touch(Template(r"收藏网址.png", record_pos=(-0.018, -0.915), resolution=(720, 1560))) # 书签星标 """书签星标元素无法定位,改用touch"""
def Clock(self):
pass
def Camera(self):
screen = r"X:\Airtest\Test_Case.air\screen" # 跑失败截图的路径
report_path = r"X:\Airtest\Test_Case.air\report" # 生成报告的路径
report_name = report_path + time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime()) + ".html" # 报告的名字
start_app("com.freeme.camera") # 启动相机
self.paizhao = poco("com.freeme.camera:id/shutter_button")
try:
def photo(): # 普通拍照
poco(text="拍照").click()
# poco(text="拍照").click()
for i in range(5):
self.paizhao.click()
def meiyan(): # 美艳
poco(text="请问").click()
for i in range(randint(3, 5)):
self.paizhao.click()
def mengyan():
poco(text="萌颜").click()
for i in range(5):
self.paizhao.click()
def xuhuan():
poco(text="虚化").click()
for i in range(5):
self.paizhao.click()
def renxiang():
poco(text="人像").click()
for i in range(5):
self.paizhao.click()
def guangjiao():
poco(text="广角").click()
for i in range(5):
self.paizhao.click()
def shipin(): #摄像
poco(text="视频").click()
sleep(2)
vss = poco("com.freeme.camera:id/btn_vss")
resume = poco("com.freeme.camera:id/btn_pause_resume")
random_action = [vss, resume] # 一拍照 2暂停
for i in range(5):
self.paizhao.click() # 开启摄像
for q in range(5):
choice(random_action).click()
sleep(10)
self.paizhao.click() # 停止
def houzhi():
photo()
meiyan()
# mengyan()
# xuhuan()
renxiang()
def qianzhi():
photo()
meiyan()
# mengyan()
# xuhuan()
renxiang()
houzhi() # 后置拍照
poco("com.freeme.camera:id/camera_switcher").click() # 切换 前置
qianzhi() # 前置拍照
shipin()
except:
snapshot(filename=screen + time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime()) + ".jpg", msg="跑失败的照片",
quality=80) # 截图
simple_report(__file__, logpath=r"X:\Airtest\Test_Case.air", output=report_name)
self.Camera() # 失败之后重来
finally:
simple_report(__file__, logpath=r"X:\Airtest\Test_Case.air", output=report_name) #output不能接11.html 必须接绝对路径
#simple_report() #1、考虑怎么终端程序也会出报告
#2、跑多轮出多次报告
def storage(self):
pass
def search(self):
pass
def stream_media(self):
pass
def GMail(self):
pass
def message(self):
set_current(1)
stop_app("com.google.android.apps.messaging/com.google.android.apps.messaging.ui.ConversationListActivity") # 启动短信
set_current(0)
os.system(
"adb -s A123456 shell am start -n com.google.android.apps.messaging/com.google.android.apps.messaging.ui.ConversationListActivity") # 启动短信
poco(text="开始聊天", name="com.google.android.apps.messaging:id/start_new_conversation_button").click()
text("19914450031")
sleep(2)
text("你好他好才是真的好")
# poco1(name="com.google.android.apps.messaging:id/compose_message_text").set_text("1212121") #使用pooo也可以
poco(name="com.google.android.apps.messaging:id/send_message_button_icon").click()
keyevent("BACK")
a=Case()
a.Camera()
08-08
08-08
08-08