TouchAction主要用来模拟手机页面的触摸点击、滚动、滑动等操作,详情查看官网文档:官方文档
代码示例:
from time import sleep
import pytest as pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
class TestTouch:
def setup(self):
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
}
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置隐式等待
self.driver.implicitly_wait(3)
def teardown(self):
self.driver.quit()
def test_touch(self):
action = TouchAction(self.driver)
# 获取屏幕的宽
window_rect = self.driver.get_window_rect()
width = window_rect['width']
height = window_rect['height']
# 使用中心点作为x坐标
x1 = int(width/2)
# 设置y坐标的起始点
y_start = int(height * 9/10)
y_end = int(height * 1/10)
action.press(x=x1, y=y_start).wait(200).move_to(x=x1, y=y_end).release().perform()
sleep(5)
if __name__ == 'main':
pytest.main()
toast是简易的消息提示框,toast特性:
toast控件类名称:android.widget.Toast
代码示例:
from time import sleep
import pytest as pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
class TestTouch:
def setup(self):
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
}
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置隐式等待
self.driver.implicitly_wait(3)
def teardown(self):
self.driver.quit()
def test_toast(self):
self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
'new UiSelector().resourceId("com.jianshu.haruki:id/iv_mine")').click()
self.driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("点击登录")').click()
self.driver.find_element(AppiumBy.ID, 'com.jianshu.haruki:id/et_account').send_keys('13000000000')
self.driver.find_element(AppiumBy.ID, 'com.jianshu.haruki:id/et_verification_code_or_password').send_keys('112345')
self.driver.find_element(AppiumBy.ID, 'com.jianshu.haruki:id/tv_user_cb').click()
self.driver.find_element(AppiumBy.ID, 'com.jianshu.haruki:id/tv_login').click()
# 打印当前页面的dom树
print(self.driver.page_source)
# 通过classname进行定位
print(self.driver.find_element(AppiumBy.XPATH, "//*[@class='android.widget.Toast']").text)
# 通过text进行定位
print(self.driver.find_element(AppiumBy.XPATH, "//*[contains(@text, '验证码无效')]").text)
sleep(3)
if __name__ == 'main':
pytest.main()