Introduction
In this example, you will learn the following:
How to convert the base script to a data-driven
script to fetch multiple values from CSV using the data configuration
option.
How to use a for loop construct to iterate through
the multiple set of data using a single script.
This sample webscript named test-for-construct
is an illustration of writing data driven test cases and usage of programming
constructs such as for loop in
the scripts.
Assumption is that there exists a CSV (Comma Separated Value) file test.csv in <QEngine
Home>/examples/payrollsystem folder that stores details of the
employees such as employee name, department.
Copy this file to <QEngine_Home>/projects/AdventNetPayrollSystem/dataset
directory.This data is going to drive the testing of the page view.html.
4 rows (records) and 2 columns exists in the CSV file.
The following steps will help you to create a test script which is the
same as the sample script test-for-construct.
This is to illustrate the usage of how to fetch values from CSV using
the Data Configuration option and how to use for loop construct.
Step 1: Selecting the Suite
In the Web Client UI, from the Suite tree, select the Suite "AdventNetPayrollSystem".
Click the "Continue" button and select the test type as Web
Application Functionality. This will display the Web Functional Main UI.
Step 2: Creating New Script
Create a script using the New Script
option from Quick Links placed
below the top frame. You can also right-click the webscripts node and
choose the Create New Script option
from the popup menu. In the dialog, specify the name of the new script
file as "test-script" and click the OK
button. The script file is by default saved in <QEngine_Home>/webscripts/<script_name>
directory. You can also save the script file in any other directory under
<QEngine_Home>/webscripts/ directory
(by creating a new directory).
Step 3: Launch Browser
Choose the Launch Browser option
from QEngine Toolbar to launch the browser. The default browser will be
launched. Specify the URL of the Web page as "http://localhost:4444/examples/payrollsystem/index.html"
in the launched browser.
Step 4: Start Recording Web Page Events
To record the events,
Choose the Start
Record option from the QEngine Toolbar.
From the index.html page, click the Employee
Details->View menu option. This will display the view.html
page.
Enter some dummy values for the name and department
fields. The list of actions performed in the Web page are recorded in
the newly created script file. This acts as the skeletal script which
is then edited to create the data-driven script as given in Step
6.
Step 5: Stop Recording Events
To stop recording events, choose the Stop
Record option from the QEngine Toolbar.
Step 6: Editing
Scripts to Insert Data Configuration and for loop construct
In case of test-user-login script,
This generated the skeletal script for one set of values in the view
form. Now, you need to edit the script to convert it into a data-driven
script. In this step, you will learn how to insert data configuration
to fetch values from a CSV file, how to use a for loop construct to store
the values into variables and replace them in the appropriate setText
statements. This will enable you to playback the view.html
form with multiple set of data using a single script.
Inserting Data Configuration
1. Place the cursor after the following line:
|
useLocalMapFile()
launchApplication("about:blank")
changeURL("http://$QE_SERVER:$QE_PORT/qehome/examples/payrollsystem/index.html",2)
setWindow( "AdventNet Payroll System",2)
clickList("View",2)
setWindow( "AdventNet Payroll System
Package - Form to View Employee Details",2) |
2. Choose Data Configuration
option
from the script editor toolbar. The View
Data Configuration screen will be displayed.
3. To add a new datasource, click the Add
button. This will display the Add/View
Data Configuration screen.
4. In this screen, from the Select DataSource Type combo, select the
datasource type as csv. Enter the new datasource name in the DataSource
Name field as "test_datasource". This should be unique.
If an existing name is given then the values configured are modified for
the existing datasource.
5. To fetch data from a CSV file, select the datasource type as csv
and enter the appropriate values for the following fields:
|
Field Name |
Description |
|
CSV File Name |
Select the CSV file name "test.csv" from the combo. |
|
Start Row Index |
Specify the start row index value as 0. |
|
End Row Index |
Specify the end row index value as 3. |
|
Start Column Index |
Specify the start column index value as 1. |
|
End Column Index |
Specify the end column index value as 2. |
6. After configuring the values, choose the Save
button to save the configurations and choose the Paste
button to paste the selected datasource in the required location in the
test script. This will paste the following lines in the script:
|
initDataSet("test_datasource")
result
= getValuesFrom("test_datasource")
#********************************************************************************
#result
is a 2D array which stores the values fetched from the specified dataset.
#You
can fetch values from the 2D array using a for loop. A sample is shown
below.
#In
this sample, based on the number of columns in the csv, name and dept
values are fetched:
#for
i in range(0,len(result)):
#name=str(result[i][0])
#dept=str(result[i][1])
#setText("yourname",name,3)
#setText("dept",dept,3)
#******************************************************************************** |
Now, your script will look as follows:
|
#
This script illustrates the usage of --for-- construct in web scripts.
You can follow the
#
conventions of Python language and modify the web scripts as per your
need.
useLocalMapFile()
launchApplication("about:blank")
changeURL("http://$QE_SERVER:$QE_PORT/qehome/examples/payrollsystem/index.html",2)
setWindow(
"AdventNet Payroll System",2)
clickList("View",2)
setWindow(
"AdventNet Payroll System Package - Form to View Employee Details",2)
initDataSet("test_datasource")
result
= getValuesFrom("test_datasource")
#********************************************************************************
#result
is a 2D array which stores the values fetched from the specified dataset.
#You
can fetch values from the 2D array using a for loop. A sample is shown
below.
#In
this sample, based on the number of columns in the csv, name and dept
values are fetched:
#for
i in range(0,len(result)):
#name=str(result[i][0])
#dept=str(result[i][1])
#setText("yourname",name,3)
#setText("dept",dept,3)
#********************************************************************************
closeWindow(
"AdventNet Payroll System Package - Form to View Employee Details",3) |
Inserting for loop construct
7. Now, edit the above lines to iterate through the multiple sets of
data using a for loop and fetch the required column value from the two-dimensional
array as follows:
|
useLocalMapFile()
launchApplication("about:blank")
changeURL("http://$QE_SERVER:$QE_PORT/qehome/examples/payrollsystem/index.html",2)
setWindow(
"AdventNet Payroll System",2)
clickList("View",2)
setWindow(
"AdventNet Payroll System Package - Form to View Employee Details",2)
initDataSet("test_datasource")
result
= getValuesFrom("test_datasource")
#********************************************************************************
#result
is a 2D array which stores the values fetched from the specified dataset.
for
i in range(0,len(result)):
displayMessage(len(result))
//Storing
the value of name fetched from the 2D array in a variable
name=str(result[i][0])
displayMessage(name)
//Storing
the value of department fetched from the 2D array in a variable
dept=str(result[i][1])
displayMessage(name)
//Replacing
the setText statements with the variable
setText("yourname",name,3)
setText("dept",dept,3)
#********************************************************************************
closeWindow(
"AdventNet Payroll System Package - Form to View Employee Details",3) |
Jython scripting language is used in webscripts and hence you can refer
to the following URL http://www.jython.org/j-jython1-ltr.pdf to
know the various programming constructs such as if, for, while etc.
Step 7: Play Recorded Events
To play the recorded events, click on the script name in the left tree
and then choose the Start Play
option from the
QEngine Toolbar. On executing the script, you will see that the values
are retrieved from the CSV file one by one and displayed in the view.html page. The results or test
reports are automatically displayed after the test script has been successfully
completed