在app手工测试过程中,我们常用的操作包含有哪些呢?——滑动、输入、点击、拖动、放大缩小等,那么这些操作转化为代码,应该如何实现呢,下面简单介绍一下,方便复习。
一、swipe滑动操作
我们对页面进行滑动,但由于屏幕大小不以,无法保证都能滑动成功,所以无法直接写死一个尺寸大小,必须使用百分比坐标,去进行滑动界面操作。
1、swipe封装
# swipe函数封装
# 从右往左滑动
def swipe_left(driver,offset = 0.9):
size = driver.get_window_size() # 获取屏幕大小
height = size['height'] # 获取高度
width = size['width'] # 获取宽度
driver.swipe(start_x = width * offset,start_y = height * 0.5,
end_x = width * (1-offset),end_y = height * 0.5)
# 从左往右滑动
def swipe_right(driver,offset = 0.9):
size = driver.get_window_size()
height = size['height']
width = size['width']
driver.swipe(start_x = width * (1-offset),start_y = height * 0.5,
end_x = width * offset,end_y = height * 0.5)
# 往上滑
def swipe_up(driver,offset = 0.9):
size = driver.get_window_size()
height = size['height']
width = size['width']
driver.swipe(start_x = width * 0.5,start_y = height * offset,
end_x = width * 0.5,end_y = height * (1-offset))
# 往下滑
def swipe_down(driver,offset = 0.9):
size = driver.get_window_size()
height = size['height']
width = size['width']
driver.swipe(start_x = width * 0.5,start_y = height * (1-offset),
end_x = width * 0.5,end_y = height * offset)
if __name__ == '__main__':
from appium.webdriver import Remote
import time
caps = {'platformName': 'Android', 'deviceName': 'emulator-5554',
'appPackage': 'com.xxzb.fenwoo', 'appActivity':'com.xxzb.fenwoo.activity.addition.WelcomeActivity'}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities=caps)
driver.implicitly_wait(3) # 等待
time.sleep(5)
# 调用函数
swipe_left(driver)
2、swipe封装到Basepage
class BasePage:
def __init__(self,driver):
self.driver = driver
self.size = driver.get_window_size()
self.height = self.size['height']
self.width = self.size['width']
def swipe_left(self,offset = 0.9):
self.driver.swipe(start_x = self.width * offset, start_y = self.height * 0.5,
end_x = self.width * (1 - offset), end_y = self.height * 0.5)
# 从左往右滑动
def swipe_right(self, driver, offset=0.9):
self.driver.swipe(start_x=self.width * (1 - offset), start_y=self.height * 0.5,
end_x=self.width * offset, end_y=self.height * 0.5)
# 往上滑
def swipe_up(self,driver,offset=0.9):
self.driver.swipe(start_x=self.width * 0.5, start_y=self.height * offset,
end_x=self.width * 0.5, end_y=self.height * (1 - offset))
# 往下滑
def swipe_down(self,driver, offset=0.9):
self.driver.swipe(start_x=self.width * 0.5,