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.
09 | If Not lcase(typename(oListResults)) = "arraylist" Then |
10 | Set oListResults = createobject( "System.Collections.ArrayList" ) |
14 | If Not lcase(typename(oDicSearches)) = "dictionary" Then |
15 | Set oDicSearches = createobject( "scripting.dictionary" ) |
18 | Set oGoogleSearch = getGoogleSearch() |
20 | sToSearch = DataTable( "Query" , dtLocalSheet) |
21 | iNumResults = oGoogleSearch.doSearch(sToSearch) |
23 | oListResults.Add iNumResults |
26 | If Not oDicSearches.Exists(iNumResults) then |
28 | oDicSearches.Add iNumResults, sToSearch |
31 | oDicSearches(iNumResults) = oDicSearches(iNumResults) & ", " & sToSearch |
35 | If cint(Environment( "ActionIteration" )) = DataTable.LocalSheet.GetRowCount Then |
38 | iMaxResults = oListResults.item(oListResults.Count-1) |
39 | sMaxResults = oDicSearches(iMaxResults) |
41 | Reporter.ReportEvent micDone, "Max search" , sMaxResults & " got " & iMaxResults |
43 | Set oListResults = nothing |
44 | Set oDicSearches = nothing |
45 | Set oGoogleSearch = nothing |
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).
06 | Public Function doSearch( ByVal sQuery) |
07 | oPage.WebEdit( "name:=q" ).set sQuery |
08 | oPage.WebButton( "html id:=gbqfba" ).click |
10 | Set oResults = oPage.WebElement( "html id:=resultStats" ) |
11 | If oResults.WaitProperty( "visible" , 1, 10000) Then |
12 | doSearch = getNumResults() |
15 | Reporter.ReportEvent micFail, Typename( Me ), "Search did not retrieve results until timeout" |
19 | Public Function getNumResults() |
22 | tmpStr = oResults.GetROProperty( "innertext" ) |
23 | tmpStr = split(tmpStr, " " ) |
24 | getNumResults = Cdbl(tmpStr(1)) |
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) |
35 | Reporter.Filter = rfEnableAll |
36 | Reporter.ReportEvent micDone, TypeName(me), "Opened browser" |
38 | Reporter.ReportEvent micDone, TypeName( Me ), "Browser was already open" |
41 | Set oPage = oBrowser.Page( "title:=.*Google.*" ) |
44 | Private Sub Class_Terminate |
45 | If oBrowser.Exist(0) Then |
47 | Reporter.Filter = rfEnableErrorsOnly |
48 | While oBrowser.Exist(0) |
51 | Reporter.Filter = rfEnableAll |
52 | Reporter.ReportEvent micDone, TypeName( Me ), "Closed browser" |
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).
1 | Function getGoogleSearch() |
2 | Set getGoogleSearch = New GoogleSearch |
---Sudhakar.Mangi
No comments:
Post a Comment