Forum Discussion

Alison_Hawke's avatar
Alison_Hawke
Occasional Contributor
15 years ago

Page object to Window object

How do I get a Window object from a Page object?  Page.Parent doesn't get there.  I don't want to reload the page, just get access to the Window object associated with it.

Also, if I have the Window object, how to I get the Page object without reloading the URL?

  • Hi Alison,





    You can get the 'window' object in the following way:







    A page object can be obtained in the way below:

    var page = Sys.Process("iexplore").Page("http://www.google.com");

    // or

    var page = Sys.Process("iexplore").Page("*");

  • Alison_Hawke's avatar
    Alison_Hawke
    Occasional Contributor
    I tried both methods, and neither worked.







    function FindWindow(myPage)

    {

        // starting with a Page object, can I maximise a window?

        var myWindow;

        myWindow = myPage.Application.Document.Script.window;

        myWindow.Maximise(); 

        // window did not maximize, no exception or other data

    }





    function GetPageWithoutReloading()

    {

        var myPage;

        myPage = Sys.Process("iexplore").Page("*");

        // Triggers an exception with no description.    

    }





    I can send you the code I'm using, I don't know what else to do.  We need a tool that can let us script our application and if I can't access both Page and Window objects reliably and repeatably, we can't use TestComplete at all.

  • Alison_Hawke's avatar
    Alison_Hawke
    Occasional Contributor
    OK, I tried again, I can get the page without reloading, this is good.





    However, I still can't get the window from the page.

  • Hi Alison,





    Ah, you wanted to get TestComplete's window object. Sorry for the confusion - I thought you needed to get the Internet Explorer DOM 'window' object accessible from scripts on a web page.





    If you have a single page opened, the object you need has the Sys.Process("iexplore").IEFrame(0) name:


  • Alison_Hawke's avatar
    Alison_Hawke
    Occasional Contributor
    That works for a single instance of IE, how would I be able to get the window object from my page object if there were multiple browser windows open at once?



    I already have the page object, how do I get from that page object to the window that page is displayed in?

  • Hi Alison,





    A good approach is to avoid working with several browser windows during tests to avoid possible name conflicts.





    Here is a script that does what you want:

    function test()

    {

      var page = Sys.Process("iexplore").Page("http://www.google.com/");

      var frame = getFrameForPage(page);

      if (null != frame) {

        frame.Maximize();

      }

    }





    function getFrameForPage(page)

    {

      var frames = Sys.Process("iexplore").FindAllChildren("RecordClass", "IEFrame").toArray();

      for (var i = 0; i < frames.length; i++) {

        var fPage = frames.FindChild("ObjectType", "Page", 10);

        if (true == fPage.Exists) {

          if (fPage.LocationURL == page.LocationURL) {

            return frames;

          }

        }

      }

      return null;

    }
  • Alison_Hawke's avatar
    Alison_Hawke
    Occasional Contributor
    OK, I'll try that solution.



    The reason I need this is I'm trying to tie recorded and written scripts togteher, and it's not possible when the recorded script insists on generating its own Window and Page objects.  I need to just hand them in and have the recorded part just work.  Right now it blows up with "Object not found" errors.



    Or is it just not possible to mix recoeded and written scripts?

  • Hi Alison,





    I do not see any problems in mixing recorded and written scripts. As for the 'Object not found' error you get when you try to playback a recorded test, it can be caused by an incorrect Name Mapping scheme in your project. Please reproduce such a problem, pack the entire project suite folder and send me the archive via the Contact Support form. I will look into your project.