HTML DOM is language independent model for representing and interacting with objects in html. It defines the structure for HTML and help in traversing through objects in HTML. QTP supports HTML DOM and following methods are used to identify an object in HTML. This approach can be useful in case QTP is not able to identify the object.
Following are the methods by which we can identify an object using HTML DOM using QTP. We will explain methods used to extract information from HTML DOM and will use the below html as reference.
<!DOCTYPE html>
<html>
<body>
<p id="para">Hello World!</p>
<p>Paragraph2</p>
<a id=”Link1” name =”LinkName” class=”LinkCls”>LinkTest</a>
<a id=”Link2” name =”LinkName” class=”LinkCls2”>LinkTest2</a>
<a id=”Link3” name =”LinkName” class=”LinkCls3”>LinkTest3</a>
</body>
</html>
GetElementsbyTagName Method
This methods gives a list of all objects with the specified TagName. For e.g.: Set objColl =Browser(“…”).Page(“….”).object.getElementsbyTagName(“a”)
This will give the list of all elements in the Page with tag name as “a”. In above sample html code, there are three elements with tag name as a. So objColl will be a collection of three elements.
In this example, we can access the link with class name as LinkTest2, we can use the below function
Public Function funcGetParticularElem(strObjName)
boolelemstat = False
set objLinkColl=
Browser("…..").Page("title:=.*").Object.getElementsByTagName("a")
For each objLink in
objLinkColl
If (objLink.classname
=strObjName) Then
objLink.click
boolelem
= True
Exit
For
End If
Next
If (boolelem= True) Then
Msgbox “Element
Found”
Else
Msgbox “element
not found”
End If
GetElementsByName Method
This gives the list of all objects matching the specified name.
For e.g.: Set objColl = Browser(“…”).Page(“….”).object.getElementsbyName(“LinkName”)
This will give the list of all elements in the Page with name as “LinkName”. Similar to above explained example on how to use HTML DOM for GetelementsbyTagName method, we can use the same for getelementsbyname method and extract useful information for the element.
GetElementByID Method
This gives the list of objects with a particular Id . Normally Id’s are unique for each object in the Page, else it picks the first element of the object satisfying the Id.
For e.g: Browser(“…”).Page(“….”).object.getElementbyId(“Link1”).click
This will select the object with Id as “Link1” in the Page and perform the requested operation on the page. Difference between GetElementById and above methods is whereas the above methods give collection of elements. This method gives the element with the Id provided. Also Id defined in the page is unique for
RunScript Method
This help to execute java script statement in QTP.
For e.g:
Browser(“…”).Page(“….”).runscript(alert(”Hello
World”);)
Once we have the collection of the objects, we can iterate through the collection and compare properties of the object like innerHTML,innerText with the expected value, and perform action on a particular object.
Once we have the collection of the objects, we can iterate through the collection and compare properties of the object like innerHTML,innerText with the expected value, and perform action on a particular object.
How to identify Object properties in the HTML Page
We can use HTML DOM to extract information from the page and is very useful in case object is not identified by QTP directly.
Suppose there are two buttons with similar properties in a web table. We can use HTML DOM to identify object based on the unique object in the next cell of web Table. To traverse the DOM structure and find properties of every element in the page, there are various inbuilt and external tools based on browsers.In IE and chrome, we can press F12 to view DOM structure and inspect elements in the html Page.
No comments:
Post a Comment