Wednesday, May 6, 2015

Adding custom methods to an object

During an automation project, generally you work on same web application. And during this project you see that making a procedure is same on all of the test cases (like ensuring the synchronization of a web page). You can make use of shared function library to develop faster/easier/maintainable test scripts (Also you can check Extensibility plugin).

Adding custom methods to an object may be one of these. For example during the synchronization of the web page we want to make sure that the Browser object is activated. To accomplish this I've added a method named "Activate" to the "Browser" object.

The first thing to do is write a generic method that activates the browser:

'Activates the Browser and moves the mouse on the Browser's Page object
Function BrowserActivate(Object)
    Dim hWnd
    Reporter.Filter = rfDisableAll
    On Error Resume Next
     If Environment.Value("Browser")="Chrome" Then
      hWnd = Window("nativeclass:=Chrome_WidgetWin_1","regexpwndtitle:= Google Chrome").getroproperty("hwnd")
      hWnd = Object.GetROProperty("hwnd")
     End If
        Window("hwnd:=" & hWnd).Activate 
        If Err.Number <> 0 Then
            Window("hwnd:=" & Browser("hwnd:=" & hWnd).Object.hWnd).Activate
        End If
    On Error Goto 0
    ''>>>This part is not mandotary, to move the mouse on a blank part of the page you can uncomment this part
 'Set obj=CreateObject("Mercury.DeviceReplay")
    'If Environment.Value("Browser")="Chrome" Then
    ' obj.MouseMove Window("hwnd:=" & hWnd).GetROProperty("abs_x") + 10, Window("hwnd:=" & hWnd).GetROProperty("abs_y") + 75
 ' obj.MouseMove Browser("hwnd:=" & hWnd).Page("index:=0").GetROProperty("abs_x") + 5, Browser("hwnd:=" & hWnd).Page("index:=0").GetROProperty("abs_y") + 5
 'End If
    Reporter.Filter = rfEnableAll
End Function

Second (just after the function declaration) we are registering this method to the Browser object.

'Register the BrowserActivate Function as a method of the Browser object
RegisterUserFunc "Browser", "Activate", "BrowserActivate"
'Now the Browser object has Activate method

Now you can use the Activate method of the Browser object.

No comments: