Monday, December 23, 2013

QTP / UFT - Hybrid Framework

QTP / UFT - Hybrid Framework

Some facts about Hybrid framework:

- Hybrid framework is combination of two or more frameworks (Data Driven, Keyword based, Modular framework etc.).
- In real time, hybrid framework is the one which is used more often.
- Hybrid framework is created by considering the strengths of the frameworks combined to create it.
- Creating Hybrid framework from scratch is complex relatively.

Lets try out the steps to create a hybrid framework:

I created the folder structure as below:



In this framework we are going to use/create one QTP/UFT test case, three function libraries, one Shared Object Repository and one Excel sheet.

GUITest1 will be in testcases folder.
All 3 function libraries and Shared Object Repository will be in resources folder.
Excel sheet will be in datasheet folder.

Record a script to: open an application (Flight Reservation), create an order and close the application.
SystemUtil.Run "D:\HP\samples\flight\app\flight4a.exe"
Dialog("Login").WinEdit("Agent Name:").Set "sach" 
Dialog("Login").WinEdit("Password:").SetSecure "52a0aeb5b390f30995d72ab31728d144b287b781" 
Dialog("Login").WinButton("OK").Click 
Window("Flight Reservation").WinObject("Date of Flight:").Type "101020" 
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver" 
Window("Flight Reservation").WinComboBox("Fly To:").Select "Frankfurt" 
Window("Flight Reservation").WinButton("FLIGHT").Click 
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "order1"
Window("Flight Reservation").WinButton("Insert Order").Click 
Window("Flight Reservation").Close
You can use Shared Object Repository with the script. Add all the objects of an application (Flight Reservation -> [Login dialog, Flight Reservation dialog, Flights Table and Flight Reservation confirmation dialog (which shows up when you click Delete Order button)]) to an Object Repository.

Resources (Menu) -> Object Repository Manager

Save the Shared Object Repository to the resources folder. Associate the Object Repository with the test (Resources (Menu)-> Associate Repositories) and delete all the objects from the Local Object Repository.

Try running the test once to make sure it is fine.

Divide the above code into three different functions as seen below:
function_Login()
function_CreateOrder()
function_CloseApplication()


function function_Login()
SystemUtil.Run "D:\HP\samples\flight\app\flight4a.exe"
Dialog("Login").WinEdit("Agent Name:").Set "sach" 
Dialog("Login").WinEdit("Password:").SetSecure "52a0aeb5b390f30995d72ab31728d144b287b781" 
Dialog("Login").WinButton("OK").Click 
End Function


function function_CreateOrder()
Window("Flight Reservation").WinObject("Date of Flight:").Type "101020" 
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver" 
Window("Flight Reservation").WinComboBox("Fly To:").Select "Frankfurt" 
Window("Flight Reservation").WinButton("FLIGHT").Click 
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "order1"
Window("Flight Reservation").WinButton("Insert Order").Click 
End Function

function function_CloseApplication()
Window("Flight Reservation").close
End Function

Try running the test once to make sure it is fine.

Move the above functions (without first 3 lines: function_Login(), function_CreateOrder(), function_CloseApplication()) from QTP/UFT test to function library named Library1_App.

We can add application independent reusable functions to the script. We will put these application independent reusable functions in a separate function library named Library1_General.

[Couple of benefits of application independent reusable functions: These same application independent reusable functions can be used no matter what application you are testing. These also make error handling and debugging somewhat easier. etc.]

Below you can see both function libraries: Library1_App (after adding reusable functions) and Library1_General. See how we are using ‘fnSetValue’ instead of ‘set’ and ‘fnButtonclick’ instead of ‘click’ etc. in Library1_App. Remember that these fnSetValue, fnButtonclick etc. are defined in Library1_General.

Library1_App
function function_Login()
SystemUtil.Run "D:\HP\samples\flight\app\flight4a.exe"
Dialog("Login").WinEdit("Agent Name:").fnSetValue "username"
Dialog("Login").WinEdit("Password:").fnSetValue "password"
Dialog("Login").WinButton("OK").fnButtonclick
End Function

function function_CreateOrder()
Window("Flight Reservation").WinObject("Date of Flight:").fnType "date"
Window("Flight Reservation").WinComboBox("Fly From:").fnSelectValue "flyfrom"
Window("Flight Reservation").WinComboBox("Fly To:").fnSelectValue "flyto"
Window("Flight Reservation").WinButton("FLIGHT").fnButtonclick
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").fnButtonclick
Window("Flight Reservation").WinEdit("Name:").fnSetValue "name"
Window("Flight Reservation").WinButton("Insert Order").fnButtonclick
End Function

function function_CloseApplication()
Window("Flight Reservation").close
End Function

Library1_General:





For more information on registering and unregistering a function in QTP/UFT see [QTP register / unregister a function].

Associate both above function libraries with test.

In the above code (Library1_App) you will also observe that instead of actual values we are passing "variable names" to the functions like
fnSetValue "username"

Here we are picking up the values (for the order) from an Excel sheet.

Data in the Excel file is as below:



For picking these values from an Excel the code is written in a separate function library (Library1_Excel).



Change the "Excel_WorkbookPath" accordingly.

In Library1_Excel, we are importing the data from Excel in QTP/UFT data table and then fetching the values from the data table.

Now we can create a QTP/UFT test case (Observe that the name of the test case i.e. GUITest1 is same as the name of an Excel sheet from where we need to fetch data for login into the application and creating first order) as below:

GUITest1:
Excel_SheetName = Environment.Value("TestName")
function_Login()
function_CreateOrder()
function_CloseApplication()

Now we have one test case (GUITest1), three function libraries (Library1_App, Library1_General, Library1_Excel), one Excel file with 1 Sheet and a Shared Object Repository. Make sure function libraries are linked and Object Repository is associated to the test.

GUITest1 will be in testcases folder.
All three function libraries and Shared Object Repository will be in resources folder.
Excel sheet will be in datasheet folder.

Run the test.

Now we can create another QTP/UFT test (GUITest2) which opens the application, creates an order, deletes an order and then at last closes the application.

GUITest2:
Excel_SheetName = Environment.Value("TestName")
function_Login()
function_CreateOrder()
function function_DeleteOrder()
function_CloseApplication()

For opening an application, creating an order and closing an application we can use the already defined functions from function library Library1_App.

We will add another function in Library1_App i.e. to delete an order:
function function_DeleteOrder()
Window("Flight Reservation").WinButton("Delete Order").fnButtonclick
Window("Flight Reservation").Dialog("Flight Reservations").WinButton("Yes").fnButtonclick
End Function

Create another sheet with name GUITest2 and its our order no. 2.



Now you have GUITest2. Associate all three function libraries to it. Associate the Shared Object Repository to it. And we have an excel sheet with the same name as test case name GUITest2 ready.

Run it to make sure it works fine.

We can run both GUITest1 and GUITest2 with below code as we have done in [Data Driven Framework].

Dim App 

Set App = CreateObject("QuickTest.Application") 
App.Launch
App.Visible = True

Dim QTP_Tests(2)

QTP_Tests(1) = "D:\testcases\GUITest1"
QTP_Tests(2) = "D:\testcases\GUITest2" 

Set res_obj = CreateObject ("QuickTest.RunResultsOptions")

For i = 1 to UBound(QTP_Tests)

App.Open QTP_Tests(i), True
Set QTP_Test= App.Test
res_obj.ResultsLocation=QTP_Tests(i) & "\Res1"

QTP_Test.Run res_obj, True
QTP_Test.Close
Next
App.Quit

Set res_obj = nothing
Set QTP_Test = nothing
Set App = nothing