与 Web 元素操作一样(参考 Selenium Web 元素操作),定位到 APP 控件元素后,可以对控件进行一系列的操作,实现与 APP 交互,比如点击、文本输入、元素属性获取等。
class TestEleOper:
def setup_class(self):
'''
完成 capability 设置
初始化 driver
:return:
'''
# 设置 cpability
caps = {
# 设置 app 安装的平台(Android,iOS)
"platformName": "Android",
# 设置 appium 驱动
"appium:automationName": "uiautomator2",
# 设置设备名称
"appium:deviceName": "emulator-5554",
# 设置被测 app 的包名
"appium:appPackage": "io.appium.android.apis",
# 设置被测 app 启动页面的 Activity
"appium:appActivity": ".ApiDemos",
# 不清空缓存信息
"appium:noReset": True,
# 首次启动的时候,不停止app
"appium:dontStopAppOnReset": True,
# 跳过安装,权限设置等操作
"appium:skipDeviceInitialization": True
}
# 初始化 driver
self.driver = webdriver.Remote(
"http://127.0.0.1:4723",
options=UiAutomator2Options().load_capabilities(caps)
)
# 添加隐式等待
self.driver.implicitly_wait(10)
def test_ele_oper(self):
'''
点击滑动条中心位置
:return:
'''
# 点击 Animation
self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "Animation").click()
# 点击 Seeking
self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "Seeking").click()
# 查看【RUN】按钮是否显示/是否可点击
run_btn_ele = self.driver.find_element(AppiumBy.ID, "io.appium.android.apis:id/startButton")
print(f"RUN按钮是否显示 {run_btn_ele.is_displayed()}")
print(f"RUN按钮是否可点击 {run_btn_ele.is_enabled()}")
# 查看【滑动条】是否显示/是否可点击
seek_bar_ele = self.driver.find_element(AppiumBy.ID, "io.appium.android.apis:id/seekBar")
print(f"滑动条是否显示 {seek_bar_ele.is_displayed()}")
print(f"滑动条是否可点击 {seek_bar_ele.is_enabled()}")
# 获取【滑动条】长度
seek_bar_ele_width = seek_bar_ele.size.get('width')
print(f"滑动条长度为 {seek_bar_ele_width}")
# 获取【滑动条】起始位置坐标
seek_bar_ele_start = seek_bar_ele.location
print(f"滑动条起始位置坐标为 {seek_bar_ele_start}")
# 获取【滑动条】中间位置坐标
seek_bar_center_x = seek_bar_ele_start.get("x") + seek_bar_ele_width/2
seek_bar_center_y = seek_bar_ele_start.get("y")
seek_bar_center = (seek_bar_center_x, seek_bar_center_y)
print(f"滑动条中间位置坐标为 {seek_bar_center}")
# 点击【滑动条】中间位置
self.driver.tap([seek_bar_center], 1000)
常见控件的基本交互方式,可以模拟用户与应用程序的交互。在编写测试脚本时,通常需要结合元素的定位方式(如 ID、XPath 等)来找到相应的控件元素。确保测试脚本中的等待和定位策略,以及对元素状态的验证,可以提高测试的稳定性和可靠性。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。