Appium元素定位(一)

news/2025/1/25 20:12:06

APP元素定位方式与Web元素定位方式大体相同,APP自动化测试中最重要的一部分是对元素进行定位,实现对APP的控制交互。Appium常用的定位方式有Accessibility ID、Class name、ID、Name、XPath、Android UiAutomator(UiAutomator2)等。

目录

  • ID定位
  • Accessibility ID定位
  • Class name定位
  • Xpath定位
  • Toast定位
    • toast介绍
    • toast定位
  • APP自动化测试实例
  • 系列文章

ID定位

使用resource-id属性定位,iOS中使用name属性。

elem = driver.find_element_by_id("com.xueqiu.android:id/enter_stock_fund")
elem = driver.find_element(MobileBy.ID,"com.xueqiu.android:id/enter_stock_fund")

Accessibility ID定位

在写Android和iOS自动化测试用例时,可以使用这种定位方法,使代码可重用, 实现跨平台自动化测试。iOS的Accessibility ID为UI元素的名称, Android的Accessibility ID为“ content-desc”属性值。

elem = driver.find_element_by_accessibility_id("Accessibility")
elem = driver.find_element(MobileBy.ACCESSIBILITY_ID,"Accessibility")

Class name定位

使用控件的class属性

elem = driver.find_element_by_class_name("classname")
elem = driver.find_element(MobileBy.CLASS_NAME,"classname")

Xpath定位

APP Xpath定位与Web元素Xpath定位一样,Xpath定位语法可参考文章 Web自动化测试:xpath & CSS Selector定位,可以通过父结点定位子结点、子结点定位父结点、子结点定位兄弟结点、 爷爷结点定位孙子结点。

Xpath定位语法参考:https://www.w3school.com.cn/xpath/xpath_syntax.asp

表达式描述
nodename选取此节点的所有子节点。
/从根节点选取。
//从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
.选取当前节点。
选取当前节点的父节点。
@选取属性。

点击雪球APP “行情”

elem = driver.find_element_by_xpath('//*[@text="行情"]').click()
elem.click()

Toast定位

toast介绍

  • Toast是手机应用消息提示框,为当前视图显示一个浮动弹出框
  • Toast类的思想:尽可能不引人注意,同时还向用户显示信息,希望他们看到
  • Toast显示的时间有限, Toast会根据用户设置的显示时间后自动消失。
  • Toast本身是个系统级别的控件,它归属于系统 settings,当一个app发送消息的时候,不是自己造出来的这个弹框,它是发给系统,由系统统一进行弹框,这类的控件不在app内,需要特殊的控件识别方法。
  • Appium使用 uiautomator底层的机制来分析抓取 Toast,并且把 Toast放到控件树里面,但本身并不属于控件。
  • 必须使用 xpath查找
    • //*[@class=‘android.widget.Toast’]
    • //*[contains(@text, “xxxxx”)]

toast定位

测试步骤:

  1. 打开ApiDemos
  2. 进入Popup Menu页面
  3. 点击"search"
  4. 打印弹出toast内容

from appium import webdriver
from appium.webdriver.common.mobileby import MobileByclass TestToast():# API Demosdef setup(self):desired_caps = {'platformName': 'android','platformVersion': '6.0.1','deviceName': '127.0.0.1:7555','appPackage': 'io.appium.android.apis','appActivity': 'io.appium.android.apis.view.PopupMenu1','automationName': 'Uiautomator2'}self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)self.driver.implicitly_wait(5)def teardown(self):self.driver.quit()def test_toast(self):self.driver.find_element_by_class_name("android.widget.Button").click()self.driver.find_element_by_xpath("//*[@text='Search']").click()# print(self.driver.page_source)print(self.driver.find_element(MobileBy.XPATH, "//*[contains(@text,'Clicked popup')]").text)

结果:

Launching pytest with arguments test_toast.py::TestToast::test_toast in D:\ProgramWorkspace\TestingDemo\test_appium============================= test session starts =============================
platform win32 -- Python 3.7.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- D:\Anaconda3\python.exe
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('D:\\ProgramWorkspace\\TestingDemo\\test_appium\\.hypothesis\\examples')
rootdir: D:\ProgramWorkspace\TestingDemo\test_appium
plugins: allure-pytest-2.8.12, hypothesis-5.5.4, arraydiff-0.3, assume-2.3.2, astropy-header-0.1.2, doctestplus-0.5.0, openfiles-0.4.0, remotedata-0.3.2, rerunfailures-9.1
collecting ... collected 1 itemtest_toast.py::TestToast::test_toast ============================= 1 passed in 37.07s ==============================Process finished with exit code 0
PASSED                              [100%]Clicked popup menu item Search

APP自动化测试实例

测试过程:

  1. 打开雪球app
  2. 点击搜索框
  3. 输入"招商银行"
  4. 选择
  5. 获取股价,并判断

Note:

  1. 实例使用的是网易mumu浏览器,开启后,使用adb命令:adb connect 127.0.0.1:7555 连接模拟器

  2. 执行代码前需要开启appium服务器

python代码:

import pytest
from appium import webdriverclass TestXueQiu:def setup(self):desired_caps = {}desired_caps['platformName'] = 'Android'desired_caps['platformVersion'] = '6.0.1'desired_caps['deviceName'] = '127.0.0.1:7555'desired_caps['appPackage'] = 'com.xueqiu.android'desired_caps['automationName'] = 'Uiautomator2'desired_caps['appActivity'] = 'com.xueqiu.android.common.MainActivity'desired_caps['newCommandTimeout'] = 3000desired_caps['noReset'] = Truedesired_caps['dontStopAppOnReset'] = Truedesired_caps['skipDeviceInitialization'] = Truedesired_caps['unicodeKeyboard'] = Truedesired_caps['resetKeybBoard'] = Trueself.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)self.driver.implicitly_wait(15)def teardown_method(self):self.driver.quit()def test_search(self, searchkey, type, price):self.driver.find_element_by_id("com.xueqiu.android:id/tv_search").click()self.driver.find_element_by_id("com.xueqiu.android:id/search_input_text").send_keys("招商银行")current_price = self.driver.find_element_by_xpath("//*[@text='SH600036']").click()current_price = self.driver.find_element_by_xpath("//*[@text='SH600036']/../../..//*[@resource-id='com.xueqiu.android:id/current_price']").text  print(current_price)assert float(current_price) > 40

执行结果:

--THE END--

系列文章

1、Appium 介绍及环境安装
2、selenium/appium 等待方式介绍
3、App控件定位:Android 控件介绍及元素定位方法
4、Appium元素定位(一)
5、Appium元素定位(二):UiAutomator定位
6、Appium控件交互
7、Android WebView测试
8、AppCrawler自动遍历测试
9、自动遍历测试之Monkey工具
10、App自动化测试工具Uiautomator2
11、App自动化测试工具Airtest
12、Android手机管理平台搭建:STF和atxserver2
13、Windows上实现iOS APP自动化测试:tidevice + WDA + facebook-wda / appium
14、iOS APP自动化:predicate定位
15、iOS APP自动化:class chain定位方法
16、使用facebook-wda进行iOS APP自动化测试


欢迎关注公众号:「测试开发小记」及时接收最新技术文章!


https://dhexx.cn/news/show-25406.html

相关文章

人脸识别(一)

想学习下人脸识别,故计划写几篇文章,用来记录和帮助他人 程序从远端拉去视频流,通过ffmpeg解码,然后opencv进行人脸检测和人脸识别,将识别后的结果画在视频上,然后推送到目的端。同时,会将识别…

opencv3.0使用Eigen方法进行人脸识别的方法

直接上代码 #include <opencv2\core.hpp> #include <opencv2\face.hpp> #include <opencv2\highgui.hpp>#include <stdlib.h> #include <stdio.h> #include <time.h> #include <algorithm> #include <vector> #include <l…

Appium元素定位(二):UiAutomator定位

UiAutomator定位用于Android APP的元素定位&#xff0c;使用UI Automator API&#xff08;UISelector类&#xff09;来搜索特定元素。 Appium将Java代码作为字符串发送到服务器实现对应用程序的交互。 目录uiautomator定位方式resource-id定位classname定位content-desc定位通过…

人脸识别(二)

源代码路径&#xff1a;https://github.com/comhaqs/face_find.git 分支 develop_step1 第一阶段得使用ffmpeg解码视频流并在qt上显示&#xff0c;这里使用的是一段电视剧视频。Qt上是使用QLabel控件显示QImage对象&#xff0c;但QImage对象只识别AV_PIX_FMT_RGBA图像数据&a…

vs12 vs2013下open3.0配置扩展模块

在使用人脸识别face.hpp的时候&#xff0c;如果直接在opencv官网下载的已编译好的.exe安装的话将没有扩展库的功能&#xff0c;如果要使用扩展库&#xff0c;必须要进行扩展库的编译。 1、准备资源 opencv未编译版&#xff1a; https://github.com/Itseez/opencv opencv扩展…

基于Python的图片隐写算法设计与实现

目 录 摘 要 1 第一章 绪论 2 1.1 研究背景 2 1.1.1 信息隐藏技术 2 1.1.2 图像格式 2 1.1.3 信息隐写的国内外发展 3 1.2 研究意义 3 1.3 本文研究的主要内容 3 第二章 理论研究与分析 4 2.1 图像隐写术 4 2.1.1 图像隐写术 4 2.1.2 图像隐写的流程 4 2.2图像隐写算法 5 2.2.1…

Linux三剑客grep、awk和sed

grep&#xff0c;sed 和 awk是Linux/Unix 系统中常用的三个文本处理的命令行工具&#xff0c;称为文本处理三剑客。本文将简要介绍这三个命令的基本用法以及它们在Windows系统中的使用方法。 目录管道grep定义选项参数实例1&#xff1a;查找文件内容&#xff0c;显示行号实例2&…

人脸识别(三)

源码位置&#xff1a;https://github.com/comhaqs/face_find.git 分支&#xff1a; develop_step2 第二阶段&#xff0c;通过opencv实现人脸识别。opencv的Mat类只支持BGR模式图像&#xff0c;所以需要进行图像转换。这里ffmpeg解码出来是AV_PIX_FMT_YUV420P&#xff0c;先转…

基于html的旅游网站的设计与实现

目 录 第一章 绪论 1 1.1. 选题背景及意义 1 1.2. 国内外研究现状 1 1.3. 研究主要内容 2 第二章 开发工具的选用及介绍 3 2.1. 网页开发技术 3 2.1.1. HTML简介 3 2.1.2. DIVCSS简介 3 2.1.3. bootstrap 3 2.2. 网页制作工具 4 2.2.1. Photoshop 4 2.2.2. Dreamweaver 4 2.2.3…

PHP代码覆盖率

先安装xdebug 参考&#xff1a;https://blog.csdn.net/nece001/article/details/112713712 xdebug的这一项配置必须要有coverage&#xff0c;逗号间不能有空格&#xff01; xdebug.mode debug,coverage 代码覆盖率 参考文档&#xff1a;&#xff08;代码已过时&#xff0c…