Python + Selenium notes (12): data-driven testing, pythonselenium

Source: Internet
Author: User

Python + Selenium notes (12): data-driven testing, pythonselenium

(1)Preface

Data-driven tests are used to parameterize input values and expected results. (For example, you can directly read the data in the Excel document by entering data and expected results)

(2)Ddt

Using ddt to perform a data-driven test, the ddt database can parameterize the variables in the test. When using ddt, use the @ ddt modifier in the test class and the @ data modifier in the test method. @ Data decorator uses parameters as test data. parameters can be single values, lists, tuples, and dictionaries. For list and metadata groups, you need to use the @ unpack modifier to parse the list and metadata into multiple parameters.

Run the following command to install the ddt database:

Pip install ddt

(3)Obtain data in Excel

To read an Excel file, you must use the xlrd library.

Library for installing xlrd

Pip install xlrd

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

Pip install xlwt

(4)Excel files used in the example

Email

Mobile phone

Login Name

Nickname

Password

Confirm Password

Expected results

Test@outlook.com

1

Bky_110

Pangu

Test> 100

Test> 100

Incorrect mobile phone number

Test@outlook.com

18898989878

B

Pangu

Test> 100

Test> 100

Not required. It must contain at least 2 Characters and a maximum of 30 characters.

(5)Example

1 from selenium import webdriver 2 from ddt import ddt, data, unpack 3 import xlrd 4 import unittest 5 # function 6 def get_data (file_name) for reading excel files ): 7 rows = [] 8 # Read excel Data 9 book = xlrd. open_workbook (file_name) 10 # access the first sheet page 11 sheet = book through the index. sheet_by_index (0) 12 # Read the data on the first sheet page of excel iteratively. nrows indicates the number of rows in excel 13 for r_idx in range (1, sheet. nrows): 14 # row_values reads data from row r_idx (0 indicates reading data from 1st columns and all subsequent columns) 15 # When reading data, in general, when the first row and the first column are indexed at 016 #, r_idx = 1, the data read from the second row of excel is 17 rows. append (list (sheet. row_values (r_idx, 0) 18 # Delete the mobile phone number and assign it to pthone, convert it to a string, and add it back to the original position 19 pthone = rows [r_idx-1]. pop (1) 20 rows [r_idx-1]. insert (1, str (int (pthone) 21 return rows22 @ ddt23 class RegisterNewUserDDT (unittest. testCase): 24 @ classmethod25 def setUpClass (cls): 26 cls. driver = webdriver. chrome () 27 cls. driver. implicitly_wait (20) 28 cls. driver. maximize_window () 29 cls. driver. get ('https: // www.cnblogs.com/') 30 login_area = cls. driver. find_element_by_css_selector ('# login_area') 31 register = login_area.find_element_by_link_text ('registration') 32 register. click () 33 # Read excel file data as parameter 34 @ data (* get_data ('data/reTest.xlsx ') 35 @ unpack36 def test_register_new_user (self, email, phone, login_name, nickname, password, confirm_password, expected_result): 37 driver = self. driver38 self. assertTrue ('user registration-blog service' = driver. title) 39 # locate fields on the registration page 40 user_email = driver. find_element_by_id ('email ') 41 user_phone_country = driver. find_element_by_id ('countrycode') 42 user_phone = driver. find_element_by_id ('phonenum') 43 user_login_name = driver. find_element_by_id ('loginname') 44 user_nickname = driver. find_element_by_id ('displayname') 45 user_password = driver. find_element_by_id ('Password') 46 user_confirm_password = driver. find_element_by_id ('confirmpassword') 47 # Clear the value of each field (if any) 48 user_email.clear () 49 user_phone.clear () 50 then () 51 user_nickname.clear () 52 user_password.clear () 53 user_confirm_password.clear () 54 # enter the email address, mobile phone number, and other information 55 minutes (email) 56 minutes (phone) 57 minutes (login_name) 58 user_nickname.send_keys (nickname) 59 user_password.send_keys (password) 60 user_confirm_password.send_keys (confirm_password) 61 # Check whether the prompt is correct (there should be a way to obtain the number of executions currently, the following writing method is too rigid) 62 if phone = '1': 63 phone_error = driver. find_element_by_id ('phonenum-error') 64 self. assertTrue (phone_error.text = expected_result) 65 elif login_name = 'B': 66 loginName_error = driver. find_element_by_id ('loginname-error') 67 self. assertTrue (loginName_error.text = expected_result) 68 69 @ classmethod70 def tearDownClass (cls): 71 cls. driver. quit ()

 

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.