前言
PO模式:Page Object,PO模式是自动化测试项目开发实践的最佳设计模式之一。
核心思想:通过对界面元素的封装减少冗余代码,同时在后期维护中,若元素位置发生变化,只需要调整页面封装的代码,提高测试用例的可维护性、可读性。
优点:
减少了冗余代码;
业务代码和测试代码被分开,降低耦合性;
维护成本低;
缺点:
结构复杂:基于流程做了模块化的拆分
例子:自动发送短信
方法:Appium+PO模式+Pytest框架数据参数化
base模块:前置代码和基本操作,base_driver.py对应打开driver,base_action.py对应元素定位、点击按钮和输入。
page模块:对应操作页面,考虑手指测试的过程需要用到多少个页面,就在page模块中创建多少个文件。page.py统一入口,有多少个页面,就写多少个函数,并创建对应的对象。
scripts模块:测试脚本。
pytest.ini:配置文件。
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_*
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取