alecxe
alecxe

Reputation: 473873

Cannot open and close tabs in Chrome anymore

The Problem:

Several months ago we've added a test for a multiple-tab functionality opening tabs with CTRL/COMMAND + t and closing with CTRL/COMMAND + v keyboard shortcuts.

Relevant helper functions:

this.getControlKey = function () {
    var isWin = /^win/.test(process.platform);
    return isWin ? protractor.Key.CONTROL : protractor.Key.COMMAND;
};

this.openAndSwitchToNewTab = function (url) {
    element(by.tagName("body")).sendKeys(protractor.Key.chord(this.getControlKey(), "t"));

    // failing, if new tab was not opened
    browser.getAllWindowHandles().then(function (handles) {
        expect(handles.length).toBeGreaterThan(1);
    });

    return browser.get(url);
};

Recently, it started to fail with a Expected 1 to be greater than 1 error, which means that a new tab was not opened. And, I've confirmed that both keyboard shortcuts don't work anymore.

Why did it stop opening and closing tabs with shortcuts?

Using the currently latest Protractor 2.1.0 and ChromeDriver 2.15 (also tried with the latest 2.16, no luck).


Thoughts and more information:

  1. At first, I thought it is a Chrome 44 related problem:

    But, using BrowserStack I've reproduced the problem on older Chrome versions too.

  2. It works in Firefox like a clockwork.

  3. I can actually see the chord sent to the body element in the logs on BrowserStack, but nothing happens in the browser.
  4. I can actually make the same code work on Windows. So, it's probably Mac OS specific.
  5. I've tried to change the way the keys are sent. Here are some of my tries:

    browser.actions().mouseMove(element(by.tagName("body"))).sendKeys(protractor.Key.chord(this.getControlKey(), "t")).perform();
    browser.driver.switchTo().activeElement().sendKeys(protractor.Key.chord(this.getControlKey(), "t"));
    
  6. Also switched to the beta channel and reproduced the same problem on Chrome 46.

  7. As a workaround, to open a tab, I can perform CTRL/COMMAND + SHIFT + click on any link inside the application:

    // open new tab by clicking a logo 
    var logo = element(by.css("a.logo"));
    browser.actions().keyDown(this.getControlKey()).keyDown(protractor.Key.SHIFT).click(logo).keyUp(this.getControlKey()).keyUp(protractor.Key.SHIFT).perform();
    
    // switch to a new tab
    return browser.getAllWindowHandles().then(function (handles) {
        return browser.switchTo().window(handles[handles.length - 1]).then(function () {
            return browser.get(url);
        })
    });
    

    The problem here is that I still cannot close the tab since CTRL/COMMAND + w does not work.

  8. It's not only Protractor specific. Here is a snippet of Python code that opens up google.com, put "testing" into the search field and sends COMMAND + A to the input box. In Firefox, it behaves as expected - selects the text in the input box, but that does not work in Chrome (Python 2.7, selenium 2.47.1, Chrome 46, chromedriver 2.17):

    from selenium.webdriver import ActionChains
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get('https://google.com')
    
    q = driver.find_element_by_name("q")
    q.send_keys("testing")
    
    ActionChains(driver).send_keys_to_element(q, Keys.COMMAND + "a").perform()
    

Upvotes: 8

Views: 1625

Answers (2)

alecxe
alecxe

Reputation: 473873

I think I've got much closer to understanding what is going on.

First of all, it appears to be OS-specific. My own tests on BrowserStack and Madhan's tests on Sauce Labs proved keyboard shortcuts with CONTROL work in Chrome 44 on Windows.

Here is a quote from the relevant related open chromedriver bug:

This bug is particularly noticeable on Mac, because a lot of special key handling is done in render view host view mac, which is above where we are simulating the event.

This leads to some things like ctrl+w doesn't close the window (on windows), command+a does select all on mac, etc. This is unfortunate but is able to be worked around for the most part.

Upvotes: 0

Madhan
Madhan

Reputation: 5818

I've tested with sauclabs from chromeVersion 38 to 44

And it worked only on 44.v44.0.2403.125 to be precise on saucelabs[Tested on local[44.0.2403.130 m] as well]

And I used ChromeDriver 2.17 and SeleniumWebDriver 2.47.1

I've tried it in Java only.Hope it will be the same for Javascript

public static void doChromeTest(WebDriver driver) {
    for (int i = 0; i < 10; i++) {
        openTab(driver);
    }
    System.out.println("After Open Tab " + driver.getWindowHandles().size());
    for (int i = 0; i < 10; i++) {
        closeTab(driver);
    }
    System.out.println("After Close Tab " + driver.getWindowHandles().size());
    driver.quit();
}

public static void openTab(WebDriver driver) {
//Both will work
   //driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL, "t"));
    new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, "t")).build().perform();
}

public static void closeTab(WebDriver driver) {
//Both will work
    //driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL, "w"));
    new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, "w")).build().perform();
}

Upvotes: 3

Related Questions