七安 七安
  • 首页
  • 七安
  • Python
  • Tools
  • 导航
  • 9
PageObject

PO

七安
2024-01-12 22:31:47

po模式好处

Page Object(页面对象)模式,是Selenium实战中最为流行,并且被做自动化测试所熟悉和推崇的一种设计模式之一。在设计测试时,把页面元素定位和元素操作方法按照页面抽象出来,分离成一定的对象,然后再进行组织。

Page Object是一种面向对象的设计模式,它重新定义了一些内容。如下:网页=类别;页面上的各种元素=变量;用户互动=方法。

由于测试代码和页面代码是分开的。因此,Page Object可以抵制微小的调调整,有助于我们构建更稳固的代码框架。

可靠且易于维护。

脚本是可读的,且代码是可重用的,还可以完全消除重复部分。

可以解决

  • 元素定位改变带来的维护成本的增加
  • 元素定位与用例分离
  • 代码冗余

page类函数封装

第一种

class PageBaidu:
    button = (By.ID, 'su')
    input = (By.ID, 'kw')

class TestBaidu:
    def test_baidu1(self, driver):
        page = PageBaidu()
        driver.get("https://www.baidu.com/")
        title = driver.title
        url = driver.current_url
        text = driver.find_element(*page.news).text
        button_text = driver.find_element(*page.button).accessible_name
        assert title == "百度一下,你就知道"
        assert url == "https://www.baidu.com/"
        assert text == "新闻"
        assert button_text == "百度一下"

第二种

class PageBaidu:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get("https://www.baidu.com")
    button = (By.ID, 'su')
    input = (By.ID, 'kw')
    def search_keyword(self, keyword):
        self.driver.find_element(*self.input).send_keys(keyword)
        self.driver.find_element(*self.button).click()

class TestBaidu:
    def test_baidu3(self):
        page = PageBaidu()
        page.search_keyword("UI自动化")

page类继承

class BasePage:
    def __init__(self, driver):
        self.driver = driver
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.driver.get("https://www.baidu.com")

class PageBaidu(BasePage): 
    button = (By.ID, 'su')
    input = (By.ID, 'kw')

    def search_keyword(self, keyword):
        self.driver.find_element(*self.input).send_keys(keyword)
        self.driver.find_element(*self.button).click()

class TestBaidu:
    def test_baidu1(self, driver):
        page = PageBaidu(driver)
        page.search_keyword("UI自动化")

数据驱动

class TestBaidu:
    @pytest.mark.parametrize("key", read_yaml()['skill'])
    def test_baidu1(self, driver, key):
        page = PageBaidu(driver)
        page.search_keyword(key)

find_element二次封装

作者设置了回复可见
立即评论,阅读全文

send_keys封装

作者设置了回复可见
立即评论,阅读全文

click封装

作者设置了回复可见
立即评论,阅读全文

操作浏览器封装

作者设置了回复可见
立即评论,阅读全文

切换窗口封装

作者设置了回复可见
立即评论,阅读全文

title、url、page_source封装

作者设置了回复可见
立即评论,阅读全文

获取文本封装

作者设置了回复可见
立即评论,阅读全文

鼠标操作封装

作者设置了回复可见
立即评论,阅读全文

Select封装

作者设置了回复可见
立即评论,阅读全文

其他功能封装

作者设置了回复可见
立即评论,阅读全文
PageObject
9
Allure
上一篇
logging
下一篇

聚合文章

PlayWright
Fluent
Pyside6
Anaconda
logging
PO
Allure

文章目录

猜你喜欢

PlayWright

七安
384 5

Fluent

七安
1,677 1

Pyside6

七安
1,569 2

Anaconda

七安
1,424 4
9

关于

采用知识共享署名 4.0 国际许可协议进行许可。
This work is licensed under a Creative Commons Attribution 4.0 International License.
资源均来自网络整理收集,仅供学习和研究使用,如有侵权请联系我们删除。

七安

网站地图 sitemap

七安

网站地图 sitemap
Copyright © 2018-2025 七安. . 京ICP备18054419号-6 鲁公网安备 37048102005039号
  • 首页
  • 七安
  • Python
  • Tools
  • 导航

搜索

  • thai
  • s3501
  • 百年好合
  • AdSense
  • ITX
  • 蹦蹦炸弹
  • Selenium
  • Pytest
  • allure
  • PageObject

七安

20
文章
0
评论
421
喜欢