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
Please add the input excel file. Otherwise it is difficult to under stand how data is kept in datasheet
ReplyDeleteThanks and I have a dandy present: House Renovation Designer Near Me home renovation jobs near me
ReplyDelete