Saturday, August 20, 2011

QTP - How to capture suggested search results that get displayed when you enter a keyword in Google?

Say for example when you start entering a Keyword "QTP", you get some suggested search results underneath the keyword. How to capture those results and store them in Run Time Data table?


After entering the Keyword, use the Object Spy on the first result item to see the details of the webtable where all the suggested search results are stored in property Innertext




Now get the Innertext from that table and split the text based on specified delimiter and store the results in your datatable. Please see the following code:

Dim RowCount, SearchResults, Splittext, CapInnertext

Browser("Internet Explorer Enhanced").Page("qtp - Google Search").WebEdit("q").Set "QTP"

'Capture the Inner text of the web table
CapInnertext=Browser("Internet Explorer Enhanced").Page("qtp - Google Search").WebTable("Class:=gac_su").GetROProperty("Innertext")

'Specify the delimiter text based on which you want to split the text
Splittext="I'm Feeling Lucky »"

ColCount=1

datatable.SetCurrentRow(1)

SearchResults=Split(CapInnertext,Splittext,-1,1) 'Split the line based on  the delimiter.

For i=0 to ubound(SearchResults)-1

Datatable(Colcount, dtlocalsheet)=SearchResults(i) 'inserting the data in qtp local sheet
Colcount=Colcount+1 'shifting to next Column in qtp data table

Next


 '
 '
 '
Make sure that you have already created enough Columns in your datatable to store the results during run time. After you have executed the above script you can see the results in your Run Time Data Table on TEST Results



1 comment: