Monday 31 August 2015

Synchronization in QTP and Mouse Operations

HomeBrowserPage.Link(LogoutBtnVar, "index:=0").ExistCheck
HomeBrowserPage.Link(AdminLnkVar, "index:=0").WaitforObj 5



Public Function CheckIfExist(ByRef test_object, iWaitTime)
    CheckIfExist = False
    If isObjectLoaded(test_object, iWaitTime) Then
        reporter.ReportEvent micPass, test_object.GetRoProperty("name"), test_object.GetRoProperty("name") & " exists."
        CheckIfExist = True
    Else
        CheckIfExist = False
        Reporter.ReportEvent micWarning, "The Object: "& test_object & "doesn't exist", "The Object: "& test_object & "doesn't exist"
    End If
End Function
RegisterUserFunc "WebArea", "ExistCheck", "CheckIfExist"
RegisterUserFunc "WebEdit", "ExistCheck", "CheckIfExist"
RegisterUserFunc "WebCheckBox", "ExistCheck", "CheckIfExist"
RegisterUserFunc "WebElement", "ExistCheck", "CheckIfExist"
RegisterUserFunc "WebRadioGroup", "ExistCheck", "CheckIfExist"
RegisterUserFunc "WebList", "ExistCheck", "CheckIfExist"
RegisterUserFunc "WebButton", "ExistCheck", "CheckIfExist"
RegisterUserFunc "Link", "ExistCheck", "CheckIfExist"
RegisterUserFunc "Image", "ExistCheck", "CheckIfExist"
RegisterUserFunc "WebTable", "ExistCheck", "CheckIfExist"
RegisterUserFunc "Page", "ExistCheck", "CheckIfExist"
RegisterUserFunc "Browser", "ExistCheck", "CheckIfExist"
RegisterUserFunc "Frame", "ExistCheck", "CheckIfExist"



isObjectLoaded= isObjectLoaded
Function isObjectLoaded(sObjName, iWaitTime)
    isObjectLoaded = False
    Cnt = 0
    Do
        If sObjName.Exist(2) Then           
            If StrComp(Lcase(Trim(sObjName.Object.readyState)), "complete",1) = 0 or StrComp(CStr(sObjName.Object.readyState), "4",1) = 0 Then           
                isObjectLoaded = True
                Exit Do
            Elseif Cnt  = sWaitTime Then
                Exit Do
            End If
        Elseif Cnt  = sWaitTime Then
            Exit Do
        End If      
        Cnt = Cnt + 1
    Loop Until isObjectLoaded = True
End Function



'step1= Browser("Login").Page("Login").Image("BC-Logo").MouseOpr (1, 1, "rightclk")
'step1=Browser("collaborative sourcing").Page("collaborative sourcing").Frame("fraLeftFrame").Link("Page Settings").MouseOpr (3,5, "mousemove")
Public Function MouseOprations(ByRef objRef, intX, intY, strOpr)
   MouseOprations=False
   If isObject(objRef) Then
'        objRef.waitForObj 10
       objRef.Highlight   
       wait 1      
       x=objRef.getROProperty("abs_x")
       y=objRef.getROProperty("abs_y")
       Set objMouse=CreateObject("Mercury.DeviceReplay")
       Select Case LCase(Trim(strOpr))
       Case "clk"
           objMouse.MouseClick x+Cint(intX), y+CInt(intY), Left_Mouse_Button
           MouseOprations=True
        Case "rightclk"
            objMouse.MouseMove x+Cint(intX), y+CInt(intY)
            objMouse.MouseClick x+Cint(intX), y+CInt(intY), Right_Mouse_Button
            MouseOprations=True
        Case "dblclk"
            objMouse.MouseDblClick  x+Cint(intX), y+CInt(intY), Left_Mouse_Button
            MouseOprations=True
        Case "mousemove"
            objMouse.MouseMove x+Cint(intX), y+CInt(intY)
            wait 1
            MouseOprations=True
       End Select
    Set objMouse=Nothing
   End If     
End Function
RegisterUserFunc "WebButton", "MouseOpr", "MouseOprations"
RegisterUserFunc "Image", "MouseOpr", "MouseOprations"
RegisterUserFunc "WebEdit", "MouseOpr", "MouseOprations"
RegisterUserFunc "WebCheckBox", "MouseOpr", "MouseOprations"
RegisterUserFunc "WebElement", "MouseOpr", "MouseOprations"
RegisterUserFunc "WebRadioGroup", "MouseOpr", "MouseOprations"
RegisterUserFunc "WebList", "MouseOpr", "MouseOprations"
RegisterUserFunc "Link", "MouseOpr", "MouseOprations"
RegisterUserFunc "WebElement", "MouseOpr", "MouseOprations"




'' To Maximize Browser
Function MaxBrowser()
   Set objWS = CreateObject("WScript.Shell")
   objWS.SendKeys "% "
   wait 1
   objWS.SendKeys "x"  
   Set objWS=Nothing
End Function


Function MaximizeBrowser(objBrowser)
    Dim hWnd
    objBrowser.Sync
    hWnd = objBrowser.GetROProperty("hwnd")
     On Error Resume Next
        Window("hwnd:=" & hWnd).Activate
         If Err.Number <> 0 Then
            hWnd = Browser("hwnd:=" & hWnd).Object.hWnd
            Window("hwnd:=" & hWnd).Activate
            Err.Clear
        End If
         Window("hwnd:=" & hWnd).Maximize
    On Error Goto 0
End Function
RegisterUserFunc "Browser", "MaximizeBrowser", "MaximizeBrowser"

'eg: strSendKey("{Enter}")
Function strSendKey(strKeys)
    Set objWS=CreateObject("WScript.Shell")
        objWS.SendKeys strKeys
    Set objWS=Nothing
End Function


'' eg: MouseClickAbsValues(Browser("").Page("").WebElement(""), int2, int2)
Function MouseClickAbsValues(objRef, intX, intY)
   MouseClickAbsValues=false
'   objRef.Highlight   
    If objRef.Exist(2) Then
        x=objRef.GetROProperty("abs_x")
        y=objRef.GetROProperty("abs_y")
        Set mo=CreateObject("Mercury.DeviceReplay")
            mo.MouseClick x+intX, y+intY, micLeftBtn
            MouseClickAbsValues=True
        Set mo=Nothing
    End If
End Function

Function KillProcessesBeforeExec()
    ProcessNames = "iexplore.exe"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = CreateObject("Wscript.Network")
    currUser = objNetwork.UserName
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    KillProcess = Split(ProcessNames,"|")
    For x = 0 to  Ubound(KillProcess)
        Set colProcessList = objWMIService.ExecQuery _
        ("SELECT * FROM Win32_Process WHERE Name ='"& KillProcess(x) &"'")
        For Each objProcess in colProcessList
            If objProcess.GetOwner ( User, Domain ) = 0 Then
                If UCase(User) = UCase(currUser) Then
                    objProcess.Terminate()
                    KillProcessesBeforeExec = true
                end if
            end if
        Next
    Next
    Set objFSO = Nothing
    Set objNetwork = Nothing
    Set objWMIService = Nothing
End Function


'''******************************************************************
'' Here wait for the object
HomeBrowserPage.Link(LogoutBtnVar, "index:=0").WaitforObject(ByRef objRef, 200)
Public Function WaitforObject(ByRef objRef, intWaitSecs)
   WaitforObject= False
    blnFlag= False
    intCount=1
    If ISObject(objRef) Then
        For iWaitSecs = 1 to intWaitSecs
            blnFlag = objRef.Exist(2)
            If blnFlag Then                        
                Do While (LCase(Trim(objRef.getROProperty("attribute/readyState")))<>"complete") or (Trim(objRef.getROProperty("attribute/readyState"))<>"4" )
                    If intCount= intWaitSecs Then
                        WaitforObject = True
                        Exit Do
                    End If
                    intCount=intCount+1
                Loop
                Exit For
            End If          
        Next
         If Not blnFlag Then
                wait 2
                WaitforObject = True
        End If
    End If 
End Function
RegisterUserFunc "Browser", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebEdit", "WaitforObj", "WaitforObject"
RegisterUserFunc "Frame", "WaitforObj", "WaitforObject"
RegisterUserFunc "Page", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebArea", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebButton", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebCheckBox", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebElement", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebFile", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebList", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebRadioGroup", "WaitforObj", "WaitforObject"
RegisterUserFunc "WebTable", "WaitforObj", "WaitforObject"
RegisterUserFunc "Link", "WaitforObj", "WaitforObject"
RegisterUserFunc "Image", "WaitforObj", "WaitforObject"





'' Wait until object disapear
Public Function waitforObjDisappear(ByRef objRef, intWaitSecs)
    waitforObjDisappear= False   
    blnFlag=False
        If ISObject(objRef) Then
            objName=objRef.getROProperty("name")
            For iWaitSecs = 1 to intWaitSecs
                If objRef.Exist(1) Then
                    If iWaitSecs = intWaitSecs Then
                        Reporter.ReportEvent micDone, "Wait till the objects remains available: " & objName, "The object: "& objName & " is still displayed."
                        Exit For
                    End If
                else   
                    blnFlag=True
                    waitforObjDisappear= True  
                    Reporter.ReportEvent micDone, "Wait till the objects remains available: " & objName, "The object: "& objName & " is no longer displayed."
                    Exit For
                End If
            Next
        End If
        If Not blnFlag Then
            wait 2           
        End If       
End Function
RegisterUserFunc "Image", "waitforObjDisappear", "waitforObjDisappear"
RegisterUserFunc "WebButton", "waitforObjDisappear", "waitforObjDisappear"
RegisterUserFunc "Link", "waitforObjDisappear", "waitforObjDisappear"
RegisterUserFunc "WebElement", "waitforObjDisappear", "waitforObjDisappear"
RegisterUserFunc "WebTable", "waitforObjDisappear", "waitforObjDisappear"
RegisterUserFunc "WebCheckBox", "waitforObjDisappear", "waitforObjDisappear"
RegisterUserFunc "WebList", "waitforObjDisappear", "waitforObjDisappear"
RegisterUserFunc "WebRadioGroup", "waitforObjDisappear", "waitforObjDisappear"
RegisterUserFunc "WebEdit", "waitforObjDisappear", "waitforObjDisappear"





No comments:

Post a Comment