Forum Discussion

rgratis's avatar
rgratis
Frequent Contributor
11 years ago
Solved

How to match a Page object to its BrowserWindow

What methods have people come up with to match a Page object to its BrowserWindow?  Both items are at the same level, as children of the Browser object.  A quick idea I had was checking for the "Focused" BrowserWindow, which I assume would contain my active page, but I'm wondering if I have missed a simpler or more robust method.

  • I contacted support as I was curious if they had a solution. This is their reply:



    ----------------------------

    [JScript]

     

    function getBrowserWindowByPage(page)

    {

      if (page != null && aqString.Compare(page.ObjectType,"Page",false) == 0)

      {

        var title = page.contentDocument.title;

        var wnd = page.Parent.FindChild("WndCaption", title + "*");

        if (wnd.Exists)

          return wnd;

      }

      return null;  

    }

     

    function main()

    {

      var browserWindowFF = getBrowserWindowByPage(Sys.Browser("firefox").Page("http://smartbear.com/"));

      var browserWindowChrome = getBrowserWindowByPage(Sys.Browser("chrome").Page("http://smartbear.com/"));

      var browserWindowIE = getBrowserWindowByPage(Sys.Browser("iexplore").Page("http://smartbear.com/"));

    }

     

    The getBrowserWindowByPage function works across all browsers. I've tested it with the Firefox v.24, Chrome v.30, and IE v.10 browsers.

    ----------------------------



    The key must be "page.contentDocument.title" which must always match up to "WndCaption" on the browser window objects. Hopefully that is it!



    I've still asked them to add it as a build-in feature.

7 Replies

  • maximojo's avatar
    maximojo
    Frequent Contributor
    I contacted support as I was curious if they had a solution. This is their reply:



    ----------------------------

    [JScript]

     

    function getBrowserWindowByPage(page)

    {

      if (page != null && aqString.Compare(page.ObjectType,"Page",false) == 0)

      {

        var title = page.contentDocument.title;

        var wnd = page.Parent.FindChild("WndCaption", title + "*");

        if (wnd.Exists)

          return wnd;

      }

      return null;  

    }

     

    function main()

    {

      var browserWindowFF = getBrowserWindowByPage(Sys.Browser("firefox").Page("http://smartbear.com/"));

      var browserWindowChrome = getBrowserWindowByPage(Sys.Browser("chrome").Page("http://smartbear.com/"));

      var browserWindowIE = getBrowserWindowByPage(Sys.Browser("iexplore").Page("http://smartbear.com/"));

    }

     

    The getBrowserWindowByPage function works across all browsers. I've tested it with the Firefox v.24, Chrome v.30, and IE v.10 browsers.

    ----------------------------



    The key must be "page.contentDocument.title" which must always match up to "WndCaption" on the browser window objects. Hopefully that is it!



    I've still asked them to add it as a build-in feature.
  • rgratis's avatar
    rgratis
    Frequent Contributor
    Bit clunky, and it doesn't guarantee the results (for example, open web pages with the same title in two different windows...), but I guess that's as good as it gets for now.



    I've found a lot of cases in the Object Browser in web testing where the tree structure does not sensibly nest objects.  I can only hope this will improve in the future.
  • maximojo's avatar
    maximojo
    Frequent Contributor
    I have the same problem. :)



    If you search down the browser window hierarchy you will find that the currently visible page is actually underneath it (as well as being under Browser which I assume is done for ease of access).



    You can search down each browser window using



    Aliases.Browser.BrowserWindow.FindAllChildren("ObjectType", "Page")



    or more page specific properties if you have them (e.g. url) to find your page. If you only have one page per browser window (i.e. not tabbed) its easy! If you happen to have more than one (i.e. multiple tabs) it seems that it will only show the one that is visible, so there will still only be one page object (I'm pretty sure).



    Thats the solution I found :)



  • rgratis's avatar
    rgratis
    Frequent Contributor
    As far as I can see in the Object Browser, only Internet Explorer is showing Page objects also under BrowserWindow.  Chrome and Firefox do not display this behavior, so it's not useful for cross-browser testing suites.



    For the moment, I'm trying to make certain only one browser window is open at a time, but it would be VERY handy to be able to tell what pages are in what windows... It looks like they get indexed in the tree by window, but the Page objects do not have an Index property, which makes that useless.
  • maximojo's avatar
    maximojo
    Frequent Contributor
    hmm that is annoying... hopefully smartbear will add something soon to allow for this! it should be something we can do easily!
  • redwoodi's avatar
    redwoodi
    New Contributor
    I have raised a support case to try and get them to take a look at this, hopefully as they realise the strength of feeling amongst the testing community for this functionality, they will include this functionality within their next release of Test Complete.
  • I tend to use the actual URL for stuff like this as it can be found at some point in both the page and browserwindow objects. Then it's a matter of going up and down the object tree as appropriate to get the object/property you want.



    It's what I use in tests where I have multiple browser windows open.



    Of course, if your windows have an identical URL open, this does not work.



    And you're right, the way IE & Chrome (I don't really use FireFox here so not so sure about that) group their object is different. Which is kind of annoying. I tend to have a couple of centralised routines for browser handling and branches within them for IE and Chrome.



    I'd rather SmartBear didn't change them as I'd have some refactoring to do if they did!