Thursday, 7 April 2011

QTP - Getting current browser URL

I'm going to show and explain how to get current URL which is opened in a browser.
For example, how to get URL "http://motevich.blogspot.com" from the following browser:

There are two solutions:
  1. Using GetROProperty("URL") method (run-time object property)
  2. Using "URL" property of "Object" (IE DOM Object)
Let's consider both variants.

  1. GetRoProperty("URL") method (run-time object property)

    GetRoProperty
    function reads properties of a run-time object in an application.
    If you open QTP Help on GetRoProperty function, you 'll see that this function can work with all objects:
    So, for example:
    • to check whether a link is visible or not, use:
      Browser("bname").Page("pname").Link("lname").GetROProperty("visible")
    • to get a page's title, use:
      Browser("bname").Page("pname").GetROProperty("title")
    • to check whether a window is maximized, use:
      Window("wname").GetROProperty("maximized")
    • to get width of WebButton, use:
      Browser("bname").Page("pname").WebButton("btname").GetROProperty("width")
    Others properties are described in QTP Help.

    In our case, to get URL of the current WebPage, we will use:
    Browser("bname").Page("pname").GetRoProperty("URL")
    This is a sample QTP script:
    (Click the image to enlarge it)


  2. "URL" property of "Object" (IE DOM Object)

    The sample syntax is:

    Browser("bname").Page("pname").Object.URL

    What is "Object" in the above statement?

    Actually, this is Internet Explorer Document Object Model (DOM) Object.
    You can read more about DOM here and here.
    Also, I recommend to read this interested article on working with IE DOM from Visual Basic.

    Tip:
    You can use DOM Object when running QTP test on Internet Explorer only!

    To make the long story short, I can say that using Object property you can get almost all elements and properties from Web page.

    Thus, I use URL property of Internet Explorer's DOM Object.
    All properties, collections, and methods of DOM Object are described in this MSDN article.

    Tip:
    The number of properties, accessed via DOM Object, is more bigger, than properties accessed via GetROProperty method.

    So, the result of above code is:
    (Click the image to enlarge it)
Summary:
Two ways were discussed:
  1. GetROProperty("URL") (run-time object property)
  2. "URL" property of "Object" (IE DOM Object)
GetROProperty method supports both IE & FireFox, but IE DOM Object provides more accessible properties and methods.
Both can get URL of the current Web page.

Source

1 comments:

QPT said...

A very good detailed explanation with examples.
QTPbook

Post a Comment

Apex Monkeys © 2008. Design by :Yanku Templates Sponsored by: Tutorial87 Commentcute Software blogs Computers Blogs