Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » 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.

Logic

Well , when we write a code for link we use to write like this......
< a href="xyz.com">Link Name1</a>
<a href="deb.com">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??

Code

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


SystemUtil.Run http://www.yahoo.com/
Set aa=description.Create
aa("htmltag").value="A"
// This type of coding is called descriptive programming.
set bb=Browser("name:=Fun.*").Page("title:=Fun.*").childobjects(aa)
cc=bb.count
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 yahoo.com 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.




SystemUtil.Run http://www.yahoo.com/
Set aa=description.Create
aa("htmltag").value="A"
'either specifying here
--------------------------------
aa("name").value="Mail"
-------------------------------
set bb=Browser("name:=Fun.*").Page("title:=Fun.*").childobjects(aa)
cc=bb.count
for i=0 to cc-1
if bb(i).getROProperty("name")="Mail" then//link name
bb(i).click
exit for
end if
next

Hope this helps you.Njoy!!!

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply