Python自動化測試~PO模型封裝

專注的阿熊發表於2021-07-19

1 :實現 BasePage

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains # 滑鼠操作

class BasePage():

     '''

     BasePage 封裝所有介面都公用的方法。

     例如 driver,find_element

     '''

     # 例項化 BasePage 類時,事先執行的 __init__ 方法,該方法需要傳遞引數

     def __init__(self,driver,url):

         self.driver = driver

         self.base_url = url

     # 進入網址

     def get(self):

         self.driver.get(self.base_url)

     # 元素定位 , 替代八大定位

     def get_element(self,*locator):

         return self.driver.find_element(*locator)

     # 點選

     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 :實現 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)

     # 進入百度

     def open_baidu(self):

         self.get()

     # 輸入資料

     def input_search_content(self,text):

         self.send_text(text,By.ID,"kw")

     # 點選按鈕

     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 :實現 外匯跟單gendan5.com 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="

         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()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2782110/,如需轉載,請註明出處,否則將追究法律責任。

相關文章