Wednesday 22 January 2014

Google Search Automation!!!!!!

Through the years I have been asked many times by novices to automation and QTP about Google/Gmail automation. This short post provides an example of Google search automation using a class (see Below). The class encapsulates opening the Browser (invoking Internet Explorer), navigating to the Google.com page, searching and retrieving the results. The Main Procedure takes care of the analysis and final reporting of the test’s status.
Prerequisites:
· Environment(“OPEN_URL”) – OPEN_URL must be defined as either an internal or external variable
· DataTable(“Query”, dtLocalSheet) – Query must be defined as a DataTable parameter in the local sheet.

01Dim oGoogleSearch
02Dim iNumResults
03Dim sMaxResults
04Dim iMaxResults
05Dim oListResults
06Dim oDicSearches
07
08'A .Net ArrayList is used to store the results and sort them with ease
09If Not lcase(typename(oListResults)) = "arraylist" Then
10    Set oListResults = createobject("System.Collections.ArrayList")
11End If
12
13'A Dictionary is used to store the search results (key) linked to the search terms (value)
14If Not lcase(typename(oDicSearches)) = "dictionary" Then
15    Set oDicSearches = createobject("scripting.dictionary")
16End If
17
18Set oGoogleSearch = getGoogleSearch()
19
20sToSearch = DataTable("Query", dtLocalSheet)
21iNumResults = oGoogleSearch.doSearch(sToSearch)
22
23oListResults.Add iNumResults
24
25'If more than one search term share the same number of results, the item associated with the key will concatenate them as a comma separated string.
26If Not oDicSearches.Exists(iNumResults) then
27    'Unique entry
28    oDicSearches.Add iNumResults, sToSearch
29Else
30    'Same number of results of a previous search
31    oDicSearches(iNumResults) = oDicSearches(iNumResults) & ", " & sToSearch
32End If
33
34'Last iteration (assuming we always run on all rows)
35If cint(Environment("ActionIteration")) = DataTable.LocalSheet.GetRowCount Then
36    oListResults.sort
37
38    iMaxResults = oListResults.item(oListResults.Count-1)
39    sMaxResults = oDicSearches(iMaxResults)
40
41    Reporter.ReportEvent micDone, "Max search",  sMaxResults & " got " & iMaxResults
42
43    Set oListResults = nothing
44    Set oDicSearches = nothing
45    Set oGoogleSearch = nothing
46End If


Here below you can see the GoogleSearch class. Some highlights:
·  Upon instantiation, the class_initialize subroutine checks if the browser is open on the Google page. If not, it opens a browser and navigates to the Google page.
·   Upon termination, the class_terminate subroutine closes the browser.
·   All relevant test objects are identified using inline descriptions (aka DP).

01Class GoogleSearch
02    public oBrowser
03    Public oPage
04    Public oResults
05
06    Public Function doSearch(ByVal sQuery)
07        oPage.WebEdit("name:=q").set sQuery
08        oPage.WebButton("html id:=gbqfba").click
09        oBrowser.Sync
10        Set oResults = oPage.WebElement("html id:=resultStats")
11        If oResults.WaitProperty("visible", 1, 10000) Then
12            doSearch = getNumResults()
13        Else
14            doSearch = 0
15            Reporter.ReportEvent micFail, Typename(Me), "Search did not retrieve results until timeout"
16        End If
17    End Function
18
19    Public Function getNumResults()
20        Dim tmpStr
21
22        tmpStr = oResults.GetROProperty("innertext")
23        tmpStr = split(tmpStr, " ")
24        getNumResults = Cdbl(tmpStr(1)) 'Assumes the number is always in the second entry
25    End Function
26
27    Private Sub Class_Initialize
28        Set oBrowser = Browser("title:=.*Google.*")
29        If Not oBrowser.Exist(0) Then
30            SystemUtil.Run "iexplore.exe", Environment("OPEN_URL")
31            Reporter.Filter = rfEnableErrorsOnly
32            While not oBrowser.Exist(0)
33                wait 0, 50
34            WEnd
35            Reporter.Filter = rfEnableAll
36            Reporter.ReportEvent micDone, TypeName(me), "Opened browser"
37        Else
38            Reporter.ReportEvent micDone, TypeName(Me), "Browser was already open"
39        End If
40
41        Set oPage = oBrowser.Page("title:=.*Google.*")
42    End Sub
43
44    Private Sub Class_Terminate
45        If oBrowser.Exist(0) Then
46            oBrowser.Close
47            Reporter.Filter = rfEnableErrorsOnly
48            While oBrowser.Exist(0)
49                wait 0, 50
50            WEnd
51            Reporter.Filter = rfEnableAll
52            Reporter.ReportEvent micDone, TypeName(Me), "Closed browser"
53        End If
54    End Sub
55End Class

The following function serves as a “constructor” for our GoogleSearch class (keep in mind that QTP/UFT does not support using the new operator for custom classes inside the test code; one must use it in a function library).

1Function getGoogleSearch()
2    Set getGoogleSearch = New GoogleSearch
3End Function


---Sudhakar.Mangi

No comments:

Post a Comment