zl程序教程

您现在的位置是:首页 >  后端

当前栏目

浅谈PO模式

模式 浅谈 PO
2023-06-13 09:11:32 时间

大家好,又见面了,我是你们的朋友全栈君。

浅谈PO模式

概述

PO模式是自动化测试的一种常见设计思路,核心思想是通过对界面元素的封装减少冗余代码,同时在后期维护中,若元素定位发生变化, 只需要调整页面元素封装的代码,提高测试用例的可维护性、可读性。换句话说,将每个页面封装成页面类,将页面间通过页面元素进行的操作抽象出来,通过管理这些抽象出来的方法管理用例集。

设计原则

  • The public methods represent the services that the page offers
  • Try not to expose the internals of the page
  • Generally don’t make assertions
  • Methods return other PageObjects
  • Need not represent an entire page
  • Different results for the same action are modelled as different methods (摘自:https://github.com/SeleniumHQ/selenium/wiki/PageObjects)

优点

  1. 业务代码和测试代码分离,降低耦合易于管理
  2. 减少冗余代码

小例子

我们以PO的思路去设计一个测试百度搜索的自动化测试代码

  1. basePage,封装页面操作和页面元素
//test
from selenium import webdriver

class BasePage(object):

    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get(u"http://www.baidu.com")

    def find_element(self, *args):
        return self.driver.find_element(*args)
  1. 封装百度页面的页面元素和操作
from selenium.webdriver.common.by import By
from pages.basePage import BasePage


class BaiduPage(BasePage):
	#元素id
    baidu_text_loc = (By.ID, 'kw')
    baidu_submit_loc = (By.ID, 'su')
	
	#定位搜索框元素
    def get_text_obj(self):
        text_ele = self.find_element(BaiduPage.baidu_text_loc)
        return text_ele
        
	#定位提交索索内容的按钮元素
    def get_sub_obj(self):
        sub_ele = self.find_element(BaiduPage.baidu_submit_loc)
        return sub_ele
        
	#执行搜索操作
    def search(self, key_words):
        self.get_sub_obj().send_keys(key_words)
        self.get_sub_obj().click()
  1. 用例集
from ddt import ddt, data
from pages.baiduPage import BaiduPage

@ddt
class BaiduTest(unittest.TestCase):

    @data('软件测试', '硬件测试')
    def test01(self, seaString):
        BaiduPage().search(seaString)
        time.sleep(5)

if __name__ == '__main__':
    unittest.main()

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/145846.html原文链接:https://javaforall.cn