- How to count all links on Web page?
- How to get them and click each link?
- How to get all WebEdits and check their values?
I'm going to show 4 approaches how to get lists of UI controls and process them (for example get their count).
As an example, I will work with links on Google Labs page. My goal is to get the list of links and count them.
I've added Google Labs page to my Object Repository and now it looks like:
I use Object Repository (OR) to simplify my demo-scripts.
Since the browser & the page were added to OR, we can use them later like:
Browser("Google Labs").Page("Google Labs").
Now we are ready to start!
- QTP Descriptive Programming (QTP DP) and ChildObjects QTP function
The approach uses Description object, which contains a 'mask' for objects we would like to get.
QTP script is:
The result of this QTP script is:
ChildObjects returns the collection of child objects matched the description ("micclass" is "Link") and contained within the object (Page("Google Labs")).
- Object QTP property and objects collections
QTP can work with DOM:
I use Object property of Page object. It represents the HTML document in a given browser window.
This document contains different collections - forms, frames, images, links, etc.
And we use Length property to get the number of items in a collection.
The result is the same as for the previous QTP script:
- Object QTP property and GetElementsByTagName method
Again, we can get access to the HTML document and use its GetElementsByTagName method.
As the name says, GetElementsByTagName method returns a collection of objects with the specified tag.
Since we are going to get all link, we should use "a" tag.
QTP script is:
The result is the following:
Note: There is another way how to select objects by tag name:
The result will be the same. 69 link will be found.
- XPath queries in QTP
The idea of this approach is to use XPath queries on a source code of Web page.
For example, "//a" XPath query returns all "a" nodes (= links) from XML file.
There is one problem. Web page contains HTML code, which looks like XML code but actually it is not.
- HTML code can contain unclosed img or br tags, XML code cannot.
- HTML code is a case-insensitive markup language, XML is a case-sensitive markup language, etc
More details here.
So, we have to convert HTML source code into XML. The converted code is named as XHTML.
You can convert HTML documents into XHTML using an Open Source HTML Tidy utility.
You can find more info about how to convert HTML code into XHTML code here.
I will use the final QTP script from this page, a bit modified:
Note: you can download tidy.exe here for above QTP script.
This QTP script leads to the same results - 69 links found:
- Bonus approah
Why don't you count all Wen page objects manually? :) Open a source code of the page and start counting :)
Just joking :)
- I shown 4 practical approaches how to count Web page links.
Similarly you can process images, webedits, etc
- Each approach gets a list of objects.
- First approach (QTP DP + ChildObjects) is the most easy
- Second & third approaches (Object + collections; Object + GetElementsByTagName) will work on Internet Explorer, because they use DOM methods
- Fours approach is biggest but it is more powerful. It allows to use complex XPath queries.
Do you like this QTP tutorial? Would you like to receive them in the future?
If yes, please subscribe to this blog RSS feed or by Email. (How to subscribe? VIDEO guide)
Do you know that you are free to use/copy/publish all my materials on your site/blog?