zoukankan      html  css  js  c++  java
  • page Object使用

    使用 page object 模式的好处:

    • 创建可跨多个测试用例共享的可重用代码
    • 减少重复代码的数量
    • 如果用户界面发生变化,修复只需要在一个地方进行更改

    官网示例代码

    PythonOrgSearch调用MainPage(main_page = page.MainPage(self.driver))将driver传到class  BasePage(object)并初始化。
    接着判断python.org是否被正常加载(assert main_page.is_title_matches(), "python.org title doesn't match.")
    接着调用(main_page.search_text_element = "pycon")传入搜索关键字‘pycon’。并调用(class SearchTextElement(BasePageElement):)传入搜索的id,进行搜索
    点击搜搜按钮,断言搜索结果是否为:No results found.
    PythonOrgSearch.py
    import unittest
    from selenium import webdriver
    import page
    
    class PythonOrgSearch(unittest.TestCase):
        """A sample test class to show how page object works"""
    
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.driver.get("http://www.python.org")
    
        def test_search_in_python_org(self):
            """
            测试python.org的搜索功能,搜索关键字“pycon”,然后验证结果是否显示。
           请注意,它不会在搜索结果页中查找任何特定文本。此测试验证结果不是空的。
            """
    
            #加载主页面
            main_page = page.MainPage(self.driver)
            #检查"Python"是否在title中
            assert main_page.is_title_matches(), "python.org title doesn't match."
            #设置搜索的文本"pycon"
            main_page.search_text_element = "pycon"
            main_page.click_go_button()
            search_results_page = page.SearchResultsPage(self.driver)
            #验证结果不为空
            assert search_results_page.is_results_found(), "No results found."
    
        def tearDown(self):
            self.driver.close()
    
    if __name__ == "__main__":
        unittest.main(verbosity=2)

    page.py

    from element import BasePageElement
    from locators import MainPageLocators
    
    class SearchTextElement(BasePageElement):
        """这个类是为了获得来自特定定位器的文本"""
        #这个是搜索框的定位器
        locator = 'q'
    
    
    class BasePage(object):
        """Base class to initialize the base page that will be called from all pages"""
    
        def __init__(self, driver):
            self.driver = driver
    
    
    class MainPage(BasePage):
        """Home page action methods come here. I.e. Python.org"""
    
        #声明一个变量,将包含检测到的文本
        search_text_element = SearchTextElement()
    
        def is_title_matches(self):
            """验证Python是否在页面标题中显示"""
            return "Python" in self.driver.title
    
        def click_go_button(self):
            """点击搜索按钮"""
            element = self.driver.find_element(*MainPageLocators.GO_BUTTON)
            element.click()
    
    
    class SearchResultsPage(BasePage):
        """搜索结果页面"""
    
        def is_results_found(self):
            # 也许会在特定的页面展示元素不存在‘No results found.’
            return "No results found." not in self.driver.page_source

    element.py

    from selenium.webdriver.support.ui import WebDriverWait
    
    
    class BasePageElement(object):
        """Base page class 是为了初始化每个页面的对象class."""
    
        def __set__(self, obj, value):
            """将文本设置为提供的值  'pycon'  并且,self.locator='q' 由调用者传递"""
            driver = obj.driver
            WebDriverWait(driver, 100).until(
                lambda driver: driver.find_element_by_name(self.locator))
            driver.find_element_by_name(self.locator).clear()
            driver.find_element_by_name(self.locator).send_keys(value)
    
        def __get__(self, obj, owner):
            """获取指定对象的值"""
            # self.locator=‘q’
            driver = obj.driver
            WebDriverWait(driver, 100).until(
                lambda driver: driver.find_element_by_name(self.locator))
            element = driver.find_element_by_name(self.locator)
            return element.get_attribute("value")

    locator.py

    from selenium.webdriver.common.by import By
    
    class MainPageLocators(object):
        """A class for main page locators. All main page locators should come here"""
        GO_BUTTON = (By.ID, 'submit')
    
    class SearchResultsPageLocators(object):
        """A class for search results locators. All search results locators should come here"""
        pass
  • 相关阅读:
    codeforces C. Fixing Typos 解题报告
    codeforces B. The Fibonacci Segment 解题报告
    codeforces B. Color the Fence 解题报告
    codeforces B. Petya and Staircases 解题报告
    codeforces A. Sereja and Bottles 解题报告
    codeforces B. Levko and Permutation 解题报告
    codeforces B.Fence 解题报告
    tmp
    API 设计 POSIX File API
    分布式跟踪的一个流行标准是OpenTracing API,该标准的一个流行实现是Jaeger项目。
  • 原文地址:https://www.cnblogs.com/ychun/p/14382012.html
Copyright © 2011-2022 走看看