什么时关键字驱动就不详细介绍了,跟之前有介绍过,详细参考关键字驱动_晒不黑的黑煤球的博客-CSDN博客
一样的用法,只是把方法名称和查找元素的表达式修改一下,读取yaml的代码都不用修改,只需要把对应的驱动对象换成APP的即可,具体如下
yaml文件代码如下:
-
# 点击我的柠檬
action: click
params:
locator: ['xpath','//*[@resource-id="com.lemon.lemonban:id/navigation_my"]']
-
# 点击我的头像
action: click
params:
locator: ['id','com.lemon.lemonban:id/fragment_my_lemon_avatar_title']
-
# 输入用户名
action: type
params:
locator: ['id','com.lemon.lemonban:id/et_mobile']
words: "18173179913"
-
# 输入密码
action: type
params:
locator: ['id','com.lemon.lemonban:id/et_password']
words: "179913"
-
# 点击登录
action: click
params:
locator: ['id','com.lemon.lemonban:id/btn_login']
-
# 断言
action: assert_error_tips_equal
params:
locator: ['id','com.lemon.lemonban:id/fragment_my_lemon_avatar_title']
expected: "雨泽"
native_page.py代码如下:
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as when
class MobileKey:
ENTER = 66
VOLUME_UP = 24
VOLUME_DOWN = 25
BACK = 4
class NativePage:
def __init__(self, driver):
self.driver = driver
def find_el(self, locator):
"""查找元素"""
return self.driver.find_element(*locator)
def click(self, locator):
"""点击"""
wait = WebDriverWait(self.driver, timeout=5)
condition = when.element_to_be_clickable(locator)
element = wait.until(condition)
ActionChains(self.driver).click(element).perform()
def type(self, locator, words):
"""输入"""
el = self.driver.find_element(*locator)
el.send_keys(words)
def get_toast_text(self):
"""获取错误信息"""
toast = self.driver.find_element('xpath', '//android.widget.Toast')
return toast.text
def get_avatar_title(self):
"""获取登录后的头像名称"""
title = self.driver.find_element('id', 'com.lemon.lemonban:id/fragment_my_lemon_avatar_title')
return title.text
def assert_error_tips_equal(self, locator, expected):
"""断言某个错误信息等于"""
el = self.driver.find_element(*locator)
assert el.text == expected
def assert_avatar_title_equal(self, locator, expected):
"""断言登录后头像等于"""
el = self.driver.find_element(*locator)
assert el.text == expected
def width(self):
"""获取app的宽度"""
return self.driver.get_window_size()['width']
def height(self):
"""获取app的高度"""
return self.driver.get_window_size()['height']
def swipe_left(self):
"""从右往左滑动"""
self.driver.swipe(start_x=self.width() * 0.9, end_x=self.width() * 0.1,
start_y=self.height() * 0.5, end_y=self.height() * 0.5)
def swipe_right(self):
"""从左往右滑动"""
self.driver.swipe(start_x=self.width() * 0.1, end_x=self.width() * 0.9,
start_y=self.height() * 0.5, end_y=self.height() * 0.5)
def swipe_up(self):
"""从下往上滑动"""
self.driver.swipe(start_x=self.width() * 0.5, end_x=self.width() * 0.5,
start_y=self.height() * 0.9, end_y=self.height() * 0.1)
def swipe_down(self):
"""从上往下滑动"""
self.driver.swipe(start_x=self.width() * 0.5, end_x=self.width() * 0.5,
start_y=self.height() * 0.1, end_y=self.height() * 0.9)
def enter(self):
"""回车"""
self.driver.press_keycode(MobileKey.ENTER)
def volume_up(self):
"""音量加"""
self.driver.press_keycode(MobileKey.VOLUME_UP)
def volume_down(self):
"""音量减"""
self.driver.press_keycode(MobileKey.VOLUME_DOWN)
def back(self):
"""返回"""
self.driver.press_keycode(MobileKey.BACK)
测试用例代码如下:
import yaml
from pages.native_page import NativePage
def test_login_keyword(app):
"""关键字驱动的测试函数"""
# 1.读取yaml文件的内容,包含app页面操作和对应的测试数据
# 2.需要一个页面对象NativePage,调用nativepage手机页面操作
with open('login_keywords', encoding='utf-8') as f:
stpes = yaml.safe_load(f)
nativepage = NativePage(app)
for step in stpes:
method_name = step['action']
method_params = step['params']
# 通过方法名称获取方法
method = getattr(nativepage, method_name)
# 因为得到的params是字典,所以需要解包
method(**method_params)
运行结果:
这篇博客介绍了如何运用关键字驱动的方法进行App自动化测试。通过yaml文件定义测试步骤,结合NativePage类实现点击、输入等操作。测试用例中加载yaml文件内容,调用页面对象的方法执行相应的操作,如点击、输入、断言等,实现对App的自动化测试流程。

1807

被折叠的 条评论
为什么被折叠?



