Quick Test Professional (QTP) is an automation testing tool by Hewlett Packard that help automation engineers to create and execute almost any type of test on an application. In this post, l just want to write a little function that you can use to google a word and choose one of the links that showed up in google and open it. QTP  programming is done with VBscript. VBscript is a scripting language that can be used to do many stuff in any standard html based site. However, VBscript is supported only by Internet Explorer, as of the time of writing this post or to the best of my knowledge.

So the idea for this function is to define a keyword, give it to the function in addition to the link that you want it to click in google after the search results showed up. Here we go,

Function myGoogler(keyword,linkNumberToClick)

‘Open google using the syntax below

SystemUtil.Run “www.google.com”,””,””,Open

‘Lets get google search box object

Set searchBox = Browser(“Title:=Google”).Page(“Title:=Google”).WebEdit(“name:=q”)

‘Now let’s enter the keyword in the search box

searchBox.Set keyword

‘Let’s hit search

searchBox.Submit

‘Let’s now get the results page after we hit search

Set resultPage = Browser(“Title:=” & keyword & “-Google Search.*”).Page(“Title:=” & keyword & “-Google Search.*”)

‘Now we need to get the major links for our search on the result page

‘Lets describe the major links as they are classified in google

Set majorLink = Description.create()

majorLink(“class”).value = l

majorLink(“abs_x”).value = 148

majorLink(“height”).value = 18

‘Now we need to select the link that we want to click on

majorLink(“index”).value = linkNumberToClick

‘Now let’s get an object containing that link we described

Set resultLinkObject = resultPage.Link(majorLink)

‘Now click on the link

resultLinkObject.Click

‘Assume we want to output the url that this link takes us to in QTP Result

Reporter.ReportEvent micDone, “Search Link Page”, resultLinkObject.GetRoProperty(“href”)

‘You can do watever you want to do with the search results

End Function

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *