Python+selenium Notes (12): Data-driven testing

Source: Internet
Author: User
Tags unpack

(i) preface

Parameterization of input values and expected results is achieved by using data-driven testing. (Example: input data and expected results can be read directly from the Excel document data)

(ii) DDT

Using DDT to perform data-driven testing, the DDT library can parameterize variables in the test. When using DDT, use the @ddt adorner on the test class and use the @data adorner on the test method. @data Decorators Use parameters as test data, parameters can be single values, lists, tuples, dictionaries. For lists and tuples, you need to parse the list and tuples into multiple parameters with the @unpack decorator.

Use the following command to install the DDT library

Pip Install DDT

(iii) obtaining data through Excel

To read an Excel file, you need to use the XLRD library.

Installing the XLRD Library

Pip Install Xlrd

If you want to write data to an Excel table, you need to use the XLWT library

Pip Install XLWT

(iv) Excel files used in the example

Mailbox

Cell phone

Login Name

Nickname

Password

Confirm Password

Expected results

[email protected]

1

Pangu

test>100

test>100

wrong phone number

[Email protected]

18898989878

B

Pangu

test>100

test>100

Non-compliant, at least 2 characters, up to 30 characters

(v) example

1  fromSeleniumImportWebdriver2  fromDdtImportDdt,data,unpack3 Importxlrd4 ImportUnitTest5 #functions for reading Excel files6 defGet_data (file_name):7rows = []8     #reading data from Excel9Book =Xlrd.open_workbook (file_name)Ten     #Accessing the first sheet page through an index OneSheet =book.sheet_by_index (0) A     #Iterates through the data of the first sheet page of Excel, sheet.nrows refers to the number of lines in Excel -      forR_idxinchRange (1, sheet.nrows): -         #row_values reads data from row R_IDX (0 for reading data from column 1th and all subsequent columns) the         #when reading the data, we generally say the first row, the first column, the index is 0 -         #so when r_idx=1, reading is actually the second line of Excel data - rows.append (List (sheet.row_values (r_idx,0))) -         #first delete the phone number and assign it to Pthone, then convert it to a string and add it back to its original location. +Pthone = Rows[r_idx-1].pop (1) -Rows[r_idx-1].insert (1, str (int (pthone))) +     returnrows A @ddt at classREGISTERNEWUSERDDT (unittest. TestCase): - @classmethod -     defSetupClass (CLS): -Cls.driver =Webdriver. Chrome () -Cls.driver.implicitly_wait (20) - Cls.driver.maximize_window () inCls.driver.get ('https://www.cnblogs.com/') -Login_area = Cls.driver.find_element_by_css_selector ('#login_area') toRegister = Login_area.find_element_by_link_text ('Register') + Register.click () -     #reading data from an Excel file as a parameter the@data (*get_data ('data/retest.xlsx')) * @unpack $     defTest_register_new_user (Self,email,phone,login_name,nickname,password,confirm_password,expected_result): Panax NotoginsengDriver =Self.driver -Self.asserttrue ('User Registration-Blog Park'==driver.title) the         #locate each field in the registration page +User_email = driver.find_element_by_id ('Email') AUser_phone_country = driver.find_element_by_id ('CountryCode') theUser_phone = driver.find_element_by_id ('Phonenum') +User_login_name = driver.find_element_by_id ('LoginName') -User_nickname = driver.find_element_by_id ('DisplayName') $User_password = driver.find_element_by_id ('Password') $User_confirm_password = driver.find_element_by_id ('ConfirmPassword') -         #clear values for each field (if any) - user_email.clear () the user_phone.clear () - user_login_name.clear ()Wuyi user_nickname.clear () the user_password.clear () - user_confirm_password.clear () Wu         #Enter your email address, phone number and other information - User_email.send_keys (email) About User_phone.send_keys (phone) $ User_login_name.send_keys (login_name) - User_nickname.send_keys (nickname) - user_password.send_keys (password) - User_confirm_password.send_keys (Confirm_password) A         #Judging whether the prompt is correct (there should be a way to get the current is the first several times, the following wording is too rigid) +         ifPhone = ='1': thePhone_error = driver.find_element_by_id ('Phonenum-error') -Self.asserttrue (Phone_error.text = =Expected_result) $         elifLogin_name = ='b': theLoginname_error = driver.find_element_by_id ('Loginname-error') theSelf.asserttrue (Loginname_error.text = =Expected_result) the  the @classmethod -     defTeardownclass (CLS): inCls.driver.quit ()

Python+selenium Notes (12): Data-driven testing

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.