PO模式:Page Object,PO模式是自动化测试项目开发实践的最佳设计模式之一。
核心思想:通过对界面元素的封装减少冗余代码,同时在后期维护中,若元素位置发生变化,只需要调整页面封装的代码,提高测试用例的可维护性、可读性。
优点:
缺点:
案例一:自动发送短信
方法:Appium+PO模式+Pytest框架数据参数化
模块分布
base_action.py:
from selenium.webdriver.support.wait import WebDriverWait
class BaseAction:
def __init__(self, driver):
self.driver = driver
def find_element(self, location, timeout=10, poll=1):
"""
:param location: 元素位置
:param timeout: 设置10秒
:param poll: 多少秒找一次
:return:
"""
location_by, location_value = location
wait = WebDriverWait(self.driver, timeout, poll)
return wait.until(lambda x: x.find_element(location_by, location_value))
def find_elements(self, location, timeout=10, poll=1):
location_by, location_value = location
wait = WebDriverWait(self.driver, timeout, poll)
return wait.until(lambda x: x.find_elements(location_by, location_value))
def click(self, location):
self.find_element(location).click()
def input(self, location, text):
self.find_element(location).send_keys(text)
base_driver.py
from appium import webdriver
def init_driver():
desired_caps = dict()
# 设备信息
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "5.1"
desired_caps["deviceName"] = "192.168.56.101:5555"
# app信息
desired_caps["appPackage"] = "com.android.mms"
desired_caps["appActivity"] = ".ui.ConversationList"
return webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
message_list_page.py
from selenium.webdriver.common.by import By
from base.base_action import BaseAction
class MessageListPage(BaseAction):
# 新建短信按钮
new_message_button = By.ID, "com.android.mms:id/action_compose_new"
def click_new_message(self):
self.click(self.new_message_button)
new_message_page.py
from selenium.webdriver.common.by import By
from base.base_action import BaseAction
class NewMessagePage(BaseAction):
# 接受者特征
recipients_edit_text = By.ID, "com.android.mms:id/recipients_editor"
# 内容特征
content_edit_text = By.ID, "com.android.mms:id/embedded_text_editor"
# 发送按钮
send_button = By.XPATH, "//*[@content-desc='发送']"
def input_recipients(self, text):
self.input(self.recipients_edit_text, text)
def input_content(self, text):
self.input(self.content_edit_text, text)
def click_send(self):
self.click(self.send_button)
page.py
from page.message_list_page import MessageListPage
from page.new_message_page import NewMessagePage
class Page:
def __init__(self, driver):
self.driver = driver
@property
def message_list(self):
return MessageListPage(self.driver)
@property
def new_message(self):
return NewMessagePage(self.driver)
test_message.py
import time
import pytest
from base.base_driver import init_driver
from page.page import Page
class TestMessage:
def setup(self):
self.driver = init_driver()
self.page = Page(self.driver)
def teardown(self):
time.sleep(3)
self.driver.quit()
@pytest.mark.parametrize(('phone', 'content'), [('18588888888', "HELLO"),('18577778888', "您好!")])
def test_send_message(self, phone, content):
# 主页-点击短信,新建短信
self.page.message_list.click_new_message()
# 新建短信-输入 接收人
self.page.new_message.input_recipients(phone)
# 新建短信-输入 内容
self.page.new_message.input_content(content)
# 新建短信-点击发送
self.page.new_message.click_send()
if __name__ == '__main__':
pytest.main([])
pytest.ini
[pytest]
# 添加命令行参数
addopts = -vs --html=report/report.html --reruns 0
# 文件搜索路径
testpaths = ./scripts
# 文件名称
python_files = test_*.py
# 类名称
python_classes = Test*
# 方法名称
python_functions = test_*
Readme
短信案例
需求:
在《短信》应用中,进入发送短信页面,在姓名和内容栏中,输入对应的数据,并点击发送。
包名界面名:com.android.mms/.ui.ConversationList
发送短信页面标识:resource-id,com.android.mms:id/action_compose_new
接收者标识:resource-id,com.android.mms:id/recipients_editor
内容标识:resource-id,com.android.mms:id/embedded_text_editor
发送按钮标识:content-desc,发送
数据:
[18588888888, HELLO]
[18577778888, 您好!]
运行test_message.py