airtest常用命令及实战


airtest

安装

官网地址Airtest Project (netease.com)

启动程序连接手机

你可以使用账号登录或直接点击左下角“skip”按钮跳过。

启动Android模拟器或者用PC连接一台手机。通过adb命令检查移动设备。

> adb devices
List of devices attached
46HDU18C27004111        device

常用命令

touch

touch(Template(r"tpl1627996328848.png", record_pos=(-0.339, 0.003), resolution=(1080, 2310)))
IDE默认生成的代码,按照图片查找
默认times=1表示点击一次,需要多次点击修改添加times,跟上次数就行

touch([10,10])
根据位置去点击

touch命令如果没有匹配到图片会报错,需要进行异常处理,否则程序会终止

swipe

用法比较多,具体可以参考文章swipe滑动

常用的两种用法

swipe((672,1214),(336,1305),duration=0.3)
从一个坐标滑动到另一个坐标,可以是元祖,也可以是数组
duration为滑动所花的时间,根据实际情况尽量真实就行

swipe(Template(r"tpl1628053242231.png", record_pos=(0.011, -0.172), resolution=(1080, 2310)), vector=[0.2684, 0.0025])

r"tpl1628053242231.png"为获取的图像的中心点X,Y坐标
vector 滑动的屏占百分比,向右为X轴正向, 向下为Y轴正向

exists

好东西,使用比较多

exists(Template(r"tpl1628003095865.png", record_pos=(0.014, 0.048), resolution=(1080, 2310)))
# 检测图片存在不存在,不存在返回None,存在则默认返回中心X,Y坐标

# 比如先检查下存在不存在,再去拿到坐标去点击,还可以根据实际情况,修改坐标的相对位置
ad_p = exists(Template(r"tpl1628003095865.png", record_pos=(0.014, 0.048), resolution=(1080, 2310)))
if ad_p:
    touch([866,ad_p[-1]])
    sleep(4.0)

assert_exists

与exists类似,assert找不到会执行AssertionError

在查找的过程中exists如果找不到,只会执行两次查找

assert_exists,如果没找到,会执行多次重新查找匹配的过程

sleep

和time.sleep功能一样,程序暂停运行时间

sleep(15.0)
暂停15秒

keyevent

不通过触摸手机来触发手机的按键事件

keyevent("BACK")
返回
keyevent("HOME")
返回主页
keyevent("POWER")
按下手机电源键

建议

学习python语言基础,airtestIDE中支持,python标准库直接使用,和python语法

代码参考

抖音极速版自动刷任务广告,及首页广告

# -*- encoding=utf8 -*-
__author__ = "admin"

from airtest.core.api import *
import random

# 定义要用的常量参数
# 宝箱位置
box_position = [920,2017]

# from poco.drivers.android.uiautomation import AndroidUiautomationPoco
# poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
# 主页视频上滑
def ad_up():
    swipe([540,1800],[540,500],duration=0.3)
    sleep(0.5)

# 上滑    
def up():
    swipe([540,1300],[540,500],duration=0.3)
    sleep(0.5)
    
# 下滑    
def down():
    swipe([540,500],[540,1300],duration=0.3)
    sleep(0.5)
    
# 关闭广告,再打开一个,再关闭
def close_ad():
    keyevent("BACK")
    sleep(3.0)
    # 获取继续看广告坐标,没有就关闭
    ad_p = exists(Template(r"tpl1628018577239.png", record_pos=(0.008, 0.019), resolution=(1080, 2310)))

    if ad_p:
        touch([ad_p[0],ad_p[1]+200])
        sleep(38.0)
        keyevent("BACK")
        
# 主页广告浏览
def first_ad():
    for i in range(10):
        ad_up()
        sleep(random.randint(10,30))
    if exists(Template(r"tpl1627998671215.png", record_pos=(0.001, 0.867), resolution=(1080, 2310))):
        touch(Template(r"tpl1627998671215.png", record_pos=(-0.001, 0.872), resolution=(1080, 2310)))

# 看广告赚金币
def look_ad():
    down()
    down()
    down()
    up()
    ad_p = exists(Template(r"tpl1628003095865.png", record_pos=(0.014, 0.048), resolution=(1080, 2310)))
    if ad_p:
        touch([866,ad_p[-1]])
        sleep(4.0)
        if exists(Template(r"tpl1628003095865.png", record_pos=(0.014, 0.048), resolution=(1080, 2310))):
            return False
        sleep(38.0)
        close_ad()
        return True
    else:
        return False

# 开宝箱得金币
def box_ad():
    touch(box_position)
    sleep(5.0)
    if exists(Template(r"tpl1628010958896.png", record_pos=(-0.101, 0.108), resolution=(1080, 2310))):
        touch(Template(r"tpl1628002008078.png", record_pos=(-0.101, 0.109), resolution=(1080, 2310)))
        sleep(38.0)
        close_ad()
        return True
    else:
        return False

# 检测异常,重新走程序
def is_error():
    # 正常继续
    if exists(Template(r"tpl1628003095865.png", record_pos=(0.014, 0.048), resolution=(1080, 2310))):
        return True
    # 异常关闭到主界面
    else:
        for i in range(6):
            keyevent("BACK")
            sleep(1.0)
        return False
    
def make_money():
    # 打开抖音app,打开来赚钱界面
    start_app("com.ss.android.ugc.aweme.lite")
    sleep(2.0)
    touch(Template(r"tpl1627998671215.png", record_pos=(-0.001, 0.872), resolution=(1080, 2310)))
    sleep(2.0)

def run():
    make_money()
    n = 40
    normal = True
    while n > 0:
        if look_ad():
            n -= 1
            if not exists(Template(r"tpl1628003095865.png", record_pos=(0.014, 0.048), resolution=(1080, 2310))):
                keyevent("BACK")
        if box_ad():
            n -= 1
            if not exists(Template(r"tpl1628003095865.png", record_pos=(0.014, 0.048), resolution=(1080, 2310))):
                keyevent("BACK")
        sleep(2.0)
        # 3次做一次异常检测重新开启程序
        if n%3 == 0:
            normal = is_error()
        if normal:
            keyevent("BACK")
            first_ad()
        else:
            make_money()
            normal = True
            
if __name__ == '__main__':
    # script content
    print("start...")
    run()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM