Python自動(dòng)化測試PO模型封裝過程詳解
在自動(dòng)化中, Selenium 自動(dòng)化測試中有一個(gè)名字經(jīng)常被提及 PageObject( 思想與面向?qū)ο蟮奶卣飨?同 ) ,通常 PO 模型可以大大提高測試用例的維護(hù)效率
優(yōu)點(diǎn):可重用,業(yè)務(wù)和對象分離,代碼結(jié)構(gòu)清晰,方便代碼維護(hù)
核心要素
1. 在 PO 模式中抽離封裝集成一個(gè)BasePage 類,該基類應(yīng)該擁有一個(gè)只實(shí)現(xiàn) webdriver 實(shí)例的屬性
2. 每一個(gè) page 都繼承 BasePage ,通過 driver 來管理本 page 中元素,將 page 中的操作封裝成一個(gè)個(gè)方法
3.TestCase 繼承 unittest.Testcase 類,并依賴 page 類,從而實(shí)現(xiàn)相應(yīng)的測試步驟
PO 實(shí)現(xiàn)進(jìn)入百度頁面輸入數(shù)據(jù)后進(jìn)入下一個(gè)頁面
組織代碼
1 :實(shí)現(xiàn) BasePage
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains #鼠標(biāo)操作 class BasePage(): ''' BasePage封裝所有界面都公用的方法。 例如driver,find_element等 ''' # 實(shí)例化BasePage類時(shí),事先執(zhí)行的__init__方法,該方法需要傳遞參數(shù) def __init__(self,driver,url): self.driver = driver self.base_url = url # 進(jìn)入網(wǎng)址 def get(self): self.driver.get(self.base_url) #元素定位,替代八大定位 def get_element(self,*locator): return self.driver.find_element(*locator) #點(diǎn)擊 def left_click(self,*locator): ActionChains(self.driver).click(self.get_element(*locator)).perform() #輸入 def send_text(self,text,*locator): self.driver.find_element(*locator).send_keys(text) #清除 def clear_text(self, *locator): self.driver.find_element(*locator).clear() # 表單切換 def switch_iframe(self,*locator): self.driver.switch_to.frame(self.driver.find_element(*locator)) #窗口切換 def switch_window(self,n): self.driver.switch_to.window(self.driver.window_handles[n])
2 :實(shí)現(xiàn) SearchPage
from selenium.webdriver.common.by import By from base.base_page import BasePage class SearchOne(BasePage): def __init__(self,driver,url): BasePage.__init__(self,driver,url) #進(jìn)入百度 def open_baidu(self): self.get() #輸入數(shù)據(jù) def input_search_content(self,text): self.send_text(text,By.ID,"kw") # 點(diǎn)擊按鈕 def click_baidu_search(self): self.left_click(By.ID, "su") def click_open_hao(self): self.left_click(By.XPATH,".//*[@id='1']/h3/a[1]")
3 :實(shí)現(xiàn) TestCase
import unittest from selenium import webdriver from page.page_one import SearchOne from page.page_two import SearchTwo class BaiBu(unittest.TestCase): @classmethod def setUpClass(cls) -> None: cls.driver = webdriver.Firefox() cls.driver.implicitly_wait(10) def test001(self): url="http://www.baidu.com" s = SearchOne(self.driver,url) s.open_baidu() s.input_search_content("123") s.click_baidu_search() s.click_open_hao() self.driver.switch_to.window(self.driver.window_handles[1]) def test002(self): s=SearchTwo(self.driver,"") s.open_baidu_map() def tearDown(self) -> None: # self.driver.quit() pass if __name__ == '__main__': unittest.main()
PO 模式的優(yōu)點(diǎn)
1:PO 提供了一種業(yè)務(wù)流程與頁面元素操作分離的模式,這使得測試代碼變得更加清晰
2 :頁面對象與用例分離,使得我們更好的復(fù)用對象
3 :可復(fù)用的頁面方法代碼會變得更加優(yōu)化
4 :更加有效的命令方式使得我們更加清晰的知道方法所操作的 UI 元素
以上就是Python自動(dòng)化測試PO模型封裝的詳細(xì)內(nèi)容,更多關(guān)于Python自動(dòng)化測試PO模型的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Django獲取該數(shù)據(jù)的上一條和下一條方法
今天小編就為大家分享一篇Django獲取該數(shù)據(jù)的上一條和下一條方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Ubuntu中安裝指定Python版本方法詳解(理論上各版本通用)
現(xiàn)在基于linux的發(fā)行版本有很多,有centos,ubuntu等,一般基于linux的衍生系統(tǒng)至少都安裝了Python2版本,但是現(xiàn)在Python已經(jīng)是3.x版本大行其道了,這篇文章主要給大家介紹了關(guān)于Ubuntu中安裝指定Python版本方法的相關(guān)資料,理論上各版本通用,需要的朋友可以參考下2023-06-06CentOS 6.X系統(tǒng)下升級Python2.6到Python2.7 的方法
今天到新公司發(fā)現(xiàn)用的CentOS 6.X系統(tǒng),默認(rèn)安裝的Python是2.6版本,可是我的程序引用的部分庫需要2.7版本或以上,所以只能升級Python到2.7版本了,現(xiàn)在將升級的步驟分享給大家,有需要的朋友們可以參考借鑒。2016-10-10python目標(biāo)檢測yolo3詳解預(yù)測及代碼復(fù)現(xiàn)
這篇文章主要為大家介紹了python目標(biāo)檢測yolo3詳解預(yù)測及代碼復(fù)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05