RSS Feed

Parameterization in QTP Explained with Examples – 1

Posted on

What is QTP parameterization?
Sometime application does not accept the duplicate data records. In this case if you run same test script with fixed set of input data, application may throw an error due to data duplication. To avoid this issue, QTP provide ways to accept different test inputs to the test script. This process of providing different input values through external parameters is called as parameterization

Types of parameterization in QTP
The variable value can be or the parameter types can be:

  1. Data Table parameters
  2. Test/Action parameters
  3. Environment variable parameters
  4. Random number parameters

In this QTP tutorial we will focus mainly on parameterization using Datatable. We will explain other types of parameterization methods in next tutorial.

Parameterization in QTP

Say you are trying to write a program that checks the login values for a couple of users on gmail.com.

The following is the code that you have for one user but you want the same to take different values each time. How do you do this?

Code to sign in to gmail for one user:

1 SystemUtil.Run "iexplore.exe", "http://www.gmail.com"
2 Browser("Gmail: Email from Google").page("Gmail: Email from Google").Sync
3 Browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebEdit("Email").Set "swatiseela"
4 Browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebEdit("Passwd").SetSecure "sfgs686898"
5 Browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebButton("Sign in").Click
6 Browser("Gmail: Email from Google").Page("Gmail - Inbox").Link("Sign out").Click

Now, go to keyword view and click on the value column for the email id and password set statements.

Typical screen that comes up when you are trying to parameterize:

QTP Parameterization datatable

As you can see, the value can either be a constant, “swatiseela” in this case, the login ID.

Or if you choose the parameterize option then the corresponding fields in the screen get activated.

QTP Parameterization datatable

From this screen you can choose to parameterize the chosen value with either a value from the data table, environment variable or a random number. Since the most often used source is the datatable we will discuss that first.

Apart from these, you could use the input and output values of a certain action as a parameter for a value. We will discuss that too in a while.

Parameterization in QTP using Datatable with Example

Parameterization in QTP using Excel
I checked the parameter value ON and then the there is a location in Datatable field following the name.

Name: The corresponding column name in the data table from where the data needs to be taken. By default QTP will suggest a name. You have an option to keep it as suggested or change it as needed.

Global Sheet:  This sheet of data is available to all the actions in a test.

Current action sheet or local sheet:  as the name suggests, it is the sheet of data that is available to a certain action.

I am going to multiple rows of data to the Global data sheet. This is where the password encoder tool comes in handy. You can put in encrypted values in your data sheet that you get from this tool.

This is how my data sheet looks like:

QTP Parameterization datatable

After parameterization this is how the code looks like:

1 SystemUtil.Run "iexplore.exe", "http://www.gmail.com"
2 Browser("Gmail: Email from Google").page("Gmail: Email from Google").Sync
3 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebEdit("Email").Set DataTable("SignInName", dtGlobalSheet)
4 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebEdit("Passwd").SetSecure DataTable("GPassword", dtGlobalSheet)
5 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebButton("Sign in").Click
6 Browser("Gmail: Email from Google").Page("Gmail - Inbox").Link("Sign out").Click
7 Browser("Gmail: Email from Google").page("Gmail: Email from Google").Sync
8 Browser("Gmail: Email from Google").Close

You will see in the above code that the values for the email ID and password are taken from datatable.

This code will run for all the 4 rows of data in the global sheet if in the following screen I set the option “Run on all rows” ON:

QTP Parameterization datatable

Just in case, if you don’t want to use the above screen to decide how many rows the code needs to be executed for you can do so programmatically. For that, you need to select the option “Run one iteration only” on the above screen and write the code the following way:

1 for i=1 to datatable.GetRowCount
2 SystemUtil.Run "iexplore.exe", "http://www.gmail.com"
3 Browser("Gmail: Email from Google").page("Gmail: Email from Google").Sync
4 datatable.SetCurrentRow(i)
5 varName=datatable.value("SignInName")
6 varPwd=datatable.Value("GPassword")
7 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebEdit("Email").Set varName
8 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebEdit("Passwd").SetSecure varPwd
9 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebButton("Sign in").Click
10 Browser("Gmail: Email from Google").Page("Gmail - Inbox").Link("Sign out").Click
11 Browser("Gmail: Email from Google").page("Gmail: Email from Google").Sync
12 Browser("Gmail: Email from Google").Close
13 next

On executing a test that runs for 2 iterations this is how the test results screen will look like:

QTP Parameterization datatable

I would like to spend some time on examining the code and trying to understand why each line and its order is important for the successful execution of the test:

  1. Why am I opening the browser within the ‘for’ loop?
  2. Why are there sync statements everywhere?
  3. Why are we programmatically closing the browser at the end instead of letting the “Record and run settings – Close the browser when test closes” option take care of that for us?
  4. Again, why is the close statement inside the ‘for’ loop?

Please note that in the above piece of code, I did not declare the variables in this test, indent the statements or provide comments. This is deliberate as I did not want to dilute the essence of the statements. The following concept will answer these questions:

State of your AUT:
The basic rule is – Each iteration should begin with the AUT being the same state and ending in the same state.

  • If the statement to open the gmail.com page was outside the for loop, the test would run fine for the first iteration but for the next one the gmail.com page would not have been opened and the test would fail.
  • If the statement to close the browser is not included in the test, then the test would open a browser with each iteration and you would end up with having as many instances of the browser open as the number of rows in the datatable.
  • Imagine if the close statement was outside the for loop, then also you will end up with too many browsers.
  • Sync statement: this forces the QTP test to wait until a certain page loads up completely before it starts performing a certain operation on it.

Always try to return your application to the state where it began. This will make sure that you provide the same interface for each of your iteration to interact with.

The following is the piece of code when you are using a local sheet instead of the global:

1 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebEdit("Email").Set DataTable("Name", dtLocalSheet)
2 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebEdit("Passwd").SetSecure DataTable("Pwd", dtLocalSheet)
3 Browser("Gmail: Email from Google").page("Gmail: Email from Google").WebButton("Sign in").Click

In QTP you can parameterize values of:

  1. Checkpoints.
  2. Object properties for a selected step.
  3. Operation arguments defined for a selected step.
  4. One or more properties of an object stored in the local object repository in the Object Properties dialog box or Object Repository window.
Advertisements

2 responses »

  1. I have been surfing on-line more than three hours lately, but I by nno means found any attention-grabbing article lkke
    yours. It is lovely worth sufficient for me.In my view, if all website owners and
    bloggers made good content as you did, the web can be much more useful than ever
    before.

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: