自動化測試模型——模塊化與參數化

含義

在創建函數或方法時設置參數,使之可根據不同的參數執行相應操作。

實例

創建一個網易郵箱的測試腳本test_mail.py:

	from time import sleep
	from selenium import webdriver
	
	dri = webdriver.Chrome()
	dri.get("http://mail.163.com")
	
	# 登錄
	sleep(2)
	dri.find_element_by_id("switchAccountLogin").click()
	login_frame = dri.find_element_by_css_selector(
	    'iframe[id^="x-URS-iframe"]')  # 定位嵌套表單
	dri.switch_to.frame(login_frame)  # 切換表單
	# dri.find_element_by_name("email").clear()
	dri.find_element_by_name("email").send_keys("username")
	# dri.find_element_by_name("password").clear()
	dri.find_element_by_name("password").send_keys("password")
	dri.find_element_by_id("dologin").click()
	sleep(5)
	
	# 退出
	dri.find_elements_by_link_text("退出").click()
	
	dri.quit()

假設要實現一個郵箱的自動化測試項目,每條用例都需要有登錄和退出操作,可創建一個新的module.py來存放登錄和退出操作。

	class Mail:
	
	    def __init__(self, driver):
	        self.driver = driver
	        
	    def login(self):
	        self.driver.find_element_by_id("switchAccountLogin").click()
	        login_frame = self.driver.find_element_by_css_selector(
	            'iframe[id^="x-URS-iframe"]')  # 定位嵌套表單
	        self.driver.switch_to.frame(login_frame)  # 切換表單
	        self.driver.find_element_by_name("email").send_keys("username")
	        self.driver.find_element_by_name("password").send_keys("password")
	        self.driver.find_element_by_id("dologin").click()
	        
	    def logout(self):
	        self.driver.find_element_by_link_text("退出").click()

修改test_mail.py,使調用Mail類的方法。

	mail = Mail(dri)
	mail.login()
	mail.logout()

與未修改前功能一致。
若需求是測試登錄功能,可將測試數據(賬號密碼)參數化,調用Mail的方法即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章