Modular drive testing, is to learn from the programming language of the modular thinking, the repeated operation of the independent success of the announcement module, understand the use of this module during the execution of the case is called when the operation is required, which can greatly eliminate duplication and improve the maintainability of test cases
The following specific 126 mailbox as an example:
First of all to use the login log out independently, put in the public module
public.py
# Coding=utf-8class Login (): # Login Login def login (self,driver): driver.find_element_by_id ("Idinput"). Clear () driver.find_element_by_id ("Idinput"). Send_keys ("username") driver.find_element_by_id ("Pwdinput"). Clear () driver.find_element_by_id ("Pwdinput"). Send_keys ("123456") driver.find_element_by_id ("Loginbtn"). Click () # Click to confirm Login Button # logout logout def logout (self,driver): time.sleep (2) driver.find_element_by_link_text (U "Exit") . Click ()
Separate the function into a separate script, mainly to increase the browser-driven parameters for the function, because the implementation of the function requires a browser-driven driver,driver needs to be given by a specific invocation of the use case.
test_case.py,test_case.py by invoking Login, Exit function for modularity
#coding =utf-8from Selenium import webdriverfrom public import Loginimport unittestclass logintest (unittest. TestCase): def setUp (self): self.driver = Webdriver. Firefox () self.base_url = "http://www.126.com" self.driver.implicitly_wait (5) def test_write_letter_ Case (self): driver = self.driver driver.get (self.base_url) #调用登录函数 login (). Login (Driver) # Realize receiving, Operation # ... #调用退出函数 Login (). Logout (Driver) def tearDown (self): self.driver.quit () if __name__ = = "__main__": Unittest.main ()
First, you need to import login () from the public.py file in the current directory, call the login () and loginout () functions in the class where needed, so it's much easier to write and maintain for each use case
Selenium+python (Modular Drive test)