PS: Turn to someone else's
Parameterization of QTP
Method One, DataTable method
This is a method provided by QTP and is the easiest way to implement parameterization. QTP provides a number of methods for DataTable objects that allow you to manipulate the DataTable flexibly. The DataTable is divided into global and local two, and global action is available, and local is only available for the current action.
The most straightforward way to parameterize through a DataTable is through the options under Keyword view, which is convenient and reduces the chance of error. Click the Value column of the item you want to parameterize, and select the arrow that appears to pop up the Value Configuration Option dialog box, where you can easily parameterize it.
Parameters Select the data source type. After selecting a DataTable in the drop-down list, select the data table you want to use global or local, and finally choose which column the name parameter is taken from, and click OK to complete the parameterization process.
The code is as follows: Dialog ("Login"). Winedit ("Agent Name:"). Set DataTable ("user", Dtglobalsheet)
This is the simplest and most straightforward way. In this way, you need to be aware that the file---seting---run needs to be set up, otherwise the result is very error-prone, so that the actual value of the parameter is not the same as expected or incorrect loop error. The best way to do this is by writing a statement to control the value in the iteration process. This approach is most commonly used during script development. Similar to the following code:
For I=0 to Datatable.getcurrentrow
Dialog ("Login"). Winedit ("Agent Name:"). Set DataTable ("user", Dtglobalsheet)
Datatable.setnextrow
Next
Questions about the DataTable iterations:
A, file-->settings-->run under the data table iterations is set to control the operation of the data in the global database; Global is the world! Run a few rows of data when the run mode is set to run all or multiple rows " Program "will be played back several times!! Cannot be reset!!
B, Edit-->action-->action call Properties-->run under the data table iterations is set to control the operation of the data tables in the action; local is partial! When run mode is set to run all or more rows, run a few rows of data "the action" will be played back several times!!
Further clarification:
- When global has multiple rows of data file-->settings-->run on all rows; action has more than one line of data action call Property->run the all Rows program runs each time, Each row in the action is executed once
- When global has multiple rows of data file-->settings-->run on all rows, action has multiple data, action call Property->run one iteration only and Globa The number of rows of >action, when the action executes to the last row, regardless of the number of rows in the global, the action executes the last line at the next playback! If the number of rows for global is <action, the action will not execute to the last row
- Action call Property->run from rows to rows, as you can understand with the above
- When there are multiple arguments in the same action, and action call Property->run on all Rows, this time the number of data for each parameter needs to be equal
Method Two, the environment variable realizes the parameterization
The Environment object provides access to the environment variable. There are two ways to source environment variables: Internal environment variables and user-defined environment variables, which support externally imported formats as XML files. The parameterization of environment variables has some limitations, because the environment variables are not flexible to the operation of the data, so the environment variables are mostly shared with the data. In this temporary as a way to learn, flexible use is good.
First of all, the user-defined environment variables, you need to define the variable name and value. Once defined, you can use these variables to parameterize the constants in the script.
Dialog ("Login"). Winedit ("Agent Name:"). Set Environment ("TEST3")
When you do this, each parameter value needs to be specified and cannot be generated in bulk. So it has a certain application scenario: when a different action in a test requires the same parameter, it is a good way to parameterize the constants with the environment variables, and then the environment variables can be used to solve the problem when the different test needs to use the same parameters.
Second, the internal environment variables. It is a set of variables defined by default QTP, including some system information, project information, and so on. Currently the most used is TestDir, which can be used to achieve a relative directory. For specific applications, when making a data-driven script, place the data file in the script folder and then use Environment ("TestDir") +filename to import the data file. This allows for easy porting without the need to consider putting data files in a specific directory.
Method Three, the external data source realizes the Parameterization
The use of external data-driven scripts is a frequently used method, which makes it easy to organize test data. Relative to the first two methods, this way data reading, control a little trouble. The following are described in several common ways.
1. Data files are organized in Excel
It is most commonly used to organize test data in Excel. This type of driver can be used in two ways, import data into Datatab or use COM to manipulate Excel files.
Mode 1, import into the DataTable
DATASTR = Environment ("TestDir") & "\login.xls"
Datatable.addsheet ("Login")
Datatable.importsheet datastr, "Sheet1", "Login"
RowCount1 = Datatable.getsheet ("Login"). GetRowCount
For i = 1 to RowCount1
Systemutil.run "C:\Program files\hp\quicktest professional\samples\flight\app\flight4a.exe"
DataTable. Setcurrentrow (i)
user = Datatable.value ("User", "Login")
PWD = Datatable.value ("pwd", "Login")
Dialog ("Login"). Winedit ("Agent Name:"). Set User
Dialog ("Login"). Winedit ("Password:"). Set pwd
Dialog ("Login"). Winbutton ("OK"). Click
Window ("Flight reservation"). Winmenu ("menu"). Select "File; Exit "
Next
Note: QTP supports the XLS suffix of an Excel document that does not support the xlsx suffix
Mode 2, using COM to manipulate Excel
Datastr=environment ("TestDir") & "\login.xls" Set excelapp = CreateObject ("Excel.Application") ' Create Excelapp Object excelapp.visible = Trueexcelapp.displayalerts = False Set book = ExcelApp.Workbooks.Open (DATASTR) ' Open Excel File Workbook set Excelshe = Book. Worksheets ("Sheet1"). UsedRange ' You need to use usedrange here to indicate the range of cells used by SHEET1, otherwise all cells will be printed out rowcount = ExcelShe.rows.count ' Calculate the number of cell rows used for i = 2 to rowcount systemutil.run "C:\Program files\hp\quicktest professional\samples\flight\app\ Flight4a.exe " user = Excelapp.worksheets (" Sheet1 "). Cells (i,1) pwd = excelapp.worksheets (" Sheet1 "). Cells (i,2) Dialog ("Login"). Winedit ("Agent Name:"). Set user Dialog ("Login"). Winedit ("Password:"). Set pwd Dialog ("Login"). Winbutton ("OK"). Click Window ("Flight reservation"). Winmenu ("menu"). Select "File; Exit "Nextset Excelshe = nothingbook.close ' Close Workbook object excelapp.quit ' Exit Excelapp Object
Note: excelapp.visible whether to display Excel as the current window.
2. Data files in TXT organization
Const forreading=1 ' open file as read-only
Tfilepath= Environment ("TestDir") & "\login.txt"
Set Fso = CreateObject ("Scripting.FileSystemObject")
Set datafile= fso.opentextfile (tfilepath,forreading,false)
Datafile.skipline ' skips the next line while reading the TextStream file. If the read file is not open, an error is generated.
Do While Datafile.atendofline<>true
ReadString = Datafile.readline ' reads an entire line of characters from the TextStream file and returns the result as a string.
Datastr=split (ReadString, ",") is used to split a ReadString with a comma into a string array.
Dialog ("Login"). Winedit ("Agent Name:"). Set datastr (0) ' accesses the first character of a string array
Dialog ("Login"). Winedit ("Password:"). SetSecure Datastr (1)
Loop
Datafile.close
Set fso=nothing
Note: The AtEndOfLine property applies only to TextStream files that are opened as read, otherwise an error occurs.
3. Data files are organized in a database
Create a database login with access and create a table login with fields User,pwd, and the primary key ID automatically generated when the table is created
strdb= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\program files\hp\quicktest Professional\Tests\Flight\ Flightlogin\login.mdb; " _
& "Persist Security info=false"
Strtablename= "Login"
Set conn=createobject ("Adodb.connection")
Set rst=createobject ("Adodb.recordset")
Conn.Open strDB
Rst.open "SELECT * from" +strtablename,conn,2,2
Dim strtest (1) ' declares an array of length 2
Rst.movefirst
Do and not rst.eof
Strtest (0) =trim (CStr (Rst.fields (1)) ' CStr convert the contents of parentheses into strings
Strtest (1) =trim (CStr (Rst.fields (2)))
Dialog ("Login"). Winedit ("Agent Name:"). Set strtest (0)
Dialog ("Login"). Winedit ("Password:"). SetSecure strtest (1)
Rst.movenext
Loop
Rst.close
Set conn=nothing
4. Data files are organized in XML
Dim xmldoc ' as DOMDocument needs to reference an XML object
Set Xmldoc=createobject ("Microsoft.XMLDOM")
Xmldoc.load ( Environment ("TestDir") & "\login.xml")
Set root=xmldoc.documentelement ' Returns the root node of the document.
For i = 0 to root.childnodes.length-1 ' retrieves the number of child nodes under the root node
Set testcases = Root.childNodes.Item (i)
For j = 0 to Testcases.childnodes.length-1 ' retrieves the number of nodes under TestCase
Set TestCase = testcases.childnodes. Item (j)
If CStr (testcase.nodename) = "User" then
Dialog (" Login "). Winedit ("Agent Name:"). Set testcase.text
End If
if CStr (testcase.nodename) = "pw D "Then
Dialog (" Login "). Winedit ("Password:"). SetSecure testcase.text
End If
Ne XT
Next
Set root=nothing
Set xml=nothing
Method four, random number
Random number parameterization is much simpler than calling external data for parameterization, so let's take a look at an example.
When the range of parameters is known, the script switches to keyword view, click the Value column to parameterize the item, parameter the option to select Randomnumber, enter a value range for the parameter, and click OK.
Window ("Flight reservation"). Dialog ("Open Order"). Winedit ("Edit"). Set randomnumber (0, 100)
For example, the following dropdown box randomly selects the value in the drop-down box:
Flyfrom = window ("Flight reservation"). Wincombobox ("Fly from:"). Getitemscount
Window ("Flight reservation"). Wincombobox ("Fly from:"). Select Randomnumber (0,flyfrom-1)
Parameterization of QTP