当前位置:网站首页>APP自动化测试-4. App控件交互
APP自动化测试-4. App控件交互
2022-07-20 13:23:00 【灯塔-tester】
APP自动化测试-4. App控件交互
前言
简单介绍元素的操作方法和元素的属性
一、元素常用的操作方法
- click():元素点击
- send_keys():对元素进行输入操作
- set_value():设置元素的值
- clear():清除元素的值
- is_displayed():判断元素是否可见
- is_enable():判断元素是否可用
- is_selected():判断元素是否为选中状态
代码示例:
from time import sleep
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
desired_caps = {
"platformName": "android",
"appium:deviceName": "b1f37e8e",
"appium:appPackage": "com.jianshu.haruki",
"appium:appActivity": "com.baiji.jianshu.ui.splash.SplashScreenActivity",
# 添加noReset后,会记录该应用之前的操作
"noReset": True,
# 设置dontStopAppOnReset后,如果应用已打开,则不会关闭应用重新打开
"dontStopAppOnReset": True,
# 跳过安装及权限设置操作,提升执行速度
"skipDeviceInitialization": True
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置隐式等待
driver.implicitly_wait(3)
# 获取元素
element = driver.find_element(by=AppiumBy.ID, value="com.jianshu.haruki:id/et_account")
# 点击元素
element.click()
# 元素中输入内容
element.send_keys("this is a test message")
# 设置元素的值
element.set_value("test value")
# 清除元素的值
element.clear()
# 判断元素是否可见
element.is_displayed()
# 判断元素是否可用
element.is_enabled()
# 判断元素是否为选中状态
element.is_selected()
sleep(3)
driver.quit()
二、元素的常用属性
- get_attribute(attributeName):根据传入的属性名称,获取元素属性值
- text():获取元素的text内容
- location():获取元素的坐标(元素左上角的坐标,配合元素的尺寸属性确定元素)
- size:获取尺寸
# 获取元素的属性值
print(element.get_attribute("displayed"))
# 获取元素的text内容
print(element.text)
# 获取元素坐标
print(element.location)
# 获取尺寸
print(element.size)
Attribute源码(部分)
/* * */
package io.appium.uiautomator2.utils;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public enum Attribute {
// checkable判断是否可被勾选,get_attribute("checkable"),返回true/false
CHECKABLE(new String[]{
"checkable"}),
// checked判断是否已被勾选,get_attribute("checked"),返回true/false
CHECKED(new String[]{
"checked"}),
// class/className, get_attribute("class")/get_attribute("className"),返回元素的类名称,例如:android.widget.EditText
CLASS(new String[]{
"class", "className"}),
// clickable判断是否可被点击,get_attribute("clickable"),返回true/false
CLICKABLE(new String[]{
"clickable"}),
// content-desc返回元素该属性的值,get_attribute("content-desc")/get_attribute("contentDescription")
CONTENT_DESC(new String[]{
"content-desc", "contentDescription"}),
// enabled,判断元素是否可用,get_attribute("enabled"),返回true/false
ENABLED(new String[]{
"enabled"}),
// focusable判断当前元素是否可以获取焦点,get_attribute("focusable"),返回true/false
FOCUSABLE(new String[]{
"focusable"}),
// focused判断当前元素是否为焦点,get_attribute("focused"),返回true/false
FOCUSED(new String[]{
"focused"}),
// longClickable判断是否可以长按点击操作,get_attribute("longClickable"),返回true/false
LONG_CLICKABLE(new String[]{
"long-clickable", "longClickable"}),
// package获取包名字符串,get_attribute("package"), 例如:com.jianshu.haruki
PACKAGE(new String[]{
"package"}),
// password判断是否密码框,get_attribute("password"),返回true/false
PASSWORD(new String[]{
"password"}),
// 返回元素resourceId属性的值,get_attribute("resourceId")/get_attribute("resource-id"),例如:com.jianshu.haruki:id/et_account
RESOURCE_ID(new String[]{
"resource-id", "resourceId"}),
// scrollable判断是否可滚动,get_attribute("scrollable"),返回true/false
SCROLLABLE(new String[]{
"scrollable"}),
// 自动选择开始位置
SELECTION_START(new String[]{
"selection-start"}),
// 自动选择结束为止
SELECTION_END(new String[]{
"selection-end"}),
// selected判断元素是否被选中(多数为下拉框),get_attribute("selected"),返回true/false
SELECTED(new String[]{
"selected"}),
// 获取元素的text/name属性值,get_attribute("text")/get_attribute("name")返回字符串,例如:
TEXT(new String[]{
"text", "name"}),
// 获取提示
HINT(new String[]{
"hint"}),
// 获取附加信息
EXTRAS(new String[]{
"extras"}),
// The main difference of this attribute from the preceding one is that
// it does not replace null values with empty strings
// 获取原始文本信息
ORIGINAL_TEXT(new String[]{
"original-text"}, false, false),
// bounds获取元素完整坐标,get_attribute("bounds"),返回坐标,例如:[0,466][1080,562]
BOUNDS(new String[]{
"bounds"}),
// 获取元素的索引,get_attribute("index"),返回元素索引值
INDEX(new String[]{
"index"}, false, true),
// 判断元素是否可见,get_attribute("displayed"),返回true/false
DISPLAYED(new String[]{
"displayed"}),
// 返回内容的长度
CONTENT_SIZE(new String[]{
"contentSize"}, true, false);
...
总结
元素的属性可以通过方法返回,也可以使用get_attribute(“属性名称”)进行获取
边栏推荐
- 亮点抢先看!2022开放原子全球开源峰会定于7月25-29日在北京举办
- 从八皇后问题看回溯法(暴力枚举)
- Hj14 string sorting
- 自定义类型
- UML sequence diagram / sequence diagram / sequence diagram
- The electricity load of many provinces has reached a record high, and efforts have been made to ensure the safety of energy supply in summer
- HJ76 尼科彻斯定理
- MySQL基础篇(函数、约束)
- Vh6501 Learning Series
- Hj17 coordinate movement
猜你喜欢
Automation sequences of canoe simulation functions
Double shutter Technology
PHP渗透测试文件包含漏洞与利用的方法
Google kicked off another "man grabbing war" for core making, and Intel's 17 year veteran joined it
路径寻找问题(状态空间搜索)
Repeaters, hubs, bridges, switches, routers
【漏洞复现】Apache Log4j2 远程代码执行漏洞
推荐5款数据可视化强势工具
3D point cloud course (V) - in depth learning
Adresse de téléchargement Canoe et téléchargement et activation de Can Demo 16, avec toutes les versions logicielles Canoe jointes
随机推荐
在CANoe中通过Panel面板控制Test Module 运行(高级)
How to implement JS template compilation
全网追杀“钱包刺客”
Chapter3 : Fighting COVID-19 with Artificial Intelligence
How to write the docker cleaning cache script
简单枚举/枚举排列
路径寻找问题(暴力求解法)例题分析
HJ13 句子逆序
编程语言之父们退休太无聊,纷纷选择重返职场
GCC getting started manual
程序员老王给自己的孩子取名字
芯片卖到沙子价:雷军的梦想,让这家公司「糟蹋了」
CANoe下載地址以及CAN Demo 16的下載與激活,並附錄所有CANoe軟件版本
HJ18 识别有效的IP地址和掩码并进行分类统计
五.uni-app API[路由与跳转、网络请求、数据缓存]
mybats中xml参数类型的设置
[upload range 17-21] secondary rendering, conditional competition, black and white list bypass
PHP渗透测试文件包含漏洞与利用的方法
Spatial noise reduction and time domain noise reduction
How many months did you write your first SCI?