The road of airtest

airtest - poco

一、poco辅助窗功能

1、选择poco模式

测试什么选择对应什么

注意:(android、ios原生不需要接入poco-sdk,其他要)

 

2、检索控件

锁定检索

跟随检索

 

3、录制脚本

 

二、API介绍&定位选择器

①基本选择器利用元素的一些基本属性来进行定位,比如name、text等等

eg:poco(name="网易云音乐")

②相对选择器利用控件之间的父子关系、爷孙关系和兄弟关系等来定位控件,例如 parent()、child()、offspring()等

eg:poco("LinearLayoutCompat").child("搜索")

③空间顺序选择器常用于Ul树中多个相同名称的节点定位,根据节点在Ul树的索引顺序进行定位,索引坐标从0开始

poco("mainActivityTab").offspring("aActionBar$Tab")[0]

 

①点击操作:

poco("star_single").click()

poco('star_single'). long_click()

 

②读取和设置控件的属性:

poco("star_single").get_name()

poco("star_single").attr('name')

poco("star_single").get_text()

poco("pos_input").set_text("123")

poco("pos_input"). setattr('text',"456")

 

③判断元素是否存在:

poco(XXX).exists()

 

④拖动与滑动:

poco("star"). drag_to (poco("shel I"))

 

⑤内部偏移和外部偏移:

poco("pearl").focus([0.1,0.1]).long_click()

poco("pearl").focus([0.9,0.9]). long_click()

poco("pearl").focus([0.5,-3]). long_click()

 

⑥遍历元素:

for star in poco("playDragAndDrop").child("star"):star. drag_to (poco("shell"))

 

⑦等待事件:

poco("bomb").wait_for_appearance()

poco("bomb").wait_for_disappearance()

yellow = poco("yellow")

blue = poco("blue")

black = poco("black")

poco.wait_for_all([yellow,blue,black])

poco.wait_for_any([bomb,yellow,blue])

 

可以下载对应demo进行操作:android_poco_demo download

其他详情可以查看:官网功能详情

 

airtest - selenium

一、元素定位语句

1、find_element_by_id()

2、find_element_by_name()

3、find_element_by_class_name()

4、find_element_by_xpath()

5、find_element_by_link_text()

6、find_element_by_partial_link_text()

7、find_element_by_css_selector()

二、操作元素

元素点击:

driver.find_element_by_xpath("[@id=\"right_content\"]").click()

输入:

driver.find_element_by_id("title").send_keys("123")

元素存在:

driver.assert_exist("//div[@alt='到学术首页']","xpath","请填写测试点.")

三、元素定位与实操

下拉框定位(用到的话,需要先引用)

引入下拉框类:Select

from selenium.webdriver.support.select import Select

定位到下拉框元素:

ime_select = driver.find_element_by_id('ime')

实例下拉框类:

select = Select(ime_select)

3种定位方式:

select.select_by_index(0)

select.select_by_value('2')

select.select_by_visible_text('关闭')

 

弹出框定位(用到的话,需要先引用)

引入弹出框类:Alert

from selenium.webdriver.common.alert import Alert

获取弹出框的文本信息:

driver. switch_to_alert().text

点击弹出框的确认按钮:

driver.switch_to_alert().accept()

 

四、Selenium辅助窗口功能

1、窗口开启Selenium Window

2、设置Chrome Path路径

设置好airtest ide Chrome Path路径,选择本地安装的chrome.exe即可

3、插入初始化语句

4、Selenium辅助窗功能实操

浏览器截图:driver.airtest_touch

5、利用web F12功能,定位xpath属性

 

 

查看所有window窗口句柄

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

import win32gui
import win32con
from pywinauto import Desktop
from airtest.core.api import *

auto_setup(__file__)


def list_all_windows():
    def callback(hwnd, extra):
        if win32gui.IsWindowVisible(hwnd):
            title = win32gui.GetWindowText(hwnd)
            print(f"0x{hwnd:x} : {title}")
    win32gui.EnumWindows(callback, None)

list_all_windows()

 

 

脱离IDE运行

一、本地python环境设置

1、不用ide自带,用自己python本地环境(python版本≤3.9)

2、本地python需要安装的库

 使用 pip install airtest 和 pip install pocoui,分别安装airtest库和pocoui库;(特别注意:poco框架的库名为pocoui,不能安装成poco)

3、删除库的命令

pip uninstall airtest;更新库到最新版本的命令:pip install-U airtest

如遇到安装超时的报错,可以换成国内源安装豆瓣源:  pip install airtest -i https://pypi.tuna.tsinghua. edu.cn/simple

4、核对安装是否正确

pip list

 

二、命令行跑脚本

1、airtest run 命令的可传参数:

一device,用来指定连接的被测设备

一log,用来指定log内容和截图存放的目录

一recording,运行脚本时进行录屏操作

-no-image,传入该参数后,在脚本运行过程中不再保存截图(airtest≥1.1.7)

 

运行示例:

airtest run D:/test/report_test.air —device Android:/// —log D:/log/ —recordingD:/log/record01.mp4

释义:在D:/test/目录下,有1个叫report_test.air的脚本;命令行运行这个脚本,指定测试设备为连接上的第一台安卓设备,log保存路径为D:/log/,运行脚本时进行录屏操作,录屏文件保存到D:/log/record01.mp4

 

 

 

 

 

 

 

 

 

Chrome设置

1、下载chromedriver

https://googlechromelabs.github.io/chrome-for-testing/#stable

查询下自己安装的chrome版本,找到并下载对应的chromedriver版本

https://google-chrome.cn.uptodown.com/windows/versions 各对应版本

2、更新对应chromedriver

在对应"AirtestIDE-win-1.2.16\AirtestIDE"目录下放chromedriver

//在chrome目录中放chromedriver

//可以在python目录下也放一个

 3、关闭chrome自动更新,防止版本太新,无法使用

cmd运行services.msc,找到以下,改为手动

 

posted on 2023-12-04 00:05  Damon_Ding  阅读(36)  评论(0)    收藏  举报