Today's small series to introduce is the simplest and most commonly used method of data driven, because it is only the introduction of methods, code after the operation of the aesthetic degree is slightly flawed, mind can change their own
Or 163 mailbox Login as an example:
Design a class that holds the data, the parameters of which are the data we need to modify, and then the data that is passed out of the class
classData (object):def __init__(self,username="', userpsw="'): Self.username=userName SELF.USERPSW=USERPSWU1=data (username="xxx1", userpsw="xxx1") U2=data (username="xxx2", userpsw="xxx2")
Design the login method, this method has been introduced in the previous small series, this time only a minor change in the code
#Add a parameter to the login method to accept the user's login informationdefLogin (user):#positioningName = driver.find_element_by_id ("Op_email3_username") Password= Driver.find_element_by_class_name ("Op_email3_password")
#empty, clear the input box with clear, because the SendKeys method is to add content in the input box, if the input box already has content, it will add content based on the original contentname.clear () password.clear ()
" "to invoke the information stored by the user" "Name.send_keys (user.username) Password.send_keys (USER.USERPSW)#LoginLogin = Driver.find_element_by_css_selector (". C-btn") Login.click ()#switch back to the main windowDriver.switch_to.window (Driver.current_window_handle)
Finally, the login method is called to implement the data-driven automated test run.
Login (u1) login (U2)
By using data-driven in code, you can decouple data and methods, and only change user information (U1=data (username="xxx1") when adding and modifying data , userpsw="xxx1")) part, and the last login call, can increase the stability and extensibility of the test script.
source, please correct me:
fromSeleniumImportWebdriverImporttime,unittest#Open BaiduDriver =Webdriver. Firefox () driver.implicitly_wait (5) Driver.get ("https://www.baidu.com/") driver.find_element_by_id ("kw"). Send_keys ("163 Email Login") driver.find_element_by_id ("su"). Click ()classData (object):def __init__(self,username="', userpsw="'): Self.username=userName SELF.USERPSW=USERPSWU1=data (username="13671086620", userpsw="wsz768950") U2=data (username="XXX", userpsw="XXX")#Add a parameter to the login method to accept the user's login informationdefLogin (user):#positioningName = driver.find_element_by_id ("Op_email3_username") Password= Driver.find_element_by_class_name ("Op_email3_password") #empty, clear the input box with clear, because the SendKeys method is to add content in the input box, if the input box already has content, it will add content based on the original contentname.clear () password.clear ( )" "to invoke the information stored by the user" "Name.send_keys (user.username) Password.send_keys (USER.USERPSW)#LoginLogin = Driver.find_element_by_css_selector (". C-btn") Login.click ()#switch back to the main windowDriver.switch_to.window (driver.current_window_handle) login (U1) time.sleep (5) Login (U2) Time.sleep (10) Driver.quit ()
Data-driven
Python automated test framework-data driven (read from code)