infotek solutions inc. - placement · infotek solutions inc. qtp keyword based framework in this...
TRANSCRIPT
Infotek Solutions Inc.
QTP keyword based Framework
In this doc we will create a small framework. With help of this framework, we will be run two
tests with help of keywords
In Framework, we will also see
a. How to create functions?
b. How to create qtp functions library file?
c. How to import functions library file in code?
d. How to add excel file as data sheet in code?
e. How to export data sheet file as excel file?
Prerequisite:
a. Knowledge of QTP working…
Architecture of Keyword Framework(what we will develop)
Where
AUT (Application under Test): it means application which his being automated in this case.
Object Repository: It is repository of the objects from AUT. QTP recognizes the objects in
the application through this object Repository.
Driver script: This is main script which drives the automation. This is where the whole logic is
written.
User Defined Function Library: It is Function Library which has common functions written in
it. These Functions will use in this framework.
Application Test Data: Application Test Data is the test data which will be tested by this
framework. Application Test data could be anything like data table, excel file, XML file or
database.
Generate Test Execution Report: Generate excel files where you can see test cases either
will be fail or Pass.
Follow steps to create small keyword based framework…
1. Create new test in QTP
2. Give the name to test
And click on Create button to create test.
3. Change the recording setting. Please select first option of web and windows
application in recording window..
4. Click on start record button to start recording.
5. Now open Flight GUI manually. Click on start all programs HP software HP
Unified Functional TestingSample Applications Flight GUI
6. Input the Agent name and password to Flight GUI login window
And click on OK button to login in Flight Reservation window.
7. After logged in, fill the info of user (Date of flight, Fly from, fly to, select flight,
name, Class )
After insert all info, click on Insert Order button to insert new order…
8. logout from the application by file Exit;
And stop recording.
9. After stop the recording, you can see your code, which will be similar like below
snapshot..
10. Now open one notepad and copy the below code to create StartApp(),
Login(),InsertDate(),InsertFlightLocation(),SelectFlight(),InsertCustomerName(),Ins
ertOrder(),Logout() functions in notepad
Function StartApp()
End Function
Function Login(login_info)
End Function
Function InsertDate(dat)
End Function
Function InsertFlightLocation(flight_station)
End Function
Function SelectFlight()
End Function
Function InsertCustomerName(cus_name)
End Function
Function InsertOrder()
End Function
Function Logout()
End Function
StartApp function is for start the Flight application.
Login function is for login into flight reservation application.
InsertDate function is for insert Date for order.
InsertFlightLocation is for insert flight stations
SelectFlight function is for select flight from the list
InsertCustomerName function is for Insert Customer name and class of Ticket
InsertOrder function is for insert new order.
Logout function is for Logout from reservation window.
11. copy first statement from qtp editor which open the application…
And paste into StartApp () function and add one below statement in StartApp() function to
return from StartApp() function
StartApp=”Pass”
after added statements, StartApp function will be similar as below snapshot.
Function StartApp()
SystemUtil.Run "C:\Program Files\HP\Unified Functional
Testing\samples\flight\app\flight4a.exe","","C:\Program Files\HP\Unified Functional
Testing\samples\flight\app\","open"
StartApp = "Pass"
End Function
12. Add below code inside Login function
login_info = Split(login_info, "-",-1)
Copy the selected statements from qtp editor, which will login into reservation window
paste selected statements in Login function.
Now change Dialog(“Login”).WindEdit("Agent Name:").Set "qaonline"
To
Dialog(“Login”).WindEdit("Agent Name:").Set login_info(0)
And
Dialog(“Login”).WindEdit("Password:").SetSecure "5334f584andsomething"
To
Dialog(“Login”).WindEdit("Password:").Set login_info(1)
copy the below code and paste into Login function
to return value from Login function
wait(4)
If Window("Flight Reservation").Exist Then
Login = "Pass"
else
Login = "Fail"
End If
If condition will check that reservation window will appear after logged in.. if yes code will
pass otherwise code will fail. After copy statements in Login function, the code inside Login
function will be similar to below Login function
Function Login(login_info)
login_info = Split(login_info, "-",-1)
Dialog("Login").WinEdit("Agent Name:").Set login_info(0)
Dialog("Login").WinEdit("Password:").Set login_info(1)
Dialog("Login").WinButton("OK").Click
wait(4)
If Window("Flight Reservation").Exist Then
Login = "Pass"
else
Login = "Fail"
End If
End Function
13. Copy the selected statement from qtp editor(value after Type keyword will be
different in your script in below statment)
Change Window(“Flight Reservation”).ActiveX("MaskEdBox").Type “yourenterdate”
To
Window(“Flight Reservation”).ActiveX("MaskEdBox").Type dat
And paste into InsertDate function and add below statement into InsertDate function to
return value from InsertDate function
InsertDate = "Pass"
After added statements the code will be similar to below code
Function InsertDate(dat)
Window(“Flight Reservation”).ActiveX("MaskEdBox").Type dat
InsertDate = "Pass"
End Function
(Note: date what you will pass greater than today’s date)
14. Add below statement
flight_station = Split(flight_station, "-",-1)
Copy the selected statement from qtp editor(flight stations may be different in your
script)
Now change Window(“Flight Reservation”).WinComboBox("Fly From:").Select “anyselect”
To
Window(“Flight Reservation”).WinComboBox("Fly From:").Select flight_station(0)
And
Window(“Flight Reservation”).WinComboBox("Fly To:").Select “anyselect”
To
Window(“Flight Reservation”).WinComboBox("Fly To:").Select flight_station(1)
And paste into InsertFlightLocation function and add below statement into
InsertFlightLocation function to return value from InsertFlightLocation function
InsertFlightLocation= "Pass"
After added statements the InsertFlightLocation function code will be similar to below code
Function InsertFlightLocation(flight_station)
flight_station = Split(flight_station,”-”,-1)
Window("Flight Reservation").WinComboBox("Fly From:").Select flight_station(0)
Window("Flight Reservation").WinComboBox("Fly To:").Select flight_station(1)
InsertFlightLocation = "Pass"
End Function
15. Copy the selected statement from qtp editor
And paste into SelectFlight function and add below statement into SelectFlight function to
return value from SelectFlight function
SelectFlight = "Pass"
After added statements the SelectFlight function code will be similar to below code
Function SelectFlight()
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
SelectFlight = "Pass"
End Function
16. Copy the selected statement from qtp editor(customer name may be different)
Now change Window("Flight Reservation").WinEdit("Name:").Set "whatyouenter"
To
Window("Flight Reservation").WinEdit("Name:").Set cus_name
And paste into InsertCustomerName function and add below statement into
InsertCustomerName function to return value from InsertCustomerName function
InsertCustomerName = "Pass"
After added statements the InsertCustomerName function code will be similar to below code
Function InsertCustomerName(cus_name)
Window("Flight Reservation").WinEdit("Name:").Set cus_name
Window("Flight Reservation").WinRadioButton("Business").Set
InsertCustomerName = "Pass"
End Function
17. Copy the below statement from qtp editor
And paste into InsertOrder function and add below statement into InsertOrder function to
return value from InsertOrder function
InsertOrder = "Pass"
After added statements the InsertOrder function code will be similar to below code
Function InsertOrder()
Window("Flight Reservation").WinButton("Insert Order").Click
InsertOrder = "Pass"
End Function
18. Copy the selected statement from qtp editor
And paste into Logout function and add below statement into Logout function to return value
from Logout function
Logout = "Pass"
After added statements the Logout function code will be similar to below code
Function Logout()
Window("Flight Reservation").WinMenu("Menu").Select "File;Exit"
Logout = "Pass"
End Function
19. Now save the notepad as keyword_custom_function.qfl in desktop where qfl is
extension of this file. (.qfl means QuickTest Functional Library which is local for
QTP)
Here are some snapshots of function library file
20. Now create a excel file name of testcases.xls and save in desktop. The content of this
file should be same as below snapshot of a file.
In above snapshot, There are two test cases
a. Login & Logout
b. Book flight
you can see that these tests have their description, TestCaseID, Execute, Result
Here
Description : Description of Test case
TestCaseID : Unique ID to each test case which will use to read steps of test case according
TestCaseID of steps.
Execute: Indicate that if Test case’ execute value is yes then that test case will execute if
Testcase’ execute value is no then that test case will execute. You can see in above snapshot
both test cases have yes value in execute column.
Result: Indicate that test case is Pass or Fail.
21. Now create a excel file name of steps.xls and save in desktop. The content of this file
should be same as below snapshot of a file.
In above snapshot, There are many keywords.
you can see that these keywords have their attributes description, TestCaseID, Data, Result
Here
Description : Description of step
TestCaseID : TestcaseID is reference of Test cases which are in testcases.xls file which will
use to read steps of test case according TestCaseID of steps.
Example: look in testcases.xls file, first test case Login & logout has TestcaseID as TC1. Now
see in steps.xls file, Three keywords have same ID, this means when first test case Login &
logout will call, these three keywords will perform in script. We will compare keywords in
our code and call the functions according to keyword.
Data : Keyword has own data like Login keyword has Agent Name and Password which are
separated by hyphen(-).
InsertDate keyword has date in format(first two characters are of month, next two characters
are of date and last two characters are year)
InsertFlightLocation keyword has Data as flight from station and flight to station, both are
separated by hyphen(-)
So Here Denver-London=> Denver is flight from station and London is flight To Station
InsertCustomerName keyword has customer name as data
Result: Indicate that step is Pass or Fail.
22. Now replace the code of action by copy below code (comments in green color and
bold) and paste into action of Testcase in qtp editor
Option Explicit 'to implicit declaration of variables
Dim testcases ' for all testcases
Dim steps 'for all steps
Dim caseid ' for test case id
Dim res ' for result
Dim i,j 'for iterations
Dim login_info, date1, cus_name, flight_station
' add two data sheets(testcases and steps) at the run time
DataTable.AddSheet "testcases"
DataTable.AddSheet "steps"
'load function library what we created function library file, filepath can be
different
LoadFunctionLibrary("C:\Documents and Settings\QA Tools
Testing\Desktop\keyword_custom_function.qfl ")
' import the testcases list and steps for above sheets, excel file path can
be different
DataTable.ImportSheet " C:\Documents and Settings\QA Tools Testing\Desktop
\testcase.xls",1,"testcases"
DataTable.ImportSheet " C:\Documents and Settings\QA Tools Testing\Desktop
\steps.xls",1,"steps"
'get the test cases from testcase sheet
testcases = DataTable.GetSheet("testcases").GetRowCount
'iterate the testcases one by one
For i = 1 To testcases
'set the cursor on particular ith row on testcase sheet
DataTable.GetSheet("testcases").SetCurrentRow(i)
'read Execute column of testcases sheet of ith row.Execute is yes for
testcase then it will execute
If DataTable.Value("Execute","testcases")="yes" Then
'retrieve test caseid
caseid = DataTable.GetSheet("testcases").GetParameter("TestcaseID")
'get the all rows of steps test sheet
steps = DataTable.GetSheet("steps").GetRowCount
'iterate the steps one by one
For j = 1 To steps
'set the cursor on particular jth row on steps test sheet
DataTable.GetSheet("steps").SetCurrentRow(j)
'check if step id is same as case id then go to case with its keyword
If DataTable.Value("TestCaseID","steps")=caseid Then
'retreive the keyword of jth row from steps sheet
Select Case Datatable.Value("keyword","steps")
'if keyword is StartApp then call StartApp and save the return value
to res
Case "StartApp"
res = StartApp()
'if keyword is Login then call Login function and save the return
value to res
Case "Login"
login_info = Datatable.Value("Data","steps")
res = Login(login_info)
'if keyword is InsertDate then call InsertDate function and save the
return value to res
Case "InsertDate"
date1 = Datatable.Value("Data","steps")
res = InsertDate(date1)
'if keyword is InsertFlightLocation then call InsertFlightLocation
function and save the return value to res
Case "InsertFlightLocation"
flight_station = Datatable.Value("Data","steps")
res = InsertFlightLocation(flight_station)
'if keyword is SelectFlight then call SelectFlight function and save the
return value to res
Case "SelectFlight"
res = SelectFlight()
'if keyword is InsertCustomerName then call InsertCustomerName
function and save the return value to res
Case "InsertCustomerName"
cus_name = Datatable.Value("Data","steps")
res = InsertCustomerName(cus_name)
'if keyword is InsertOrder then call InsertOrder function and save the
return value to res
Case "InsertOrder"
res = InsertOrder()
'if keyword is Logout then call Logout function and save the return
value to res
Case "Logout"
res = Logout()
End Select
'save the result in Result column of jth step in steps data sheet what
we added in beginning
DataTable.Value("Result","steps")= res
End If
Next
'save the result in Result Column of ith testcase in Testcase data sheet
what we added in beginning
If res="Pass" Then
DataTable.Value("Result","testcases") = "Pass"
else
DataTable.Value("Result","testcases") = "Fail"
End If
End If
Next
' export the sheets after execution of all test cases to different xls files...
DataTable.ExportSheet "C:\Documents and Settings\QA Tools
Testing\Desktop\exer_testcase.xls","testcases"
DataTable.ExportSheet " C:\Documents and Settings\QA Tools Testing\Desktop
\exer_steps.xls","steps"
23. Now execute the testcase and see the execution flow..
24. Here is some execution snapshots
25. Now See exported files
Keyword steps are passed.
Test cases are passed