Sunday, 21 September 2014

QTP - Hybrid framework

In the posts Data driven automation framework , Keyword driven framework,Linear framework you used keywords (names of the functions that the user has created) and have data driven the test to make sure that different users are created at the same time. This is nothing but a hybrid framework.

The combination of any two or more frameworks that we have discussed so far is a hybrid framework.
In my experience, no one framework works effectively for a certain project. Hybrid framework is what gets used most often.
Few important points about frameworks:
  1. Framework is just a solution that worked best in a certain situation but should not be construed as a set of rules to be definitely followed. It should be seen more like guidelines.
  2. There might be many other frameworks in use, we have only listed and explained the common ones
  3. Nomenclature – Different people address their frameworks with different names. So if the names are slightly different from one application to another, it is normal.
  4. Most of the frameworks can be used in conjunction with one another when any one of them cannot provide an all round solution to your testing goals.

QTP - Data Driven automation framework


We are working our way through figuring out how to derive at an automation framework that works best for a certain testing project and also are defining certain frameworks that already exist.
The example that we were using in the previous QTP framework article was creating a new Gmail account.
To start with, we coded the creating of a new account scenario just by recording and play back in a linear fashion. Seeing how it lacked in modularity, readability and reusability we broke it down into functions that would be referenced as keywords moving forward.
We did achieve modularity, readability and reusability through this method but we needed to make the program even more robust so that it can take different sets of values without having to modify the script itself.
That is exactly what we are going to achieve by data driving the tests.

Data Driven automation framework using QTP

Creating multiple Google user accounts is the task we will try to accomplish using this framework.
In the example earlier, we hard coded the first name, last name, user id details etc. into our code while trying to create an account. We will have to separate the code from the data if we have to achieve data driving aspect for this script.
The data should come from a source that is not the program itself.
Typically the data input can be anything:
  1. MS Excel files
  2. Data base
  3. Text files
  4. XML files….etc.
Excel files are the ones that get used most often. The very fact that each action in QTP comes integrated with a data sheet of its own explains why that’s so.
You can use one or more data sources for a single script. The excel sheet that comes with the action can be used or you can use an external excel sheet too. Basically a data sheet can be any relevant external file.
For example:
This is the code that we need to data drive:
Browser("Gmail: Email from Google").Page("GoogleAccounts").WebEdit("FirstName").Set "satish"
Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("LastName").Set "s"
Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("GmailAddress").Set "test"
All the data right now is hardcoded. Let us now see how we can take these values from the datasheet.
Go to the expert view for the statement in QTP and click on the value column for a step. The following window open up:
Data driven framework
Select the parameter option, choose a name for the parameter (this will be the column name in the data sheet) and choose whether you are going to use the global sheet or local sheet (global sheet is available for all the actions in a test, but local sheet is specific to the current action).
For the ‘Name’ field in the screen, QTP provides a default value. The user has an option to keep it the same or change it.
Data driven framework
On clicking OK, a new column gets created in the datatable.
Data driven framework
This is how the data sheet that contains 3 sets of firstname, last name and account id looks like:
Data driven framework
Once parameterized, the code looks like:


Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("FirstName").Set DataTable("G_First_Name", dtGlobalSheet)
Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("LastName").Set DataTable("G_Last_Name", dtGlobalSheet)
Browser("Gmail: Email from Google").Page("Google Accounts").WebEdit("GmailAddress").Set DataTable("gmail_address", dtGlobalSheet)
If we have to create these 3 user IDs with the data in the sheets, we need to have 3 iterations. Iteration is nothing but a test run.
Once the data is set up we will have to instruct QTP on how many times this code needs to run, or how many iterations.
This is how we do it: Go to File->Settings and Run  (click on image to enlarge)
Data driven framework
In the above screen set the iteration properties as required.
Alternately, you can instruct QTP about the iterations programmatically. As always, this allows more control and also more programming skills. So it is really up to the comfort level of the tester to choose either of these methods.
The components in a data driven framework are:
  1. Test script
  2. Data files
  3. Shared Functional library(if exists or could be a linear program)
  4. Object repository (Again, this component will not exist if descriptive programming was used to create objects)
The test results will show a “Passed’ or ‘Failed’ status for each test run.
Apart from the data table that comes by default, we can use any external excel file as an input sheet.

QTP - Keyword driven Framework

How can we make the above linear framework test better? How can we overcome the cons?

Obviously, we need reusability, modularity and readability. Trying to incorporate these features and arriving at an optimum solution is nothing but an attempt at creating a new, more improved framework.
What are the reusable components?
  1. Launching of gmail and arriving at the ‘Google Accounts’ page. This is a given, since validating this page means to first get here. ‘GoTo Google Account” – can be made into a separate function that can be called over and over again.
  2. Enter the details and validating them – You can further break this up into positive and negative blocks to include more level of modularity
  3. Account creation – The final level of validation and accomplishing the task at hand
Once you have arrived here, not only have you identified components that can be called over and over again, but you have also broken you linear program into modules.
Functions:
So far in our series we have not dealt with functions. Functions are nothing but a piece of code that does a certain operations. It accepts input parameters from the program that calls it and returns value to it.
As a general practice all the reusable pieces of code are grouped into a file that contains all the reusable functions. This file is associated as a resource to your QTP test.  Typically a function library can be a file of the type: .vbs, .txt or .qfl
Back to our example – This is how the function library file can be:
Function gotoGoogleAccount()
'Open Gmail
SystemUtil.Run "iexplore.exe", "http://www.gmail.com"
'Page Sync
Browser("Gmail").Page("Gmail").Sync
‘Click on create account
Browser("Gmail").Page("Gmail").WebLink(“Create Account”).Click
‘Enter the details
End Function
Function EnterDetails()
Browser("Gmail").Page("Google Accounts").WebEdit(“First Name”).Set “Swati”
Browser("Gmail").Page("Google Accounts").WebEdit(“Last Name”).Set “test”
‘Fill in several other details
End Function
Function SubmitToCreate()
‘Submit
Browser("Gmail").Page("Google Accounts").WebButton(“Next Step”).click
End Function

Now your actual script will be:
'Open GMail
gotoGoogleAccount()
‘Enter the details
EnterDetails()
‘Submit

SubmitToCreate()
From the above program it is now clear that we have achieved readability, modularity and if in case another program wants to use the login function, we can surely reuse it. All you have to do is associate the function library to that new test too and you are good to go.
You can also see that in your script the function names are functioning as if they are VBScript’s keywords and hence the name for this framework.
The components or test assets in this kind of frameworks are:
  1. Test scripts
  2. Shared OR
  3. Shared function library
Now, what else would make this program even better? If we could make the EnterDetails() function to take different sets of data and create different accounts and not be limited to the data that we hard coded into the program. That exactly is the next step. Data driving your tests and the approach where we do this is the data driven framework.