Python automated test framework-data driven (read from code)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.