To start with QTP, we must create a simple basic test with QTP, motive of this test is to perform addition function on calculator and check weather the outcome/actual result is equal to the expected result. There are some steps that we should follow while creating any basic test.
To create OR
1.1 Go to Menu item Resources -> Object repository.
1.2 Click on "Add object to local" button -> Cursor will change to "hand" icon, click the cursor on Calculator on the top of task bar (It will add all the objects of Calculator at once, if we click on any object then only 1 object will be added in the OR, adding all the objects at once is recommended.)
1.3 Press Ok -> Default objects only -> Ok to add all the objects in OR.
Step2. Perform the action. (You may record the action like in the example we are taking Start recording -> Click 4 button -> Click + button -> Click Click 6 button -> Click = button on calculator) to perform action 4+6 on calculator.
' Record the action
Window("Calculator").Activate
Window("Calculator").WinButton("C").Click
Window("Calculator").WinButton("4").Click
Window("Calculator").WinButton("+").Click
Window("Calculator").WinButton("6").Click
Window("Calculator").WinButton("=").Click
Step 3. Read the actual result value from the object where result is being shown.
Tip : If you do not know the name of the object use "Locate in repository: functionality in Object Repository.
Use "GetROProperty" to read the value from the text box.
strValue=Window("Calculator").WinEdit("Edit").GetROProperty("text")
Keyword view can also be use to used to generate above code.
Step 4. Compare actual result and expected result.
Step 5 Post the result using "reporter.ReportEvent"
If strValue= "10. "Then
reporter.ReportEvent micPass,"add", "pass"
Else
reporter.ReportEvent micFail,"Fail", "Expected value is 10. and the actual result is " & strValue
End If
Step 6. View result in Menu -> Automation -> Results.
' Record the action
Window(" Calculator").Activate
Window("Calculator").WinButton("C").Click
Window("Calculator").WinButton("4").Click
Window("Calculator").WinButton("+").Click
Window("Calculator").WinButton("6").Click
Window("Calculator").WinButton("=").Click
' Read result
strValue=Window("Calculator").WinEdit("Edit").GetROProperty("text")
msgbox strValue
' Compare the result
If strValue= "11. "Then
'msgbox "pass"
' Report the result
reporter.ReportEvent micPass,"add", "pass"
Else
' msgbox "Fail"
reporter.ReportEvent micFail,"Fail", "Expected value is 11. and the actual result is " & strValue
End If
Test Name : Basic test for addition of Calculator
Step 1. Always create complete object repository as the first step while creating any test.
You may go to the following link to go through the basics of QTP to start, please check this
To create OR
1.1 Go to Menu item Resources -> Object repository.
1.2 Click on "Add object to local" button -> Cursor will change to "hand" icon, click the cursor on Calculator on the top of task bar (It will add all the objects of Calculator at once, if we click on any object then only 1 object will be added in the OR, adding all the objects at once is recommended.)
1.3 Press Ok -> Default objects only -> Ok to add all the objects in OR.
Step2. Perform the action. (You may record the action like in the example we are taking Start recording -> Click 4 button -> Click + button -> Click Click 6 button -> Click = button on calculator) to perform action 4+6 on calculator.
' Record the action
Window("Calculator").Activate
Window("Calculator").WinButton("C").Click
Window("Calculator").WinButton("4").Click
Window("Calculator").WinButton("+").Click
Window("Calculator").WinButton("6").Click
Window("Calculator").WinButton("=").Click
Step 3. Read the actual result value from the object where result is being shown.
Tip : If you do not know the name of the object use "Locate in repository: functionality in Object Repository.
Use "GetROProperty" to read the value from the text box.
strValue=Window("Calculator").WinEdit("Edit").GetROProperty("text")
Keyword view can also be use to used to generate above code.
Step 4. Compare actual result and expected result.
Step 5 Post the result using "reporter.ReportEvent"
If strValue= "10. "Then
reporter.ReportEvent micPass,"add", "pass"
Else
reporter.ReportEvent micFail,"Fail", "Expected value is 10. and the actual result is " & strValue
End If
Step 6. View result in Menu -> Automation -> Results.
******************Complete Code********************
Window(" Calculator").Activate
Window("Calculator").WinButton("C").Click
Window("Calculator").WinButton("4").Click
Window("Calculator").WinButton("+").Click
Window("Calculator").WinButton("6").Click
Window("Calculator").WinButton("=").Click
' Read result
strValue=Window("Calculator").WinEdit("Edit").GetROProperty("text")
msgbox strValue
' Compare the result
If strValue= "11. "Then
'msgbox "pass"
' Report the result
reporter.ReportEvent micPass,"add", "pass"
Else
' msgbox "Fail"
reporter.ReportEvent micFail,"Fail", "Expected value is 11. and the actual result is " & strValue
End If
********************************************************