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(“属性名称”)进行获取