What can be done if the Objects are not being recognised as expected by QTP
1. Make sure you have the appropriate add ins installed
2. Try opening QTP first and then the application and also visa-versa
3. Try opening the application through QTP by changing the Record and Run settings. Add the url for the application and then select the option "Record and Run only on"
4. Try opening the application through SytemUtil.Run and then try
5. Check the object type with the developers are try to decide which property an be used to uniquely identify the object
6. Go for descriptive programming
7. Try and create Virtual objects and try
8. Go for some misc options to find a work around like active cursor, active tab. Also try if it is possible to get to the object using the keyboard shortcuts.
9. Last but not the least check if there is some work around with out touching that part of the page or screen. This is generally not prefered at all.
Search This Blog
Friday, July 30, 2010
Creating Objects
To create user defined objects and give them properties that you want
Myobj = Description.Create()
Myobj("Attachedtext").Value = "Agent"
Myobj("htmltag").Value = "A"
The object is now created. Now let us see how we can use this object in our script
Dialog(" ").WinEdit(Myobject).Set "Name"
Myobj = Description.Create()
Myobj("Attachedtext").Value = "Agent"
Myobj("htmltag").Value = "A"
The object is now created. Now let us see how we can use this object in our script
Dialog(" ").WinEdit(Myobject).Set "Name"
Basic Excel
Lets us open an excel sheet and try to read and write data into it.
Set Excel = CreateObject("Excel.Application")
Set Book = Excel.Workbooks.Open("Path")
Set Sheet = Book.Worksheets("Global")
For Row = 1 to 4
For Col = 1 to 3
msgbox Sheet.Rows(Row).Columns(Col)
Next
Next
For Row = 1 to 4
For Col = 1 to 3
Sheet.Rows(Row).Columns(Col).value = Row+Col
Next
Next
Set Excel = CreateObject("Excel.Application")
Set Book = Excel.Workbooks.Open("Path")
Set Sheet = Book.Worksheets("Global")
For Row = 1 to 4
For Col = 1 to 3
msgbox Sheet.Rows(Row).Columns(Col)
Next
Next
For Row = 1 to 4
For Col = 1 to 3
Sheet.Rows(Row).Columns(Col).value = Row+Col
Next
Next
Calculator Automation
In this we are trying to automate the basic fucntions of the Calculator i.e Addition, Subtraction, Multiplication and Division. We are doing this here for 25 rows of data. After each iteration the results are being compared with another excel sheet that has the results already calculated. This is for the purpose of verfication. We are also capturing the memory usage for the Calculaotr Application in the process. The code is divided into 2 segments: one is the main where we have only the function calls and the second is the functions space where we store all our functions as .vbs files.
Main
Open_Calculator
Check_Launch
Load_Data
Check_RowCount
Add_Parameter
For Set_Row= 1 to 10
If Set_Row = 1or Set_Row = 7 or Set_Row = 14 or Set_Row = 20 or Set_Row = 25 Then
Task_Manager()
End If
Environment.Value("Incorrect_Input")=0
Call_Arithemetic (Set_Row)
Next
File_Name=InputBox("Enter the file name f or the Results file")
SaveClose(File_Name)
Functions
********* Application Launch Checkpoint ***********************
Public Function Check_Launch()
Wait 2
If Window("Calculator").Exist Then
Reporter.ReportEvent micPass, "Application Launch", "The application has launched successfully"
Else
Reporter.ReportEvent micFail, "Application Launch", "The application has failed to launch"
End If
End Function
'******** Check Row Count ****************************
Public Function Check_RowCount()
RowCount = DataTable.GetRowCount
If RowCount=25 Then
Reporter.ReportEvent micPass, "RowCount", "There are 25 rows of data"
Else
Reporter.ReportEvent micFail, "RowCount", "Incorrect Row Count. It should be 25 instead of "&RowCount
End If
End Function
'******* Check the typed data *************
Public Function Check_TypedData(Num)
Text= Window("Calculator").WinEdit("Input_Output Box").GetROProperty("text")
Text=clng (Text)
Text=RTrim(Text)
Text=LTrim(Text)
If Text = Num Then
Reporter.ReportEvent micPass, "TypedText", "The typed data is the same as in the input file"
Else
Reporter.ReportEvent micFail, "TypedText", "The typed data does not match the data in the input file"
Environment.Value ("Incorrect_Input") = 1
DataTable("Status", Globaldt)= "Fail"
DataTable("Description", Globaldt) = "Entered number " & Num & " and Calculator output " & Text & " do not match"
End If
End Function
'*********************** Check the Output Data *****************
Public Function Perform_Operation (k, Numbers, Operation)
If Environment.Value ("Incorrect_Input") = 0 Then
If Numbers=3 Then
Get_Digits(DataTable(k,Globaldt))
Check_TypedData (DataTable(k,Globaldt))
Else
Get_Digits(DataTable((6-k), Globaldt))
Check_TypedData(DataTable((6-k), Globaldt))
End If
Type_Key(Operation)
Else
Exit Function
End If
End Function
'****************** Arithemetic Functions*******************
Public Function Arithemetic (Operator)
Select Case Operator
Case "Add"
Numbers=3
Operation="\+"
Case "Multiply"
Numbers=3
Operation="\*"
Case "Subtract"
Numbers=2
Operation="\-"
Case "Divide"
Numbers=2
Operation="\/"
End Select
For k=1 to Numbers
Call Perform_Operation (k, Numbers, Operation)
Next
If Environment.Value("Incorrect_Input")= 0 Then
Ans=Window("Calculator").WinEdit("Input_Output Box").GetROProperty("text")
Ans=Round (Ans,2)
DataTable (Operator, Globaldt)=Ans
Window("Calculator").WinButton("Clear").Click
Else
Window("Calculator").WinButton("Clear").Click
Exit Function
End If
End Function
'************* Start Calculator ***************
Public Function Open_Calculator()
SystemUtil.Run "C:\WINDOWS\system32\calc.exe"
End Function
'******************** Load Data ********************************
Public Function Load_Data()
DataTable.Import("D:\Ranger Automation\Learn QTP\Calculator_Exc\Data File\" & Environment.Value("Load_Data") & ".xls")
End Function
'************ Check Input Rows *****************************
Public Function Check_Input (Start_Row, End_Row)
If (Start_Row > End_Row) Then
Msgbox ("The data entered is invalid. End Row can not be less than the Start Row")
Window("Calculator").Close
ExitTest
End If
End Function
'*************Call Arithemetic Function **************
Public Function Call_Arithemetic (Set_Row)
DataTable.SetCurrentRow(Set_Row)
Arithemetic ("Add")
Arithemetic ("Multiply")
Arithemetic ("Subtract")
Arithemetic ("Divide")
Validate_Data(Set_Row)
End Function
''********** Getting the number split into digits***************
Public Function Get_Digits (Num)
Length_Num=Len(Num)
Do Until Length_Num=0
Digit=Left (Num, 1)
Type_Key(Digit)
Num=Right(Num,(Length_Num-1))
Length_Num=Length_Num-1
Loop
End Function
'*************Typing the keys in the calculator*****************
Public Function Type_Key(input)
If input="." Then
Window("Calculator").WinButton("Decimal").Click
Else
Window("Calculator").WinButton("text:="&input).Click
End If
End Function
'***************Save and Close Calculator ********************
Public Function SaveClose(File_Name)
sPath="D:\Ranger Automation\Learn QTP\Calculator_Exc\Results\" & File_Name & ".xls"
DataTable.Export (sPath)
Window("Calculator").Close
Dialog("Windows Task Manager").Close
End Function
'************* Add Status Column *************
Public Function Add_Parameter
DataTable.GetSheet(1).AddParameter "Status", ""
DataTable.GetSheet(1).AddParameter "Description", ""
DataTable.DeleteSheet(2)
DataTable.AddSheet("Task_Manager")
DataTable.GetSheet(2).AddParameter "Percentage", ""
DataTable.GetSheet(2).AddParameter "Memory_Usage", ""
DataTable.GetSheet(2).AddParameter "Peak_Memory_Usage", ""
SystemUtil.Run "taskmgr.exe"
Selection = Dialog("Windows Task Manager").WinTab("text:=Tab", "regexpwndclass:=SysTabControl32").GetROProperty("selection")
If Selection <> "Processes" Then
Dialog("Windows Task Manager").WinTab("text:=Tab", "regexpwndclass:=SysTabControl32").Select "Processes"
End If
End Function
'*************** Validate the data *******************
Public Function Validate_Data(Set_Row)
If Environment.value("Incorrect_Input") = 0 Then
Set Excel= CreateObject("Excel.Application")
sPath = "D:\Ranger Automation\Learn QTP\Calculator_Exc\Data File\" & Environment.Value ("Expected_Results") &".xls"
Set Book=Excel.Workbooks.Open(sPath)
Excel.Visible = True
Set Sheet= Book.Worksheets("Global")
Flag = 0
For Col = 4 to 7
If Flag = 0 Then
Actual = DataTable(Col, Globaldt)
Actual = Round (Actual, 2)
Expected = Sheet.Rows(Set_Row+1).Columns(Col)
If Actual = Expected Then
DataTable("Status", Globaldt) = "Pass"
Else
DataTable("Status", Globaldt) = "Fail"
Flag =1
Reporter.ReportEvent micFail, "Data Incorrect", "Data in Row " & Set_Row& " Column "& Col & " is Incorrect"
DataTable("Description", Globaldt) = "Data in Col "& Col & " is Incorrect. The expected value is " & Expected
End If
ElseIf Flag = 1 Then
Exit For
End If
Next
Book.Close
Else
Exit Function
End If
End Function
'*********************** Task Manager ****************************
Public Function Task_Manager()
DataTable.GetSheet(2).SetCurrentRow(Environment.Value("SetRow"))
mem =Dialog("Windows Task Manager").WinlistView("Image Name").GetSubItem("calc.exe", "Mem Usage")
peak = Dialog("Windows Task Manager").WinlistView("Image Name").GetSubItem("calc.exe", "Peak Mem Usage")
DataTable("Percentage", "Task_Manager")= Environment.Value("Percentage")
DataTable("Memory_Usage", "Task_Manager")=mem
DataTable("Peak_Memory_Usage", "Task_Manager") = peak
Environment.Value("Percentage") = Environment.Value("Percentage") + 25
Environment.Value("SetRow") = Environment.Value("SetRow") + 1
End Function
Main
Open_Calculator
Check_Launch
Load_Data
Check_RowCount
Add_Parameter
For Set_Row= 1 to 10
If Set_Row = 1or Set_Row = 7 or Set_Row = 14 or Set_Row = 20 or Set_Row = 25 Then
Task_Manager()
End If
Environment.Value("Incorrect_Input")=0
Call_Arithemetic (Set_Row)
Next
File_Name=InputBox("Enter the file name f or the Results file")
SaveClose(File_Name)
Functions
********* Application Launch Checkpoint ***********************
Public Function Check_Launch()
Wait 2
If Window("Calculator").Exist Then
Reporter.ReportEvent micPass, "Application Launch", "The application has launched successfully"
Else
Reporter.ReportEvent micFail, "Application Launch", "The application has failed to launch"
End If
End Function
'******** Check Row Count ****************************
Public Function Check_RowCount()
RowCount = DataTable.GetRowCount
If RowCount=25 Then
Reporter.ReportEvent micPass, "RowCount", "There are 25 rows of data"
Else
Reporter.ReportEvent micFail, "RowCount", "Incorrect Row Count. It should be 25 instead of "&RowCount
End If
End Function
'******* Check the typed data *************
Public Function Check_TypedData(Num)
Text= Window("Calculator").WinEdit("Input_Output Box").GetROProperty("text")
Text=clng (Text)
Text=RTrim(Text)
Text=LTrim(Text)
If Text = Num Then
Reporter.ReportEvent micPass, "TypedText", "The typed data is the same as in the input file"
Else
Reporter.ReportEvent micFail, "TypedText", "The typed data does not match the data in the input file"
Environment.Value ("Incorrect_Input") = 1
DataTable("Status", Globaldt)= "Fail"
DataTable("Description", Globaldt) = "Entered number " & Num & " and Calculator output " & Text & " do not match"
End If
End Function
'*********************** Check the Output Data *****************
Public Function Perform_Operation (k, Numbers, Operation)
If Environment.Value ("Incorrect_Input") = 0 Then
If Numbers=3 Then
Get_Digits(DataTable(k,Globaldt))
Check_TypedData (DataTable(k,Globaldt))
Else
Get_Digits(DataTable((6-k), Globaldt))
Check_TypedData(DataTable((6-k), Globaldt))
End If
Type_Key(Operation)
Else
Exit Function
End If
End Function
'****************** Arithemetic Functions*******************
Public Function Arithemetic (Operator)
Select Case Operator
Case "Add"
Numbers=3
Operation="\+"
Case "Multiply"
Numbers=3
Operation="\*"
Case "Subtract"
Numbers=2
Operation="\-"
Case "Divide"
Numbers=2
Operation="\/"
End Select
For k=1 to Numbers
Call Perform_Operation (k, Numbers, Operation)
Next
If Environment.Value("Incorrect_Input")= 0 Then
Ans=Window("Calculator").WinEdit("Input_Output Box").GetROProperty("text")
Ans=Round (Ans,2)
DataTable (Operator, Globaldt)=Ans
Window("Calculator").WinButton("Clear").Click
Else
Window("Calculator").WinButton("Clear").Click
Exit Function
End If
End Function
'************* Start Calculator ***************
Public Function Open_Calculator()
SystemUtil.Run "C:\WINDOWS\system32\calc.exe"
End Function
'******************** Load Data ********************************
Public Function Load_Data()
DataTable.Import("D:\Ranger Automation\Learn QTP\Calculator_Exc\Data File\" & Environment.Value("Load_Data") & ".xls")
End Function
'************ Check Input Rows *****************************
Public Function Check_Input (Start_Row, End_Row)
If (Start_Row > End_Row) Then
Msgbox ("The data entered is invalid. End Row can not be less than the Start Row")
Window("Calculator").Close
ExitTest
End If
End Function
'*************Call Arithemetic Function **************
Public Function Call_Arithemetic (Set_Row)
DataTable.SetCurrentRow(Set_Row)
Arithemetic ("Add")
Arithemetic ("Multiply")
Arithemetic ("Subtract")
Arithemetic ("Divide")
Validate_Data(Set_Row)
End Function
''********** Getting the number split into digits***************
Public Function Get_Digits (Num)
Length_Num=Len(Num)
Do Until Length_Num=0
Digit=Left (Num, 1)
Type_Key(Digit)
Num=Right(Num,(Length_Num-1))
Length_Num=Length_Num-1
Loop
End Function
'*************Typing the keys in the calculator*****************
Public Function Type_Key(input)
If input="." Then
Window("Calculator").WinButton("Decimal").Click
Else
Window("Calculator").WinButton("text:="&input).Click
End If
End Function
'***************Save and Close Calculator ********************
Public Function SaveClose(File_Name)
sPath="D:\Ranger Automation\Learn QTP\Calculator_Exc\Results\" & File_Name & ".xls"
DataTable.Export (sPath)
Window("Calculator").Close
Dialog("Windows Task Manager").Close
End Function
'************* Add Status Column *************
Public Function Add_Parameter
DataTable.GetSheet(1).AddParameter "Status", ""
DataTable.GetSheet(1).AddParameter "Description", ""
DataTable.DeleteSheet(2)
DataTable.AddSheet("Task_Manager")
DataTable.GetSheet(2).AddParameter "Percentage", ""
DataTable.GetSheet(2).AddParameter "Memory_Usage", ""
DataTable.GetSheet(2).AddParameter "Peak_Memory_Usage", ""
SystemUtil.Run "taskmgr.exe"
Selection = Dialog("Windows Task Manager").WinTab("text:=Tab", "regexpwndclass:=SysTabControl32").GetROProperty("selection")
If Selection <> "Processes" Then
Dialog("Windows Task Manager").WinTab("text:=Tab", "regexpwndclass:=SysTabControl32").Select "Processes"
End If
End Function
'*************** Validate the data *******************
Public Function Validate_Data(Set_Row)
If Environment.value("Incorrect_Input") = 0 Then
Set Excel= CreateObject("Excel.Application")
sPath = "D:\Ranger Automation\Learn QTP\Calculator_Exc\Data File\" & Environment.Value ("Expected_Results") &".xls"
Set Book=Excel.Workbooks.Open(sPath)
Excel.Visible = True
Set Sheet= Book.Worksheets("Global")
Flag = 0
For Col = 4 to 7
If Flag = 0 Then
Actual = DataTable(Col, Globaldt)
Actual = Round (Actual, 2)
Expected = Sheet.Rows(Set_Row+1).Columns(Col)
If Actual = Expected Then
DataTable("Status", Globaldt) = "Pass"
Else
DataTable("Status", Globaldt) = "Fail"
Flag =1
Reporter.ReportEvent micFail, "Data Incorrect", "Data in Row " & Set_Row& " Column "& Col & " is Incorrect"
DataTable("Description", Globaldt) = "Data in Col "& Col & " is Incorrect. The expected value is " & Expected
End If
ElseIf Flag = 1 Then
Exit For
End If
Next
Book.Close
Else
Exit Function
End If
End Function
'*********************** Task Manager ****************************
Public Function Task_Manager()
DataTable.GetSheet(2).SetCurrentRow(Environment.Value("SetRow"))
mem =Dialog("Windows Task Manager").WinlistView("Image Name").GetSubItem("calc.exe", "Mem Usage")
peak = Dialog("Windows Task Manager").WinlistView("Image Name").GetSubItem("calc.exe", "Peak Mem Usage")
DataTable("Percentage", "Task_Manager")= Environment.Value("Percentage")
DataTable("Memory_Usage", "Task_Manager")=mem
DataTable("Peak_Memory_Usage", "Task_Manager") = peak
Environment.Value("Percentage") = Environment.Value("Percentage") + 25
Environment.Value("SetRow") = Environment.Value("SetRow") + 1
End Function
Thursday, July 29, 2010
Sentence to Words
This is how you can convert a sentence to word. This is helpful when u get the object property as a string and then need to separate it into words for further processing. This is also very helpful in the Data Driven framework, where the criterion for running the tst suites may be provided in strings.
Str = Window(" ").Object(" ").GetVisibleText
Var = Split(Str, "delimiter") ' The delimiter can be a space or comma or $ symbol etc
Len_Array = UBound (Var)
For i = 0 To Len_Array
Str_1 = Var(i)
Msgbox Str_1
Next
Str = Window(" ").Object(" ").GetVisibleText
Var = Split(Str, "delimiter") ' The delimiter can be a space or comma or $ symbol etc
Len_Array = UBound (Var)
For i = 0 To Len_Array
Str_1 = Var(i)
Msgbox Str_1
Next
Task Manager
This is the sample code for getting the memory usage and peak memory usage for an application using QTP and writing it out in the DataTable. Here we are using the calc.exe application. This method can be used to get the data from any WinList View object.
SystemUtil.Run "taskmgr.exe"
Selection = Dialog("Windows Task Manager").WinTab("text:=Tab", "regexpwndclass:=SysTabControl32").GetROProperty("selection")
If Selection <> "Processes" Then
Dialog("Windows Task Manager").WinTab("text:=Tab1", "regexpwndclass:=SysTabControl32").Select "Processes"
End If
mem =Dialog("Windows Task Manager").WinlistView("Image Name").GetSubItem("calc.exe", "Mem Usage")
peak = Dialog("Windows Task Manager").WinlistView("Image Name").GetSubItem("calc.exe", "Peak Mem Usage")
DataTable("Percentage", "Task_Manager")= Environment.Value("Percentage")
DataTable("Memory_Usage", "Task_Manager")=mem
DataTable("Peak_Memory_Usage", "Task_Manager") = peak
SystemUtil.Run "taskmgr.exe"
Selection = Dialog("Windows Task Manager").WinTab("text:=Tab", "regexpwndclass:=SysTabControl32").GetROProperty("selection")
If Selection <> "Processes" Then
Dialog("Windows Task Manager").WinTab("text:=Tab1", "regexpwndclass:=SysTabControl32").Select "Processes"
End If
mem =Dialog("Windows Task Manager").WinlistView("Image Name").GetSubItem("calc.exe", "Mem Usage")
peak = Dialog("Windows Task Manager").WinlistView("Image Name").GetSubItem("calc.exe", "Peak Mem Usage")
DataTable("Percentage", "Task_Manager")= Environment.Value("Percentage")
DataTable("Memory_Usage", "Task_Manager")=mem
DataTable("Peak_Memory_Usage", "Task_Manager") = peak
Subscribe to:
Posts (Atom)