QTP Frameworks : Designing QTP Modular Framework from scratch
In the last article, you had seen how to create test scripts using QTP’s Linear Framework approach. The article discussed its advantages & disadvantages and covered the various components used to design the linear framework. We will now move a step further and discuss QTP’s Modular Framework and see how it overcomes some of the shortcomings of the linear framework approach.
What will be covered in this article?
This article will cover the following aspects of QTP Modular Framework –
- a) What is QTP Modular Framework and how is it different from the Linear Approach?
- b) Different components involved in Modular Framework Design.
- c) Designing QTP Modular Framework from scratch.
- d) Advantages and Disadvantages of Modular Framework approach.
- e) Sample Scripts.
Let us discuss each of these aspects in detail.
Why you need QTP Modular Framework?
Before we begin explaining the Modular Framework, let us first revisit the most important disadvantage of linear framework. The most important disadvantage of linear framework is that it lacks re-usability. This lack of re-usability can be classified into 2 types –
a) Lack of Code Re-usability. Since the code in the Linear Framework in written step by step in a linear manner, you can’t really reuse it at different places without copy pasting it. We use modular framework approach to overcome this shortcoming of linear framework.
b) Lack of Data Re-usability. You have this limitation in Linear Framework because the data is hard-coded within your script. Hence we can’t use the same code with multiple data values. You can overcome this shortcoming by using Data Driven approach.
What is QTP Modular Framework?
QTP Modular Framework (also known as Functional Decomposition Framework) is the approach where you first identify the re-usable code from your test cases. Then you write this re-usable code inside different functions and call these functions wherever required. The advantage of this approach is that the re-usable code would always stay at one place and thus it would be easy to maintain the code because you would have to make the changes at a single place only. To re-use this piece of code, all you have to do is call the function wherever required.
The only challenging part here is to identify the reusable portions in your test case flow. Once that is done, you have to just create functions and use it wherever required. Let us see an example which would help you understand this concept more clearly.
Consider this scenario where you have to automate 2 test cases. In the first test case, you have to login to the application, create an order and then logout. In the second test case, you have to again login, then you have to modify an existing order and then logout.
Can you identify the re-usable flows from the above 2 test cases? Yes, from the above test cases we can clearly see that there are 2 different re-usable flows that are used in both these test cases – Login and Logout. Let’s see how the flow would look like in the Modular Framework approach.
'- - - - - Login Function - - - - - Function fnLogin() 'Open Application 'Enter Used Id and password 'Click on Login button 'Verify successful login End Function '- - - - - Logout Function - - - - - Function fnLogout() 'Logout from the application 'Verify Logout is successful 'Close the application End Function '===== Test Case 1 - Create Order ===== 'Call Login function fnLogin() 'Code to create order ' .......... ' .......... ' .......... 'Call Logout function fnLogout() '===== Test Case 2 - Modify Order ===== 'Call Login function fnLogin() 'Code to modify order ' .......... ' .......... ' .......... 'Call Logout function fnLogout()
Different Components involved in QTP Modular Framework
The below image shows the different components that you would mostly use in a Modular or Functional Decomposition Framework.
a) Object Repository: You can use a shared object repository that would store the objects and its properties. You can avoid using this component if you use Descriptive Programming to write your test scripts.
b) Function Library: This would be an external file which would contain all the re-usable functions like Login, Logout etc. This external file can have .vbs, .txt or .qfl extension.
c) Test Scripts: You can have multiple test scripts which would cater to different test cases. You have to associate or map each of your test cases to the function library so that the test cases can call the functions from the function library.
Designing QTP Modular Framework from scratch
Below are few generic points that would assist you to design a basic Modular Framework and then build it up from there.
Step 1) Analyze Manual Test Cases and identify the reusable functions/ flows. Before you actually start building your modular framework, the first task that you should ideally do is to analyze your manual test cases (that have been identified for automation) and note down all the flows that you think are reusable.
The reason why this is important is that once you have the list of reusable flows ready with you, you would script for the flow keeping the re-usability factor in mind. That is, you would know that this flow is reusable and thus you would try to make it as generic as possible so that it can be re-used with other test cases without much changes.
If you don’t do this activity then there is a possibility that you would write the flow as it is in the test case (i.e. you might not create a function for it). Later when you see that the same flow is being used re-used in some other test case, you would then need to change the previous test cases also where you wrote the code as it is. All this will lead to wastage of time for modifying the scripts.
Example (Sample Scenarios): As part of designing the Modular Framework from scratch, we will take a couple of scenarios from GMail which we will automate using the Modular Framework approach.
Test Case 1: Login to Gmail >> Find the number of mails you received today >> Logout from Gmail.
Test Case 2: Login to Gmail >> Find the number of unread emails in the inbox >> Logout from Gmail.
As part of QTP Modular Framework design, we will create 2 test scripts in QTP for the above mentioned scenarios. As you have seen above, the first task that you should do is to identify the re-usable flows from the test cases identified for automation.
From the above scenarios, you can clearly identify 2 reusable flows – Login to Gmail and Logout from Gmail. which will be used in both the test cases. Once you have identified all the reusable flows, you can now move over to the next step shown below.
Step 2) Writing the first Test Script and creating Re-usable Functions: Now, the task is to write your first script in QTP which should include the reusable flows (Login and Logout flows in this case). A very simple process of doing this task is shown below.
a) First of all write the entire test script in QTP as if you were writing a test script in Linear QTP Framework design. So you will have all the code (login, checking for mails received today and logout) written step by step one after the other.
b) In case the test script is very big with lots of lines of code, you can write a few lines of code and then unit test it to verify that this chunk of code is working fine. Once you have confirmed that this code doesn’t contain any errors, you can start writing the next set of code.
c) Run the entire flow and see if it is running fine without any errors. This step would ensure that the logic you have scripted is working fine.
d) Now is the time when you create the re-usable functions. Since you have already written the code, just create a new function inside the action and cut paste the code into it (see the below image for illustration). That is, you create a function named fnLogin() and cut-paste the login code inside this function. Do the same for other re-usable flows in the test case (Logout in this example).
e) Call the re-usable function in appropriate places (places from where you cut the code).
f) At this point, your test case is written according to modular framework approach (with re-usable function calls). Run the test script and make sure that your code with function calls works fine.
NOTE 1: One important point to note here is that the you are saving the reusable function in the same test script only. With this method, these re-usable functions will not be available to the other test scripts. So we have to store these functions in some external file and map (or associate) this function library to all the test cases that want to use the re-usable functions.
g) Create a new function library. Cut-paste the re-usable functions from your action into this new function library. Save the function library and associate it with your test script. (Read the 4th method on how to associate function library to a QTP script)
h) Now at this stage your re-usable functions are getting called from the function library and test case specific code is available in the test case action. Run this test case now and make sure that it runs correctly.
You have now written a test script using QTP Modular Framework approach. :–) Check the below image which would help you understand the above steps (points a to h) more clearly.
Step 3: Writing more test scripts and updating the function library in the process. Since you have already written the first test script, writing the remaining ones is a very straightforward task. You will mostly be following the points that we had covered in Step 2. Lets briefly see what you have to do here.
a) Create a new test case in QTP. And associate the shared function library (created in step 2g) to this test case.
b) In this test case we have to count the number of unread mails in the inbox. Since login function has already been created, just call this function in your test case. (At this point, you might want to run the test case and see that the login functionality is working fine for this test case also).
c) After calling the login function, write the code to count the number of unread mails. Then call the logout function.
d) Run your test script and see that it should work fine without any issues.
NOTE 2: Since in this test script, we had to only use the pre-created functions (login and logout), we just called this functions from the shared function library. If you are required to create some other reusable function in this test case, you can do it the same way as discussed in step 2 (writing it in test script first, then creating a function and saving it in the test script itself, and finally adding this function in the function library).
You can use the above mentioned approach (in step 2 and step 3) to write all your test scripts according in QTP modular framework design.
NOTE 3: To create a reusable function, we have advised that you first add it in the main script then move it to the function library once it is working fine from the main test case. This approach is suggested because it is easier to create and debug functions from the main script due to intellisense and other such options which assist you in scripting. Once you are familiar and comfortable with creating functions, you can directly start writing them in the function library itself.
QTP Modular Framework – Advantages and Disadvantages
- a) The amount of time spent on coding is lesser as you are creating re-usable functions and calling them wherever required.
- b) Script maintenance is relatively easier because the re-usable code is available at a single place only no matter how many times it is called. If there is a change in any re-usable function, you would be required to make the changes at only one place.
- a) Additional time spent in analyzing the manual test cases to find out the reusable flows. This is not required in linear framework.
- b) Users need more technical expertise in automation to work on modular frameworks.
- c) In case you use only modular framework, the data will still be hard coded in the script only. So you can’t use the same test script for multiple data without changing the values before each run.
NOTE 4: The main issues with linear framework were lack of code re-usability and data re-usability. QTP Modular Framework solves only one of these limitations. The other limitation (data re-usability) still looms large. So, you cannot use QTP modular framework alone in many of your automation projects (especially if your test cases need to be validated against multiple sets of data). To work on any real life automation project, you should really be looking at using a combination of QTP modular framework + data driven framework.
Whats your thought on this article? Do you see any other components that can be added here? Let us know your comments in the comments section..