Qtp import Excel File Read Excel file parameterization

Source: Internet
Author: User

Parameterization types: data table, environment variable, random number, and external data parameterization
1. Data Table parameterization
A
,
File --> Settings --> run data table
In iterations, the operation mode of data in global in the data table is set to control; Global is global! When the running mode is set to run all or multiple rows, run several rows of data
How many times will it be played back !! Cannot be reset !!
B, Edit --> action
Call properties --> run data table
In iterations, the operation mode of the data in the action in the data table is controlled. Local
Yes! When the running mode is set to run all or multiple rows, it is necessary to play back several lines of Data "this action !!
Further description:
A,
When global has multi-row data file --> Settings --> run on all rows; action has multi-row data action
Call property-> run on all rows each time the program runs, each line in the action must be executed once
B,
When global has multiple rows of data file --> Settings --> run on all rows; action has multiple rows of data, Action
Call property-> run one iteration only
And the number of rows in global> the number of rows in action. When the action is executed to the last row
The number of rows is several. the last row is executed for the action in the next playback! If the number of rows in global is less than the number of rows in action, the action cannot be executed on the last row.
CAction call property-> run from rows to rows, which can be understood in conjunction with the above descriptions.
DWhen multiple parameters exist in the same action, and action call property-> run on all rows, the Data Count of each parameter must be equal.

2. Random Number (random) parameterization
A
Can specify the range of random numbers
BYou can specify a name for the random number, and specify the method by which the random parameter generates a new random number. In this case, the random number can be used elsewhere.
Method for generating a new random number:
AFor each action iteration the random number of the same action does not change during iteration, and the random number is re-generated only when different actions are run.

Remain unchanged in an action
B, For each test iteration in each test run complete process, the random number remains unchanged. The random number changes when the test is repeated, for example, when the global parameter set in data table is executed cyclically.

The storage remains unchanged in one system Iteration
C, Once per entire test run in the whole test the random number remains unchanged, equivalent to only generate a random number once

Remain unchanged throughout the test

3. environment variable (Environment) parameterization
A
The environment variables are derived from the internal environment variables and user-defined environment variables. Internal environment variables are a set of variables defined by qtp by default, including system information and project information, such as testdir and OS. The user-defined environment variables must be defined by the user. They can be imported from external sources in the XML format.
BEnvironment variable parameters have certain limitations. It does not have flexible data tables for data operations, and the most commonly used environment variables are data sharing.
CWhen environment variables are used as parameters, each parameter value must be specified and cannot be generated in batches. Therefore, it has certain application scenarios: when different actions in a test need the same parameter, it is a good method to parameterize constants using environment variables. Second, when the same parameters are required for different test, using environment variables can solve this problem well.

4. parameterization of external data sources
This is a frequently used method to run scripts driven by external data, which facilitates the organization of test data. Compared with the first three methods, it is a little difficult to read and control data in this way. The following describes several common methods.
A. data files are organized in Excel format
Using EXCEL to organize test data is the most common. This type of driver can be used to import data to AB or use com to manipulate Excel files. The following is a sample code for the two methods. (Enter the flight user name and password)

 Method 1Import to able
'Get the data file path and import the data file to the datatable running table
Datastr = "D: \ data.xls"
Datatable. addsheet ("testdata ")
Datatable. importsheet datastr, "sheet1", "testdata"
Wait 1
'Enter the user name and password
For I = 1 to datatable. getsheet ("testdata"). getrowcount
Systemutil. Run "D: \ Program Files \ quicktest Professional \ samples \ flight \ app \ flight4b.exe"
Datatable. setcurrentrow I 'sets I to the current execution row
Username = datatable. Value ("username", "testdata ")
Password = datatable. Value ("password", "testdata ")
Dialog ("login"). winedit ("agent name:"). Set Username
Dialog ("login"). winedit ("Password:"). setsecure Password
Wait 1
Dialog ("login"). winbutton ("cancel"). Click
Next

Method 2Use com to manipulate Excel
Datastr = "D: \ data.xls"
Set exlobj = Createobject ("Excel. application ")
When exlobj. Visible = false' is set to true, the document will open
Exlobj. displayalerts = false
Set book = exlobj. workbooks. Open (datastr)
Set sheet = book. worksheets ("sheet1 ")

For I = 2 to sheet. usedrange. Rows. Count
Systemutil. Run "D: \ Program Files \ quicktest Professional \ samples \ flight \ app \ flight4b.exe"
Username = exlobj. worksheets ("sheet1"). cells (I, 1) 'or username = sheet. cells (I, 1)
Password = exlobj. worksheets ("sheet1"). cells (I, 2) 'or password = sheet. cells (I, 2)
Dialog ("login"). winedit ("agent name:"). Set Username
Dialog ("login"). winedit ("Password:"). setsecure Password
Wait 2
Dialog ("login"). winbutton ("cancel"). Click
Next
Exlobj. Quit
Set exlobj = nothing

B. Data Files are organized in TXT format

Const forreading = 1
Tfilepath = "D: \ data.txt"
Set F_3 = Createobject ("scripting. FileSystemObject ")
Set datafile = fso3.opentextfile (tfilepath, forreading, false)
Datafile. skipline
Do While datafile. atendofline <> true
Systemutil. Run "D: \ Program Files \ quicktest Professional \ samples \ flight \ app \ flight4b.exe"
Readstring = datafile. Readline
Datastr = Split (readstring ,",")
Dialog ("login"). winedit ("agent name:"). Set datastr (0)
Dialog ("login"). winedit ("Password:"). setsecure datastr (0)
Wait 2
Dialog ("login"). winbutton ("cancel"). Click
Loop
Datafile. Close
Set F_3 = nothing

C. Data Files are organized in databases. The following code uses access. Other types of databases are similar.

 

Strdb = "provider = Microsoft. Jet. oledb.4.0; Data Source = D: \ data. mdb; persist Security info = false"
Strtablename = "data"
Set conn = Createobject ("ADODB. Connection ")
Set rst = Createobject ("ADODB. recordset ")
Conn. Open strdb
RST. Open "select * from" + strtablename, Conn, 2, 2
Dim strtest (1)
RST. movefirst
Do while not RST. EOF
Systemutil. Run "D: \ Program Files \ quicktest Professional \ samples \ flight \ app \ flight4b.exe"
Strtest (0) = trim (CSTR (RST. Fields (1 )))
Strtest (1) = trim (CSTR (RST. Fields (2 )))
Dialog ("login"). winedit ("agent name:"). Set strtest (0)
Dialog ("login"). winedit ("Password:"). setsecure strtest (1)
RST. movenext
Wait 2
Dialog ("login"). winbutton ("cancel"). Click
Loop
RST. Close
Set conn = nothing

D. Organize data files in XML format

 

 

Dim xmldoc 'As domdocument must reference an XML Object
Set xmldoc = Createobject ("Microsoft. xmldom ")
Xmldoc. Load ("D: \ testdata. xml ")
Set root=xmldoc.doc umentelement
For I = 0 to root. childnodes. Length-1
Systemutil. Run "D: \ Program Files \ quicktest Professional \ samples \ flight \ app \ flight4b.exe"

Set testcases = root. childnodes. Item (I)
For J = 0 to testcases. childnodes. Length-1
Set testcase = testcases. childnodes. Item (j)
If CSTR (testcase. nodename) = "username" then
Dialog ("login"). winedit ("agent name:"). Set testcase. Text
End if
If CSTR (testcase. nodename) = "password" then

Dialog ("login"). winedit ("Password:"). setsecure
Testcase. Text
End if
Next
Wait 2
Dialog ("login"). winbutton ("cancel"). Click

Next
Set root = nothing
Set xml = nothing

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.