Appium控件交互

news/2023/12/10 15:16:40

与Web元素操作一样(参考Selenium Web元素操作),定位到APP控件元素后,可以对控件进行一系列的操作,实现与APP交互,比如点击、文本输入、元素属性获取等。

目录

  • 元素操作
    • 元素常用操作方法
    • Python测试实例
  • 元素属性
    • get_attribute()方法
    • 属性获取实例
  • 系列文章

元素操作

元素常用操作方法

  • 点击方法 element.click()
  • 输入操作 element.send_keys(“appium”)
  • 设置元素的值 element.set_value(“appium”)
  • 清除操作 element.clear()

Python测试实例

安装ApiDemos-debug.apk,下载地址:https://github.com/appium/appium/blob/master/sample-code/apps/ApiDemos-debug.apk

  1. 点击Views
  2. 滑动选择并点击TextFields
  3. 输入文本“appium”
  4. 清除文本
#!/usr/bin/python3
#-*-coding:utf-8-*-
import pytest
from appium import webdriver
from appium.webdriver.common.mobileby import MobileByclass TestLocator: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'] = 'io.appium.android.apis'desired_caps['appActivity'] = '.ApiDemos'desired_caps['automationName'] = 'Uiautomator2'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(5)def teardown_method(self):self.driver.quit()def test_locator(self):self.driver.find_element_by_xpath('//*[@text="Views"]').click()self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("TextFields").instance(0));').click()elem = self.driver.find_element(MobileBy.ID, "io.appium.android.apis:id/edit")elem.send_keys("appium")elem.clear()

元素属性

  • 获取属性值:get_attribute(name)
  • 获取元素文本:element.text
  • 获取元素坐标:element.location
  • 获取元素尺寸(高和宽): element.size
  • 是否可见 element.is_displayed 返回True/False
  • 是否可用 element.is_enabled 返回True/False
  • 是否被选中 element.is_selected 返回True/False

get_attribute()方法

get_attribute()方法能获取的属性,元素的属性几乎都能获取到。属性名称和uiautomatorviewer里面的一致。

字符串类型:

  • text:返回 text
  • resource-id:返回 resource-id, API=>18
  • class:返回 class, API=>18
  • content-desc:返回 content-desc属性
  • bounds

布尔类型:

  • checkable
  • checked
  • clickable
  • enabled
  • focusable
  • focused
  • scrollable
  • long-clickable
  • password
  • selected
  • displayed

注意:有些属性展示在 uiautomatorviewer里,但是不能通过get_attribute获取,比如: index。

属性获取实例

获取“App”的属性值

from appium import webdriver
from appium.webdriver.common.mobileby import MobileByclass TestGetAttribute: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'] = 'io.appium.android.apis'desired_caps['appActivity'] = '.ApiDemos'desired_caps['automationName'] = 'Uiautomator2'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(5)def teardown_method(self):        self.driver.quit()def test_get_attribute(self):elem = self.driver.find_element_by_xpath('//*[@text="App"]')print("text:" + elem.get_attribute("text"))print("resource-id:" + elem.get_attribute("resource-id"))print("class:" + elem.get_attribute("class"))print("package:" + elem.get_attribute("package"))print("content-desc:" + elem.get_attribute("content-desc"))print("bounds:" + elem.get_attribute("bounds"))print("checkable:" + elem.get_attribute("checkable"))print("checked:" + elem.get_attribute("checked"))print("clickable:" + elem.get_attribute("clickable"))print("enabled:" + elem.get_attribute("enabled"))print("password:" + elem.get_attribute("password"))print("displayed:" + elem.get_attribute("displayed"))print("######################")print("text:" + elem.text)print(elem.location)print(elem.size)print(elem.is_displayed)print(elem.is_enabled)print(elem.is_selected)

执行结果:

text:App
resource-id:android:id/text1
class:android.widget.TextView
package:io.appium.android.apis
content-desc:App
bounds:[0,342][720,414]
checkable:false
checked:false
clickable:true
enabled:true
password:false
displayed:true
######################
text:App
{'x': 0, 'y': 342}
{'height': 72, 'width': 720}
<bound method WebElement.is_displayed of <appium.webdriver.webelement.WebElement (session="cceeee8f-ad89-4d00-ab4f-25b57de58ea5", element="a1443341-aad8-4814-8f9b-27ada3fe6f50")>>
<bound method WebElement.is_enabled of <appium.webdriver.webelement.WebElement (session="cceeee8f-ad89-4d00-ab4f-25b57de58ea5", element="a1443341-aad8-4814-8f9b-27ada3fe6f50")>>
<bound method WebElement.is_selected of <appium.webdriver.webelement.WebElement (session="cceeee8f-ad89-4d00-ab4f-25b57de58ea5", element="a1443341-aad8-4814-8f9b-27ada3fe6f50")>>
--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-25394.html

相关文章

基于PLC的棒料自动上料车削加工装置设计

目 录 1 绪论 1 1.1自动上下料机现状 1 1.2设计目的 1 1.3 国内外研究方向 2 1.4 设计原则 2 2 棒料自动上料车削加工装置方案设计 3 2.1自动上料车削加工装置的总体设计 3 2.1.1 车削加工装置总体结构的类型 3 2.1.2 设计具体采用方案 4 2.2 车削加工装置腰座结构的设计 4 2.…

Android WebView测试

混合应用中包含 Web 视图的应用&#xff0c;在 Appium 介绍及环境安装 中介绍了appium支持WebView测试&#xff0c;本文将分享Android 混合页面的测试方法。 目录WebView测试环境准备手机端PC端查看手机浏览器版本查看手机webview版本客户端代码WebView元素定位Android混合页面…

人脸识别(四)

源码位置&#xff1a;https://github.com/comhaqs/face_find.git 分支&#xff1a; develop_face_recognition opencv的人脸识别模块现在是放在另外一个库opencv_contrib里&#xff0c;需要编译到opencv里才可以使用&#xff0c;故这里将opencv和opencv_contrib的源码都下下来…

基于RFID的学生一卡通管理系统的设计与实现

目 录 摘 要 I ABSTRACT II 第1章 绪论 1 1.1选题背景及意义 1 1.2国内外研究现状 1 1.3研究主要内容 2 第2章 系统设计 4 2.1需求分析 4 2.1.1可行性分析 4 2.1.2功能需求概述 5 2.1.3 UML用例图 5 2.2学生一卡通的基本组成结构 7 2.2.1天线 7 2.2.2阅读器 7 2.2.3电子标签 11…

自动遍历测试之Monkey工具

某些移动APP业务线多&#xff0c;流程复杂&#xff0c;且产品迭代速度快&#xff0c;在回归测试中涉及到大量用例&#xff0c;工作量大&#xff0c;解决这个问题的一个可行方法是使用自动遍历测试&#xff0c;可以降低用例维护成本&#xff0c;尽可能的自动化覆盖回归业务。 目…

平昌县网上书城系统设计与实现

目 录 第一章 概述 1 1.1 开发背景 1 1.2 开发环境 2 1.3 开发工具 2 1.3.1 MyEclipse 2 1.3.2 Navicat 3 1.3.3 MySQL 3 第二章 需求分析 5 2.1 总体需求 5 2.3 功能需求 6 2.4 性能需求 6 2.5 运行需求 6 2.6 其他需求 7 2.7 可行性分析 7 2.7.1技术可行性 7 2.7.2 经济可行性…

Linux系统介绍:内核、shell及软件包管理

目录Linux内核版本查看系统版本shell常见 shell运行 shell查看Linux系统信息查看系统位数查看内存信息查看CPU信息Linux软件包管理源码编译安装RPMRPM包安装rpm命令其它用法yumDPKGDPKG 命令安装apt查看历史命令historyLinux 文件句柄Linux系统主要包括3层&#xff0c; 硬件&am…

基于MATLAB虚拟仿真的四杆机构运动分析

目 录 1 绪论 (1) 1.1 本文研究的目的意义 (1) 1.2 四杆机构的发展与应用 (1) 1.3 本文研究的主要内容 (2) 2 平面四杆机构运动学分析 (3) 2.1 四杆机构自由度的计算 (3) 2.2 角位移分析 (3) 2.3 角速度分析 (3) 2.4 角加速度分析 (3) 2.5 计算仿真前四杆机构的初始参数值 (4) …

VSCode + Python环境配置

VSCode Python开发环境配置 目录Visual Studio Code 下载安装设置 Python 环境安装code runner插件配置code runnerRun in Terminal其它配置配置VSCode自动保存快捷键Jupyter Notebook插件安装将ipynb文件转换为markdown系列文章Visual Studio Code 下载安装 Visual Studio Co…

基于Ocean Connect云平台的照明控制系统设计

目 录 摘 要 I Abstract II 1 绪论 1 1.1选题背景及意义 1 1.2国内外研究现状 2 1.2.1云平台发展现状 2 1.2.2智能照明的发展现状 2 1.3 研究主要内容 3 2 总体方案设计 5 2.1 系统的需求分析 5 2.2 系统架构设计 6 2.3 通信协议设计 7 2.3.1协议数据帧格式 7 2.3.2控制协议的具…