Data-driven testing (Python)

Source: Internet
Author: User

Two drives in the field of automation, object-driven and data-driven

    • Data driven: Changes in test data cause changes in execution results called data-driven
    • Keyword driver: Change of test object name causes change of test result called keyword driver

1, read the parameter of the file
Baidu Table Search As an example, we can through the script loop execution, read a file of different content to complete the automatic
, that is to say, each time we take the search for the keyword is different, and each time Baidu search results are different,
This is also the nature of data-driven.

The code is as follows:
D:\abc\data.txt
baidu_read_data.py

#coding =utf-8from Selenium Import webdriverimport os,timesource = open ("D:\\abc\\data.txt", "r") values = Source.readlines () Source.close () # Executes the loop for serch in Values:browser = Webdriver. Firefox () browser.get ("http://www.baidu.com") browser.find_element_by_id ("kw"). Send_keys (Serch) browser.find_ element_by_id ("su"). Click () browser.quit ()

Here's a brief explanation, the Open method opens the local data.txt file as read-only (R), and the ReadLines method reads the file contents line-by-row.

With a For loop, Serch can get a row of data to the file each time, after positioning to the Baidu input box,
Pass data into Send_keys (Serch). This is called by loop until all the contents of the file are read.
2, the user name password parameterization (read files)

The following example East one login Baidu as an example, the main is to read the file (because Baidu login, need to verify the code, so there will not be successful login)

According to the above method, the user name password in the automation script is parameterized, from the current search to the Python read method has, the entire file read, read by line, fixed byte read.

How to read a user name and password at a time two information, the original modification is this:
Create two files, each with a username and password
Calling User name password login logon script

#coding =utf-8from Selenium Import webdriverimport os,timesource = open ("D:\\abc\\data2.txt", "r") #用户名文件user = Source.read (5) #用户名长度source. Close () Source2 = open ("D:\\abc\\data3.txt", "r") #密码文件pw = Source2.read (6) # Password length source2.close () Driver = Webdriver. Firefox () driver.get ("Https://passport.baidu.com/v2/?login&tpl=mn&u=https%3A%2F%2Fwww.baidu.com%2F") driver.find_element_by_id ("Tangram__psp_3__username"). Clear () driver.find_element_by_id ("tangram__psp_3__ UserName "). Send_keys (k) time.sleep (3) driver.find_element_by_id (" Tangram__psp_3__password "). Clear () Driver.find_ element_by_id ("Tangram__psp_3__password"). Send_keys (v) time.sleep (3) driver.find_element_by_id ("TANGRAM__PSP_3__ Submit "). Click () time.sleep (3) Driver.close ()

Disadvantages:

Although the goal is achieved, there are many problems with this implementation:

    • User name password in separate files, so that the user name password must be one by one corresponding
    • The length of the read must be specified, and the test readlines () is not a row of data being read.
    • Cannot loop read.

3, the user name of the parameterization (dictionary)
User name Password parameterization
Resolving Loop Calls
If fixed just read user name, password two value, can be implemented by the following method.
Create a fun.py file that defines a dictionary method (the method file and the case file are placed in a root directory):

Def zidian ():d ={' fnngj ': ' a23456 ', ' testing360 ': 123456}print "suess read username and password!!" Return D

The dictionary can conveniently store k,v key pairs, a key corresponding to a value; Note that if there are non-digits in the password, a single quotation mark is required.

The following loop invokes the value of the dictionary:

#coding =utf-8from Selenium Import webdriverimport os,timeimport fun #导入函数 # Loop through the user name password in the dictionary, assigning values to K,vfor k,v in Fun.zidian (), respectively . Items ():d river = Webdriver. Firefox () driver.get ("Https://passport.baidu.com/v2/?login&tpl=mn&u=https%3A%2F%2Fwww.baidu.com%2F") driver.find_element_by_id ("Tangram__psp_3__username"). Clear () driver.find_element_by_id ("tangram__psp_3__ UserName "). Send_keys (k) time.sleep (3) driver.find_element_by_id (" Tangram__psp_3__password "). Clear () Driver.find_ element_by_id ("Tangram__psp_3__password"). Send_keys (v) time.sleep (3) driver.find_element_by_id ("TANGRAM__PSP_3__ Submit "). Click () time.sleep (3) Driver.close ()

The script is a lot more stable, the value of each fetch is very fixed, and the same implementation of the parameters and script separation, if hundreds of scripts call the fun () function, when you need to modify the user name password, only modify the fun () function inside the word

The value of the code is available.
4, the User name password parameterization (function)
In fact, in the project only need to be parameterized on the line, do not need to loop read content. Then the function call can be easily solved.
First define the fun.py, (the method file and case file are placed in a root directory)

def user (k= ' fnngj ', v=123456): print "Suess read username and password!!" Return k,v

Assigns a default value and returns the result.

Call function Value:

#coding =utf-8from Selenium Import webdriverimport os,time# importing function Import fun #通过调用函数获得用户名 & Password k,v = Fun.user () print K, Vdriver = Webdriver. Firefox () driver.get ("Https://passport.baidu.com/v2/?login&tpl=mn&u=https%3A%2F%2Fwww.baidu.com%2F") driver.find_element_by_id ("Tangram__psp_3__username"). Clear () driver.find_element_by_id ("tangram__psp_3__ UserName "). Send_keys (k) driver.find_element_by_id (" Tangram__psp_3__password "). Clear () driver.find_element_by_id ( "Tangram__psp_3__password"). Send_keys (v) driver.find_element_by_id ("Tangram__psp_3__submit"). Click () Time.sleep ( 3) Driver.close ()

Operation Result:

>>> =================== RESTART ================================
>>>
Suess Read username and password!!
Fnngj 123456
.
----------------------------------------------------------------------
Ran 1 Test in 25.484s
Ok

Data-driven testing (Python)

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.