I am developing a set of automated test scripts for the YouPlayOff webset in QTP, and notice that the script files will take a lot of (may be too much) disk space eventually. I reviewed the default test folder structure in QTP, and found the following problems:
1, Script files in the Test folder can be big:
- An empty Object Repository file takes 192K space (if not empty, will take more space). Each Test folder contains at least 2 actions, i.e., Action0 and Action1, and each action has 1 Object Repository file. As a result, an empty Test folder costs 192K*2=384K space just because the Object Repositories. If there are more actions in the Test folder, the Object repository files will take more space.
- The Excel file, i.e., the Data Table, also takes too much space. Each action use one Excel sheet, and the actions that were called by the main action also has their own Excel sheets. In a complex test, Excel files may take up to 4MB space.
2, Script files in the Test folder are too many:
A simplest QTP test script contains 4 folders, 15 files, and the number of files increases as the number of Action increases.
Due to the above 2 problems, a finished QTP project may take 700-800MB space, and contain thousands of script files. This will make the scripts difficult to maintain and to do the version control, and if we want to transfer the scripts to other computers, it will take a long time.
However, if instead of
- Reuse actions (Action is saved in a folder contains multiple files)
- Use Local Object Repository for each action
- Use Local Excel Sheet (i.e., Data Table) for each action
We
- Reuse functions (Function is a single file)
- Use Shared Object Repository for multiple scripts (Reduce the number of Object Repository files)
- Use Shared Excel File with multiple sheets (Reduce the number of Excel files)
then the number of script files in the QTP project will decrease dramatically, and the size of the QTP project can be decreased to 10MB.
Now, I will explain how to do this in detail:
First, record and modify your script, then save it as “Action xyz”. Open the folder of “Actions xyz”, you will find an Excel file – “Default.xls” for the Data Table information, and there are 2 sub-folders: i.e., Action0 and Action1. Action0 is a default action created by QTP, and Action1 is the real action that created by you. if you are interested in understanding more about the difference between Action0 and Action1, please click here. Within the folder of Action1, you will see a file called as “ObjectRepository.bdb” which is the local ObjectRepository for this Action. Blow is an indication of the layout of the “Action xyz” folder.
- folder “Action xyz”
- folder Action0
- folder Action1
- ObjectRepository.bdb
- other stuff
- Default.xls
- other stuff
Second, in QTP, choose File-New-Function Library, copy the code from “Action xyz” and paste it to this new Function Library, modify it to either Sub or Function. For how to write a Sub and Function, please see here. Save the Function Library as “Sub xyz” or “Function xyz”. Save them as *.qfl, *.vbs, or *.txt doesn’t matter, they will behave the same. Open the folder where “Sub xyz” or “Function xyz” is saved, you will find out, compare to “Action xyz” (which is a folder with multiple files and sub-folders), “Sub xyz” or “Function xyz” is just a single file!
Third, create a driver Action for your project. This action will be the only action for your project, and it will function as the entry point of your project. All the other scripts are saved as either Subs or Functions in the project, and these Subs and Functions will be executed by the driver Action. The driver Action is a normal Action, and it has same layout as “Action xyz”, i.e., 2 sub-folders and some files, but since all the other scripts are saved as single files (*.qfl, *.vbs, or *.txt), so the total disk space used will be decreased a lot! Now you know, one project actually only needs one Action folder! This is probably why we can only open a single Action in QTP.
The script of the Sub, Function, and driver Action may look like this:
“Function xyz.qfl”:
Function function_xyz(inputs)
some statements
function_xyz = some value
some statements
End Function
“Sub xyz.qfl”:
Sub sub_xyz()
some statements
Call function_xyz(inputs)
some statements
End Sub
“driver Action”:
Call sub_xyz()
Forth, associate the Subs and Functions with the driver Action (open Subs, Functions and the driver Action in QTP, right click inside the editing area of the Sub or Function, click the last item in the pop-up menu to associate with the driver Action). Note: if the Subs and Functions are not in the same folder of the driver Action, or the Subs and Functions are in different folders, you should still be able to associate them with the driver Action.
Fifth, set up the Object Repository for the driver Action. Currently, the “ObjectRepository.bdb” file within the Action1 folder in the driver Action folder is 192Kb, i.e., it is empty. Since the Object Repository is empty, the driver Action can not be run. We can use existing Object Repositories from recorded actions to create Object Repository for the driver Action. For example, we recorded ActionX, ActionY, and ActionZ, and have transferred them to SubX, FunctionY, and SubZ. Say the Object Repositories for ActionX, ActionY, and ActionZ are “ObjectRepositoryX.bdb”, “ObjectRepositoryY.bdb” and “ObjectRepositoryZ.bdb”, respectively.
- step1, open ActionX, and choose Resources-Object Repository. From the Object Repository window, choose File-Export Local objects. Export it to “ObjectRepositoryX.tsr”. *.bdb is local Object Repository file, and *.tsr is shared Object Repository file.
- step2, repeat step1 for “ObjectRepositoryY.bdb” and “ObjectRepositoryZ.bdb”, export them to “ObjectRepositoryY.tsr” and “ObjectRepositoryZ.tsr”, respectively.
- step3, open the driver Action, and choose Resources-Object Repository Manager. From the Object Repository Manager window, choose Tools-Object Repository Merge Tool. In the Object Repository – Merge Tool window, choose to merge “ObjectRepositoryX.tsr” and “ObjectRepositoryY.tsr”, and save the merge result as “ObjectRepositoryXY.tsr”.
- step4, repeat step3 for “ObjectRepositoryXY.bdb” and “ObjectRepositoryZ.bdb”. Save the merge result as “ObjectRepositoryXYZ.tsr”.
- step5, move “ObjectRepositoryXYZ.tsr” into the Action1 folder of the driver Action folder, and delete “ObjectRepository.bdb” inside the folder, and rename “ObjectRepositoryXYZ.tsr” as “ObjectRepository.bdb”.
Now, the driver Action can be run properly.
Sixth, combine “Default.xls” files from different recorded actions into one excel file (multiple sheets), and save it under the driver Action’s folder. Modify the scripts of Subs and Functions to cope with the excel file. If you want to import data from the excel file to an array, please see post#9. If you want to export data from array to an excel sheet, please refer to post#16.
Seventh, if there are checkpoints in the recorded Actions, you need to transfer them into normal VBScript, since QTP checkpoints won’t work for Subs nor Functions, i.e., checkpoints created by QTP won’t be recognized by *.qfl, *.vbs, nor *.txt file.
=========================================================
You can download an example folder structure and code by clicking here. In the “Tests_YouPlayOff” folder, the “YouPlayOff” folder is the driver action folder, which you can loaded in QTP as a test. In the Action1, there are only two lines of code:
Call Sub_LoginNonconfirmed()
Call Sub_LoginRedirect()
The “Sub_LoginNonconfirmed()” is saved as a *.qfl file, which can be opened from notepad, or opened in QTP as a function library. This “Sub_LoginNonconfirmed.qfl” is a substitution of the test/action mentioned in post#6, and in this sub, “Sub_Login.qfl” is called.
The “Sub_LoginRedirect()” is also saved as *.qfl file, and it is a substitution of the test/action mentioned in post#5. In the “Sub_LoginNonconfirmed.qfl” file, “Sub_Login.qfl” and 3 functions are called. The 3 functions are “Function_ExcelColumnCount.qfl”, “Function_ExcelRowCount.qfl”, and “Function_ExportArrayToExcel.qfl”, and they are used to import data from Excel “Input.xlsx” to the sub. The Excel is a substitution of the Data Table, and the location of the Excel is defined in “Constant.qfl” which is used in “Sub_LoginNonconfirmed.qfl”.
#5: Use QTP to test Log in page redirect function for YouPlayOff.com
In this blog, let’s test the log in redirect function, i.e., from page A, choose log in, then the borrower should be redirected to page A.
This test is complicated, since I want to test multiple starting pages in one action. I would suggest you to try this first by yourself, and I am pretty sure you will have questions, then you can come back to read this blog and solve your questions one by one.
What scripting skills you can practice or learn from this test:
1, Call an existing action (RunAction);
2, Use the DataTable to test multiple inputs;
3, Difference between Global DataTable and Local DataTable (run multiple iterations);
4, How to define variables (Dim);
5, How to wait until certain page is loaded (Sync);
6, How to navigate to a web-page (Navigate);
7, How to handle URLs which contain question mark (?);
8, How to find certain character is in the string (InStr);
9, How to replace characters in a string (Replace);
10, How to use message box (msgBox);
11, How to identify an object from different pages dynamically (Page(“url:=” & PageURL).Link(“text:=” & SignInText));
12, How to use Select Case;
13, How to add comment in the Expert View (‘);
14, How to add a Checkpoint;
15, How to check according to the value from a DataTabel;
16, In order to start next iteration successfully, you need to reset the test environment at the end of the test.
Global Data Table: (you can also read the data from an Excel file directly, see post#9)
http://testing.youplayoff.com
http://testing.youplayoff.com/register
http://testing.youplayoff.com/Login
http://testing.youplayoff.com/create
http://testing.youplayoff.com/categories
http://testing.youplayoff.com/categories/tree
http://testing.youplayoff.com/categories/popular
http://testing.youplayoff.com/playoffs
http://testing.youplayoff.com/recommended
http://testing.youplayoff.com/recent
http://testing.youplayoff.com/groups
http://testing.youplayoff.com/whatsup
http://testing.youplayoff.com/whatsup?show=2
http://testing.youplayoff.com/whatsup?show=3
http://testing.youplayoff.com/help
http://testing.youplayoff.com/about
http://testing.youplayoff.com/privacy
http://testing.youplayoff.com/terms
http://testing.youplayoff.com/contact
Posted by Jia Qi in Quick Test Professional (QTP)