site
stats
By Anish Pillai Anish Pillai Posted under Advanced Concepts | QTP Concepts | QTP Framework

Designing Keyword Driven Framework mapped at Functional Level – Part 2

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Till now, we have covered many aspects of the Keyword Driven Framework in detail. This would be the last article of the Keyword Driven Framework series. In this article, we will see the actual implementation of the Keyword Driven Framework mapped at functional level (for more information about the design aspects of this framework, refer the previous article from this link – Keyword Driven Framework mapped at Functional Level – Part 1).

UPDATES

1) You are free to download and play around with the code used for this framework. The download link is available at the end of the article.

2) Just like this, we have written more articles on various other QTP Frameworks. If you wish to have a look at those, please visit QTP Framework main page. The bottom of the page contains links to other QTP Frameworks.


Topics that will be covered in this article

We will be covering the following topics in this article –

1) Identifying the components that will be used in this framework.

2) Identifying the test cases that will be automated as part of this framework.

3) Describing the process which would be used to write test scripts in this framework.

4) Enhancements/Improvement points for the framework.

Let us now see each of these topics one by one.

Components that will be used in this Framework

Following are the components that we will use to create this framework from scratch.

1) QTP Test Scripts (or QTP Actions): As part of this framework creation, we will be automating 3 test cases. And for each of these test cases, we will create a separate QTP Test Script.

2) Function Library: There will be 2 function libraries that will be created for this framework. The first function library will contain all the functions that are related only to the framework (and not to the application). The second function library will contain the functions that will contain the functions related to the application.

3) Object Repository: For this framework, we will use the Object Repository to store the object properties. Descriptive Programming will not be used anywhere in this framework.

4) Excel Sheet to Store Test Case Flow: We will be using a single excel workbook (and a single excel sheet in the workbook) to store the flow of all the 3 test cases.

Above mentioned are the 4 major components that will be used in this automation framework. Please note that the data for the test cases will be hard-coded in the test scripts only. Separate excel sheets will not be created to store data. The below figure shows the components and their flow.

Components of Keyword Driven Framework


Test Cases that will be automated for this Framework

For this framework, we will be working on the same test cases that were automated for Keyword Driven Framework mapped at Operation Level. The 3 test cases are –

1) TC_01_CreateOrder: For this test case, we will write a script that will login to Mercury Flight Reservation system and then create a new flight reservation.

2) TC_02_ModifyOrder: In this test case, the script will log in to the application, open an existing order and then modify the order.

3) TC_03_DeleteOrder: Here, the script will again log in to the application, open an existing order and then delete the order.

Step By Step Method to Automate the First Test Script

In this article, we will cover the creation of first test script in detail. And this test script will be created as per the framework specifications. Once you understand the underlying concepts used for creating the first script, you can use the same concepts to script the remaining test cases.

The below image depicts the basic steps which we will use to script the first test case from scratch.

Keyword Driven Framework - Test Script Creation Flow

Let us now cover each of these points in detail.


Step 1: Creating the First Test Script in QTP in Linear Manner

The first step would be to create the entire test script in the normal way (i.e. in the linear manner). For this, you can follow the steps mentioned below.

Step 1A: Open a new Test Case in QTP.

Step 1B: Navigate to Resources -> Object Repository Manager to open a new Object Repository.

Step 1C: Open the Flight Reservation Application (windows version) from Start -> All Programs -> QuickTest Professional -> Sample Applications -> Flight (this path may differ depending upon the OS and QTP Versions)

Step 1D: Add all the necessary objects (that are required for order creation) in the Object Repository.

Step 1E: Associate the Object Repository with the Test Script.

Step 1F: Once the Object Repository has been associated with the Test Script, write the script to insert a new order in the flight reservation system.

Step 1G: Run the script to verify that the code is working fine.

The code you have written to insert a new order in Flight Reservation System will look similar to the code shown below.

'===== Code to Login to the Application =====
SystemUtil.Run "C:\Program Files (x86)\HP\QuickTest Professional\samples\flight\app\flight4b.exe"
Dialog("Login").WinEdit("AgenName").Set "anish"
Dialog("Login").WinEdit("Password").Set "mercury"
Dialog("Login").WinButton("OK").Click

'===== Code to Insert a New Order =====
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "%(fn)" 'Press Alt F+N to open new order screen
Window("Flight Reservation").ActiveX("DateOfFlight").Type "010114"
Window("Flight Reservation").WinComboBox("FlyFrom").Select "Paris"
Window("Flight Reservation").WinComboBox("FlyTo").Select "Sydney"
Window("Flight Reservation").WinButton("Flights").Click
'Select any Flight
Window("Flight Reservation").Dialog("FlightsTable").WinButton("OK").Click
'Provide Name and Insert Order
Window("Flight Reservation").WinEdit("Name").Set "Anish"
Window("Flight Reservation").WinButton("InsertOrder").Click

'===== Logout from Application =====
Window("Flight Reservation").Close

If your code to Insert New Order in Flight Reservation Application is running fine, then you have accomplished the first task. You now have the basic script that can insert a new order. Now all your remaining steps would focus on how to modify this script and fit it into the framework.

Step 2: Dividing the Script into Different Functions and calling them from the Test Script

In the previous step, you had created the test script in linear fashion. Now in this step, you would need to convert the linear flow into different functions and then call the same from the test script. To do this, you have to follow the steps mentioned below –

Step 2A: First Step is to identify the functions which should be created from the linear flow. One look at the test case, and you would get to know that the test case can be divided into 3 flows or functions. These are – Login, Insert Order and Logout.

Step 2B: Create 3 different functions in the Test Case and then cut paste the appropriate code in all the 3 functions. Example, the Login function will contain the code to login to the application.

Step 3B: Once all the functions have been created, call these functions from the test script itself. The code in the QTP test script should look something like this –

' - - Test Case Flow - -
fnLogin()
fnInsertOrder()
fnLogout()

'========================================
'   Function Name - fnLogin
'   Purpose - This function is used to login to flight reservation application
'========================================
Function fnLogin()

	SystemUtil.Run "C:\Program Files (x86)\HP\QuickTest Professional\samples\flight\app\flight4b.exe"
	Dialog("Login").WinEdit("AgenName").Set "anish"
	Dialog("Login").WinEdit("Password").Set "mercury"
	Dialog("Login").WinButton("OK").Click

End Function

'========================================
'   Function Name - fnInsertOrder
'   Purpose - This function inserts a new order in flight reservation application
'========================================
Function fnInsertOrder()

	Set WshShell = CreateObject("WScript.Shell")
	WshShell.SendKeys "%(fn)" 'Press Alt F+N to open a new order screen
	Window("Flight Reservation").ActiveX("DateOfFlight").Type "010114"
	Window("Flight Reservation").WinComboBox("FlyFrom").Select "Paris"
	Window("Flight Reservation").WinComboBox("FlyTo").Select "Sydney"
	Window("Flight Reservation").WinButton("Flights").Click
	'Select any Flight
	Window("Flight Reservation").Dialog("FlightsTable").WinButton("OK").Click
	'Provide Name and Insert Order
	Window("Flight Reservation").WinEdit("Name").Set "Anish"
	Window("Flight Reservation").WinButton("InsertOrder").Click

End Function

'========================================
'   Function Name - fnLogout
'   Purpose - This function is used to logout from the flight reservation application
'========================================
Function fnLogout()

	Window("Flight Reservation").Close

End Function

Step 2D: Run the code to verify that it is working fine.

In the beginning of the article, we had mentioned that we will be creating 2 function libraries in this framework. And one of these function libraries will be used to store the test case flow. So now, we will create a function library and then move the functions from the test script to this function library. This will be the function library that will contain the application related code.

Step 2E: Create a new function library and save it at an appropriate location.

Step 2F: Associate this function library to the QTP Test Script.

Step 2G: Cut paste the functions from the test script to the function library.

Now at this step, the function library will contain all the 3 functions and the test script will contain the function calls for all these 3 functions. The code in the test script and the function library will look something like this –

* * * * * Code in QTP Test Script * * * * *

fnLogin()
fnInsertOrder()
fnLogout()


* * * * * Code in Application Related Function Library * * * * *

'========================================
'   Function Name - fnLogin
'   Purpose - This function is used to login to flight reservation application
'========================================
Function fnLogin()

	SystemUtil.Run "C:\Program Files (x86)\HP\QuickTest Professional\samples\flight\app\flight4b.exe"
	Dialog("Login").WinEdit("AgenName").Set "anish"
	Dialog("Login").WinEdit("Password").Set "mercury"
	Dialog("Login").WinButton("OK").Click

End Function

'========================================
'   Function Name - fnInsertOrder
'   Purpose - This function inserts a new order in flight reservation application
'========================================
Function fnInsertOrder()

	Set WshShell = CreateObject("WScript.Shell")
	WshShell.SendKeys "%(fn)" 'Press Alt F+N to open a new order screen
	Window("Flight Reservation").ActiveX("DateOfFlight").Type "010114"
	Window("Flight Reservation").WinComboBox("FlyFrom").Select "Paris"
	Window("Flight Reservation").WinComboBox("FlyTo").Select "Sydney"
	Window("Flight Reservation").WinButton("Flights").Click
	'Select any Flight
	Window("Flight Reservation").Dialog("FlightsTable").WinButton("OK").Click
	'Provide Name and Insert Order
	Window("Flight Reservation").WinEdit("Name").Set "Anish"
	Window("Flight Reservation").WinButton("InsertOrder").Click

End Function

'========================================
'   Function Name - fnLogout
'   Purpose - This function is used to logout from the flight reservation application
'========================================
Function fnLogout()

	Window("Flight Reservation").Close

End Function

Step 2H: Run the code and make sure that everything runs fine.

Once this step is finished successfully, you will have the script that is made up of separate functions. At this point, you can see that the flow of the test case is defined in the QTP Test Script. (All the functions are being called from the QTP Test Script and therefore the flow of the test cases is available in the QTP test script only)

The next step would be to create a Test Case Flow excel sheet where we will add all the keywords associated with the test case. By doing this, the flow of the test case will be dictated by the test case flow excel sheet. Let’s see how this can be done.

Step 3: Creating a Test Case Flow Excel Sheet and mapping it with the Test Script

In this step, we will create a test case flow excel sheet and then add the keywords that have been identified for the test case. Then we will write code that will read the keywords from the excel sheet call the functions associated with the test cases. You can follow the below mentioned steps to perform the above task.

Step 3A: The first step is to identify the keywords that will be associated with our test case. Looking at the functions that we have come up with in the previous step, we can have the following keywords which we can associate with the script –

  • login – To login to the application
  • InsOrd – To insert a new order into the application
  • logout – To logout from the application

Step 3B: Once the keywords have been identified, the next step is to add the keywords in the excel sheet. Create a new excel sheet and add keywords to it as shown in the below image. Save the excel sheet by giving some appropriate name.

Keyword Driven Framework - Test Case Flow

Now we have to write the code that will fetch the keywords from the excel sheet and call the associated function. The code to fetch open the excel sheet and fetch the keywords is the generic code that is independent of the application. This is because even if you have some other application, the only change here would be the name of the test case, the keywords and the description.

No matter what the keywords are, the code to fetch them would remain the same. So, the functions that we would write here would go to a separate function library (framework related function library). This way you would be able to keep the generic and application specific code separate.

Step 3C: Create a new function library where you will store the framework specific code. For this framework, we will name this function library as ‘FrameworkRelatedFunctionLibrary’. Associate this function library with the test script.

Step 3D: You have to now write the function that will read the keywords from the excel sheet. Below is the sample code that shows how this can be done.

'========================================
'   Function Name - fnReadKeywords
'   Purpose - This function reads the keywords that are associated to a given test case
'========================================
Function fnReadKeywords(sTCName)

	'Add a new Sheet into QTP Data Table
	DataTable.AddSheet("dtTCFlow")

	'Import the Excel Sheet into QTP Data Table
	DataTable.ImportSheet sExcelLocation, "TestCaseFlowSheet", "dtTCFlow"

	'Loop through all the rows in the Data Sheet
	iRow = DataTable.GetSheet("dtTCFlow").GetRowCount

	For iR=1 to iRow
			'Set the Current Row in the Data Sheet according to the loop counter
			DataTable.SetCurrentRow iR

			'Capture the Keyword based upon the test case name
			If DataTable("TestCaseName", "dtTCFlow") = sTCName Then
					'Call the executeFlow function that will execute the function associated to the keyword
					 executeFlow DataTable("TestCaseFlow", "dtTCFlow")
			End If
	Next

End Function

The above function opens the excel sheet, loads its contents into QTP Datatable and then reads the keywords that are associated to the given test case. Once it identifies the keyword, it passes the keyword to a different function which then executes the actual function associated with this keyword.

Step 3E: The next step is to map the keywords with the actual flow functions so that whenever a keyword is encountered, the matching function can be executed. We will create a separate function where will define the mapping in a Select Case statement. Since this function is dependent on the application, we will store this function in the application related function library. Below you can see how this function has been implemented for the first test case.

'========================================
'   Function Name - executeFlow
'   Purpose - This function executes the functions that are associated to the given keyword
'========================================
Function executeFlow(flowName)

	'Call the function associated with the keyword
	Select Case flowName
		Case "login"  fnLogin()
		Case "insOrd" fnInsertOrder()
		Case "logout" fnLogout()
	End Select

End Function

Now the final part that is pending is to call the above functions from the test script. If you recall, the main test script currently contains the flow of the test case. But now, since the flow has been defined in the test case flow excel sheet, we can remove the previous function calls from the main test script.

We can now just call the fnReadKeywords() function from the main script. Once the function has been called it will do its job and execute all the functions that are associated with the keyword.

Step 3F: Open the main QTP Test Script. Delete the function calls that are written there and write the new code that will call the fnReadKeywords() function and pass the test case name as the parameter. The code for that will look similar to the one shown below.

sTestCaseName = Environment.Value("TestName")
fnReadKeywords sTestCaseName

Step 3G: Execute the test case and see if it is running fine.

Once you verify that the test case is running fine, you will have with you the working model of a basic Keyword Driven Framework where the keywords are mapped at function level. Now you can easily add more test cases to this framework by following the steps mentioned above.

The entire framework together with the function library and flow for all the 3 test cases is available in the sample framework. You can download this framework (link available near the bottom of the article) and play around with the code.

Improvements in the Framework

As mentioned in the beginning of the article, this is a very basic framework that will just contain the basic things to help you understand the concepts easily. You can try to improve this framework by adding more features to it. This would make the framework more robust and easy to use and maintain. This would also work as a practice exercise for you and would help you learn more stuff.

Please note that these changes or improvements are not available in the attached framework. This is just a practice exercise which you can try from your end.

Improvement 1) Currently, the test case flow just contains the TestCaseName, Keywords and Description only. You can try to add an additional Column where you can store the result as ‘Pass’ or ‘Fail’ for each keyword. You would also need to write some code which after executing every function, will update this column as ‘Pass’ or ‘Fail’. This will help you keep a track of the results easily.

Improvement 2) You can add one more additional column which will tell QTP whether the particular flow has to be executed or not. The column can have values as ‘Yes’ or ‘No’. Once QTP encounters a keyword, it will first check this column and then execute the function associated with the keyword only when the column contains the value ‘Yes’.

After implementing the above 2 enhancement points, the test case flow excel sheet would look something like this.

Keyword Driven Framework - Test Case Flow Excel Sheet

Improvement 3) You would also have observed that the data is hard-coded with the flow. Because of this, if you want to run the flow for multiple data, you would need to change the data values before each run. To avoid this, you can try to separate the data from the flow by adding it to a separate excel sheet. This way you would be able to change the data without touching the flow and you can also re-run the same flow multiple times by using a loop.

Try out these changes and let me know if you face any issues in-between. If you have any points to add here, or if you have any other improvement which can be added in this framework, please let us know about it using the comments section.



If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.

Visit QTP Frameworks Main Page for more articles on QTP Frameworks. You can also visit our QTP Tutorials page for more QTP Tutorials.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • saju

    best article I have ever seen.no words to price. expecting information on hybrid framework.kalakki aliya…..

  • saju

    you are really great to have this much knowledge in 2 years

  • saju

    please pour more knowledge:)

  • saju

    am your great fan now.u deserve that.expecting more information on hybrid framework:)

    • Anish10110

      Thank you Saju.. 🙂

      I will soon start working on the Hybrid Framework and will upload the articles once its done.. 🙂

  • MSREDDY

    Hi Anish, could you pls post, describing the enhancements?

    • Anish10110

      Hi MSReddy,

      I would be working on Hybrid Framework and most of the improvements would be covered as part of that framework only. Till that time, you can try to implement and these features on your own. Let me know if you face any issues with anything.

      • MSREDDY

        Thank you ANISH. I'll try on my own, let you know If I face any problems. I feel much lucky to get in touch with a good guy like you. Thanks again

  • MSREDDY

    Hi Anish, could you pls let me know the code relating for the extra column labelled Execute and how to update the result status Pass / Fail. PLS, I'm completely new to the coding part.

    • Anish10110

      Sent you couple of files which has the code.

      • nai

        can you please send me too with the extra columns and call it from the test or function file?

        • Anish10110

          Its not the direct code. But the code contains the logic. You would need to go through it and see how its implemented. Then you an implement the same in your code. Mailed you the code.

          • kishore

            Hi Anish,

            For this, we can store the result based on the LastRunResult in the run time data table and then we can export and save the runtime table with the same name as the original excel file we imported….

            Is this approach correct?Otherwise, please correct me..

            Thanks
            Kishore

          • Anish10110

            Hi Kishore,

            This method is also correct. Btw, what I feel is there's no correct or incorrect method. If it does the job, then its good. However, the efficiency of the code can be debated. What do you think about this?

          • kishore

            Hi Anish,

            I have realised we need to set the qtp results object and then need to get the lastrunresults from that. But when we launch the tests in the way described in this article, how do we capture the results? Either we need to capture it from XML results file that will be generated for each test run? Please share the code snippets you have so that I will take a look…Regarding efficiency, I feel, it is better to get the result from the XML file instead of creating the results objects..

          • Anish10110

            Yeah.. This could be tricky…

            Why not try one more method.. At the end of every function (business flow) you can have a check to see, if there was any error in the function. This can be done by capturing the err.number property. This way, whenever you get a non-zero value, you can update that particular business flow as fail in the excel sheet. If the number is 0, then you can update as pass.

            Also, one better way would be to directly update the excel sheet (rather than exporting from data table). This is because, if you want to store in data table, and then export in the end, there will be a possibility that you will loose all the data in case your script fails just near the end of the test case. But if you directly update the excel as and when a flow finishes, you would have updated the excel in different intervals. So if the test fails near the end, you would still have the pass/fail result of most of the business flows. This way you would be able to track the results in a better way.

  • Aruna

    Hi Anish,

    can u plz send me the code to update the result status in xls thru mail? for the reference?

    Thanks,
    Aruna

  • Shanthi

    I searched so much for detailed explanation with eg, but couldn't find anything that will really give me an insight. All the sites had high level info only.
    This site was the only one which had step by step exp any new QTP user can follow. Great work. I needed info in short period of time and would have struggled without ur article.
    Thanks
    Shanthi

    • Anish10110

      thanks Shanti.. 🙂

  • Ravindra Sharma

    Hi Anish,
    Could you please send the code to update the results in excel sheet.

  • Surya

    Hi Anish,

    Thanks for the wonderful post. There is lot of clarity in explanation and easy to understand for the newbies also. It would be really helpful for freshers if you can explain the function to read the keywords in more detail. That function is bit complex for me.

    Thanks,
    Surya

  • Pingback: QTP Framework Tutorials - Framework Types, Examples & Code Snippets - Automation Repository()

  • Pingback: razor()

  • Abhi vijay

    hi Anish,

    Best , Easy Understandable and impressive article .
    Anishi I want to learn coding part of framework function in depth. so please suggest me any book regarding this.
    and if possible ,please post me more complex code on my-email,

    Thanks,
    Ankit Vijay

  • Srini

    In Improvement 3 "change the data without touching the flow and you can also re-run the same flow multiple times by using a loop" How can i implement this .. can u pls gv me a sample code..

  • Santosh

    Hi Anish, Wonderful post, Thanks.
    I need one favor Anish from you. Can you please advice me when a Framework designer who have task to develop a complete regression pack, should go for Operation level or function level keyword framework..? some advantage over other ? memory issue ? maintenance ? future expansion?

    Please advice!!

    • Hi Santosh,

      I would almost always go with functional level keyword framework. The main reason for that is it easier to maintain. The operational level one becomes complex and it becomes more difficult to handle when you increase the number of test cases..

      Functional level keyword framework is better in terms of maintenance and future expansion..

  • Suresh

    Hi Anish,

    The way you explained the keyword driven framework is excellent.
    Could you please send me the code for describing improvement logic

    • Hi Suresh, I haven't implemented the code containing the improvements. The only reason I added the points was to provide some ideas to users…

    • Sandip Kumar

      Hi Anish,
      wonderful post very helpful understanding basic logic of keyword driven framework and why are we using this.Ton of thanks.

  • naseem

    Hi Anish
    Your on Frame work is awesome, it really help new people like me alot. i request you to send me the code to update the result status in xls thru mail?.i will be very helpful to me. Thanks a ton.

  • Veen

    Hi Anish,

    Excellent article that has helped me a lot in creating an apt framework.. thanks a ton.
    Could you please help me with the coding part that deals with the PASS/FAIL and the results column.

    Also,
    Could you please elaborate this section in detail :
    "Open the main QTP Test Script. Delete the function calls that are
    written there and write the new code that will call the fnReadKeywords()
    function and pass the test case name as the parameter. The code for
    that will look similar to the one shown below.
    1
    2sTestCaseName = Environment.Value("TestName")
    fnReadKeywords sTestCaseName"
    Thanks,
    Veena Varghese

  • newbieQTP

    Hi Anish,

    first of all thanks a lot for putting up so much of efforts in writing down such a detailed explanation for designing frameworks…Really beneficial for beginners like me…

    I was trying running keyword framework(functional level).. but i am getting
    Type mismatch: 'fnExecuteTestCase'
    Line (2): "fnExecuteTestCase sTestCaseName".

    Please anyone can suggest where I am getting it wrong .. any syntax error or if there is anything else i missed out…thanks

    • Deepak

      Please use this

      Function fnExecuteTestCase(sTCName)

      fnReadKeywords(sTCName)

      End Function

  • sarath

    Hi Anish,
    One small query, in this framework we need to pass the data for creating order. Generally it will not be static. We need to select different row in excel depend on the testcase. How to linkup this? How can we provide inteligence to script to fetch data from specific row.
    thanks,
    sarath

  • Deepak

    Dear Anish ., thank you very much. The kind of help you are doing is hugely valuable. The explanations of the key word driven frame work and all the other articles are of superb quality. Hats off to you Sir…. People like you builds well developed India.

  • Sunil

    HI Anish,

    Thank you so much for your wonderful QTP tutorial.

  • Sugam

    Hi Anish,
    Your article is really good and helpful too. Could you please send me code for creating results after execution in excel and chart forms. I mean for marking the status of test cases as Passed or Failed.
    Thanks,
    Sgupta

  • Lobo Varghese

    Article is just awesome…. great work for beginners.
    Helped a lot to understand the work flow.

    • Thanks.. Glad that you found it useful.. 🙂

  • Alex

    Very clear!! 🙂

  • AutomationTester

    Hi,
    can i know how can you handle dynamic data displayed based on some search criteria as set of rows in rows,column fashion? Because if i want to click on any row then i need to know at runtime which row to click.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×