APP自动化测试总结四:基于坐标的点击、按键操作及处理toast弹窗
一、基于坐标的点击
1 startx = 792 2 endx = 900 3 starty = 49 4 endy = 137 5 center_x = (startx + starty) / 2 6 center_y = (endx + endy) / 2 7 8 # 基于坐标的点击, 9 # 通过元素定位查看元素的坐标边界 10 # 计算中点,得到被点击的的坐标 11 driver.tap([(center_x, center_y)])
二、按键操作的基本封装
import time from appium.webdriver import Remote caps = { "platformName": "Android", "appPackage": "com.lemon.lemonban", "appActivity": ".activity.WelcomeActivity" } driver = Remote(desired_capabilities=caps, command_executor='http://127.0.0.1:4723/wd/hub' ) driver.implicitly_wait(10) # 快速进入指定页面 com.lemon.lemonban/.activity.LoginActActivity # 还可以跨 app 使用 class Keys: ENTER = 66 VOLUME_UP = 24 VOLUME_DOWN = 25 HOME = 3 ## 按下按键 # 音量 + driver.press_keycode(24) # 音量 减 driver.press_keycode(25) # 回车键,确认键 driver.press_keycode(66) # Home driver.press_keycode(3) #利用类属性调用 driver.press_keycode(Keys.ENTER)
三、处理toast弹窗
import time from appium.webdriver import Remote caps = { "platformName": "Android", "appPackage": "com.lemon.lemonban", "appActivity": ".activity.WelcomeActivity" } driver = Remote(desired_capabilities=caps, command_executor='http://127.0.0.1:4723/wd/hub' ) driver.implicitly_wait(10) # 快速进入指定页面 driver.start_activity('com.lemon.lemonban', '.activity.LoginActActivity') driver.find_element('id', 'com.lemon.lemonban:id/et_mobile').send_keys('123') driver.find_element('id', 'com.lemon.lemonban:id/et_password').send_keys('123') driver.find_element('id', 'com.lemon.lemonban:id/btn_login').click() # 获取这个toast弹框 el = driver.find_element('xpath', '//android.widget.Toast') #toast弹窗的xpath是固定的。 print("元素", el) print(el.text) time.sleep(3) driver.quit()