Forum Discussion

Adagio's avatar
Adagio
Frequent Contributor
7 years ago

How to Get the URL of the current web page ?

Hello, I clicked on an object in IE browser, and then a new browser window opens up with the details of the object I clicked.

How can I fetch the URL of this new window, and how can I switch between these 2 windows? 

 

Thank you

UL_Automate

3 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Well, as for how to "switch" between the two windows, they are different "page" objects off the browser.  Something like

     

    browser.page('http://www.firstpage.com');

    browser.page('http://www.secondpage.com');

     

    If you send commands to a page, TestComplete automatically switches to that page.

     

    Now.. the tricky part... I'm assuming that the new window that opens up has a highly dynamic URL that you don't necessarily know ahead of time.  So, what I would do is actually do a FindAllChildren call off the browser object.  This will return all the pages.  I'm assuming you only have the two pages then.  So, you would iterate through the result list of that FindAllChildren and look for the page that is NOT equal to the URL of your first page... that's the one you want.... then, once you have the object, it's a matter of calling

     

    foundPage.URL

     

    to get the URL for the new page.  Something like

     

    allPages = Aliases.browser.FindAllChildren('ObjectType', 'Page', 1, true);
    for (var i=0;i < allPages.length; i++) {
        if (allPages[i].URL != 'http://www.firstpage.com/) {
            foundPage = allPages[i];
            break;
        }
    }
    Log.Message(foundPage.URL);

     

     

    • Adagio's avatar
      Adagio
      Frequent Contributor

      Thanks Rob! This idea definitely looks good to me when I would like to do the comparison between the URLs.

      Now, This is the situation:

       

      You are right. New browser window has a dynamic path, and I can go back to the main window and open up another window by clicking on a different object in the same HTML table. I would like to do some verification on these 2 new windows and close them. What I'm trying to find is the way I can switch between these 3 windows by mentioning which window I would want to look at, fetch it's URL to confirm that it has a specific substring, verify some elements/text on the page, and close it.

       

      I thought of taking some help from the recording feature. After clicking the object, when the new window opened up, TestComplete named it as :

      browserWindow = browser.BrowserWindow2;

       

       

      When I manually restored the main window(without doing Alt+Tab to switch between) it started recognizing the main window as:

       

      MSTaskListWClass = Aliases.explorer.wndShell_TrayWnd.ReBarWindow32.MSTaskSwWClass.MSTaskListWClass;

      MSTaskListWClass.ClickButton();

       

      It did the same in a different way when I was switching between the windows using (ALT + TAB).

       

       

      So, is there a way I can use Page object or method to switch between these windows and fetch the URLs ? 

       

      Appreciate your help!

       

      Thank you

      Abhi

       

       

       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Well, first question: Do I know you outside this forum?  Most people here call me "Robert" so for you to call me "Rob" suggests I know you somehow. :)

         

        Question about these two dynamic windows that open up... is there anything common in the URL when it opens up that you could use for mapping? You can wildcard URL's in your namemapping so, even if part of the URL is dynamic, if there is enough static information in the URL to indicate it is the page that comes from that particular kind of object, you could map it that way.  You can then still grab the URL property to find the substring to do that modification.  Then, interacting with the different pages should be simply a matter of sending commands to the different mapped pages.  Same thing with validating objects, fields, etc., on those two pages.  If you can map them, then you should have it pretty easy.

         

        As to what's happening with doing the switching between tabs... that's a little strange.  Not sure what's going on there.  I'd have to investigate further.