UI 自动化测试框架设计与 PageObject 改造( 二 )

Main 模块是首页的 PageObject ,其中的方法封装了首页的重要功能,比如下面代码中的 goto_search_page 封装了点击搜索并跳转到 Search 页:
from appium.webdriver.common.mobileby import MobileByfrom selenium.webdriver.common.by import Byfrom test_appium.page.base_page import BasePagefrom test_appium.page.profile import Profilefrom test_appium.page.search import Searchclass Main(BasePage):    #点击搜索按钮后,进入搜索页    def goto_search_page(self):        self.find(MobileBy.ID, "tv_search").click()        #进入搜索页        return Search(self._driver)    def goto_stocks(self):        pass    def goto_trade(self):        pass    def goto_messages(self):        passSearch 模块可以搜索一支股票,还可以获取股票的价格,比如下图:

UI 自动化测试框架设计与 PageObject 改造

文章插图
 
封装代码如下:
from appium.webdriver.common.mobileby import MobileByfrom selenium.webdriver.remote.webdriver import WebDriverclass Search:    _driver: WebDriver    def __init__(self, driver):        self._driver = driver    #输入要搜索的内容    def search(self, key: str):        self._driver.find_element(MobileBy.ID, "search_input_text").send_keys(key)        self._driver.find_element(MobileBy.ID, "name").click()        return self    #获取股票价格,用于判断    def get_price(self, key: str) -> float:        return float(self._driver.find_element(MobileBy.ID, "current_price").text)最后对上述代码建立测试,新建测试模块 test_search :
import pytestfrom test_appium.page.app import Appclass TestSearch:    def setup(self):        self.main = App().start().main()    def test_search(self):        assert self.main.goto_search_page().search("alibaba").get_price("BABA") > 200以上,供大家参考,欢迎一起留言探讨 。
(文章来源于霍格沃兹测试学院)

【UI 自动化测试框架设计与 PageObject 改造】


推荐阅读