Forum Discussion

Adagio's avatar
Adagio
Frequent Contributor
6 years ago

Closing the current Tab in the Chrome browser

Hi, 

 

I have a dashboard page open in the chrome browser. Here I search for an entity which opens up in a new tab, I veryfy some information on this and want to close this tab and go back to the dashboard page. I have to repeat this process at least 10 times for different entities. 

 

I'ven't had any success in closing the tab consistently. Most of the time, this ends up closing the 'dashboard' page itself.

 

Here is what I'm using: 

 

 

 

////////////////////Main//////////////////////////////////////////////////
function main(){
/*
Here goes the complete program to run the test
*/

// closing the current tab
CloseTab(getURL());

}

///////////////////// Close the current tab in the browser /////////////////
function CloseTab(URL)
{
  var browser = Sys.Browser("*");
   var page = browser.Page("*");
   Sys.Browser().Page(URL).Keys("^w")

}

/////////////////fetch the URL of current page in the browser///////////////
function getURL(){ 
  
  var browser = Sys.Browser("Chrome");
  var page = browser.Page("*");
  var url = page.URL;
  return url;

}

Any help would be appreciated!

 

Thank you

Abhi

5 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Every URL is a "page" in the object tree of TestComplete.  So, simply selecting Browser.Page('*') isn't going to get the current page necessarily.  The "current" page is identifed by whether or not the "Visible" property is true.

    So, in your getURL, I would actually search for whatever page object is currently visible.

    function getURL(){ 
      
      var browser = Sys.Browser("Chrome");
      var page = browser.FindChild(["ObjectType", "Visible"],["Page", true], 0);
      var url = page.URL;
      return url;
    
    }
    • Adagio's avatar
      Adagio
      Frequent Contributor

      Thank you, Robert! 

       

      I tired this solution, and it worked well the first time, but when I was re-running the tests, same old thing started occuring again. It was closing the main dashboard page. 

       

      Thank you

      Abhi

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Another possiblity then is a timing issue.  If your code to close the tab is happening before the tab is recognized by TestComplete's object recognition, that could be the issue.  That's basically what this seems to be.