How To Count The Numbers of Links On A Page and click on a link in QTP

This is a very common scenario when automating a webpage. Say on a webpage I need to check if a particular link is available or not. Or there might be a scenario when you need to click on a particular link which is generating dynamically. If it is static then you can record and do. But which is dynamic in nature , for them we need to implement artificial intelligence.
Before we go ahead and code...Lets think about the logic.


Well , when we write a code for link we use to write like this......
< a href="">Link Name1</a>
<a href="">Link Name2</a>
so through QTP if we can read the elements of a page and check if a "A" tag is there in the page element it will be surely a link.Isn't it??


So, all the links looks like the same. And they follow the same pattern. This is small piece of code which will look like-

Set aa=description.Create
// This type of coding is called descriptive programming.
set bb=Browser("name:=Fun.*").Page("title:=Fun.*").childobjects(aa)
msgbox cc

There is another way to find out the count of link
Set oDesc = Description.Create()
oDesc("micclass").Value = "Link"
//here we are filtering the object oDesc by its default class micclass. micclass of a link is "Link"
Set Links = Browser("Browser").Page("Google").ChildObjects(oDesc)
NumberOfLinks = Links.Count()
Msgbox NumberOfLinks

even, if, you need to click on a specific link you can modify the script little bit..say i have a link called and I need to click on the mail link.
Logic is again same ...find out all the links
Now the extra is that we need to check if the link name is "Mail" . If so generate an click event.

Set aa=description.Create
'either specifying here
set bb=Browser("name:=Fun.*").Page("title:=Fun.*").childobjects(aa)
for i=0 to cc-1
if bb(i).getROProperty("name")="Mail" then//link name
exit for
end if

Hope this helps you.Njoy!!!
How To Count The Numbers of Links On A Page and click on a link in QTP How To Count The Numbers of Links On A Page and click on a link in QTP Reviewed by Animesh Chatterjee on December 21, 2008 Rating: 5

No comments:

Powered by Blogger.