Objective
in 17 we talked about the parameterization of Excel data, someone asked the TXT data parameterization what to do, the following small series for you to take your TXT data parameterized explanation
First, Baidu search As an example, automatically search five times different keywords. The input data is different, which causes the output to change.
Test script:
#coding =utf-8
From selenium import Webdriver
Import UnitTest, time, OS
Class Login (UnitTest. TestCase):
def test_login (self):
Source = open ("D:\\test\\txt.txt", "R")
Values = Source.readlines ()
Source.close ()
# execution Loop
For Hzy in values:
Driver=webdriver. Firefox ()
Driver.get ("http://www.baidu.com/")
Driver.maximize_window ()
driver.find_element_by_id ("kw"). Send_keys (Hzy)
driver.find_element_by_id ("su"). Click ()
Time.sleep (2)
Driver.close ()
TXT file:
The Open method opens the local txt.txt file as read-only (R), and the ReadLines method reads the entire file content line by row.
With a For loop, Hzy can get a row of data to the file each time, after locating the Baidu input box, the data is passed into Send_keys (Hzy). This is called by loop until all the contents of the file are read.
Second, login parameterization
Now according to the above ideas, the Automation script user, the name of the password parameterized, through the Python document we found that Python read the file: The entire file read, read-line, fixed byte read.
Did not find a good way to read two data at a time.
Create two files, each of which holds the user name password.
Test script:
#coding =utf-8
From selenium import Webdriver
From selenium.common.exceptions import nosuchelementexception
Import UnitTest, time, OS
Class Login (UnitTest. TestCase):
def test_login (self):
Source = open ("D:\\test\\un.txt", "R") #用户名文件
UN = source.readline () #读取用户名
Source.close ()
Source2 = open ("D:\\test\\pw.txt", "R") #密码文件
PW = Source2.readline () #读取密码
Source2.close ()
Driver=webdriver. Firefox ()
Driver.get ("http://www.baidu.com/")
Driver.maximize_window ()
driver.find_element_by_id ("txtUserName"). Clear ()
driver.find_element_by_id ("txtUserName"). Send_keys (un)
driver.find_element_by_id ("Txtpassword"). Clear ()
driver.find_element_by_id ("Txtpassword"). Send_keys (PW)
driver.find_element_by_id ("Userlogin"). Click ()
Time.sleep (2)
Try
t = Driver.find_element_by_xpath ("//form/div[4]/div/div[1]/div[1]/div/a/img")
Except nosuchelementexception:
Assert 0, U "Login failed, unable to find upper left corner log"
Driver.close ()
Originally wanted to use the example of Baidu, but there is a verification code, trouble, so with the company intranet test environment.
Open TXT file separately, through UN and PW to receive user account and password information, transfer the received data through Send_keys (XX) into the execution program.
Although the goal is achieved, there are many problems with this implementation:
1, the user name password in different files, modify the user name and password is more troublesome.
2, Un.txt and Pw.txt files can only save a user password, the inability to read a good loop.
Multiple account and password TXT file, small series has not found a solution, welcome to give comments and ideas.
Multi-account and password suggestions are also solved with Excel parameterization.
Selenium2+python Automated 21-txt data parameterization "reprint"