Tuesday 28 May 2013

DOM Uses in QTP


Wikipedia defines Document Object Model(DOM) as:

A platform- and language-independent standard object model for representing HTML or XML and related formats.

In QTP, one can understand DOM as a method for accessing the source code of a webpage directly through vb scripting. 
Web pages typically consist of graphics and text written in HTML. Web pages may also use other scripted languages such as Java, JavaScript, Common Gateway Interface (CGI), and Visual Basic script. To view the HTML source code for a Web page, right-click the Web page, and then click View Source.We can access the source page of any webpage using .object notation.

For example- if you need to check total links on a web browser, then you can use the following code:


Set Links = Browser("Google").Page("Google").Object.Links
msgbox "Total links: " & Links.Length

Common HTML DOM Methods-


getElementById Method - Returns a reference to the first object with the specified value of the ID attribute.


'Sets the value of obj to the first element in the page that has an ID of "mark":
Set obj = Browser("Browser").Page("Yahoo!").Object.getElementByID("mark")


'Sets value in web edit using element's ID:
Browser("name:=Google").Page("title:=Google").Object.getElementByID("XXXX").Value="QTP"

getElementsByName Method - Retrieves a collection of objects based on the value of the NAME attribute.



Set obj= Browser("name:=Google").Page("title:=Google").Object
.getElementsByTagName("INPUT")  
inCount=obj.Length-1  

For i=0 to inCount  
If obj(i).Name="q" and obj(i).Type="text" Then  
Browser("name:=Google").Page("title:=Google").Object.getElementsByName(obj(i).Name)(0).Value="QTP" 'Sets value in web edit using element's name.  
End If  
Next



getElementsByTagName Method - Retrieves a collection of objects based on the specified element name.


Set Parent = Browser("Mortgage Calculator").Page("Mortgage Calculator")
Set obj = Parent.Object.getElementsByTagName("input")
inCount=obj.Length-1 

For i=0 to inCount
    If obj(i).Name="param" and obj(i).Type="text" Then
        obj(i).Value="QTP"  
   End If
Next 


More Examples-

1-'find the src of the images in the web table
Set img = Browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.all.tags(“img”)
msgbox img.length

For i=0 to img.length-1
   msgbox img(i).src
Next

2-'find the column count in a web table-
Set col=Browser(“Browser”).Page(“Page”).WebTable(“Happy”).Object.cols
msgbox col.count

5 comments:

  1. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates…
    Regards,
    http://www.traininginsholinganallur.in/qtp-training-in-chennai.html

    ReplyDelete
  2. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information...QTP Training in Chennai

    ReplyDelete
  3. • Good article! There is a great need for more in-depth reviews of certain products and technologies. Your tips are really helpful for anybody who wants to create reviews of any type. Great job. Thanks.
    oracle Training in Chennai


    ReplyDelete
  4. Thank you for sharing this information and Very good looking blog
    sas Training in Chennai

    ReplyDelete
  5. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
    Azure Training in Chennai

    ReplyDelete