Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to close a tab in WebDriver or Protractor?

Is there a way to physically close a tab via Protractor or WebDriver?

I ask because while I know how to switch tabs programmatically, but it does not bring the active tab to the foreground. I can't always tell what is going on in my E2E tests that run on SauceLabs because when I view the screen casts it is showing the tab that I navigated away from, not the active one.

Am I going about this incorrectly?

it('should do something in the previous tab', function(done) {     browser.getAllWindowHandles().then(function (handles) {         browser.switchTo().window(handles[0]);         // do something         expect(something).toEqual(thisThing);         done();     }); }); 
like image 679
Aaron Avatar asked Apr 07 '15 22:04

Aaron


1 Answers

You can try the following:

  1. Switch to the new opened tab.
  2. Close the current windows (in this case, the new tab).
  3. Switch back to the first window.

    browser.getAllWindowHandles().then(function (handles) { browser.driver.switchTo().window(handles[1]); browser.driver.close(); browser.driver.switchTo().window(handles[0]); }); 
like image 163
Sakshi Singla Avatar answered Sep 23 '22 11:09

Sakshi Singla