The Challenge:
A few months back, we implemented a test for a multiple-tab feature that involved opening tabs with CTRL/COMMAND + t
and closing them with CTRL/COMMAND + v
keyboard shortcuts.
Key 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);
};
Lately, this test has been failing with an error message stating Expected 1 to be greater than 1
, indicating that the new tab was not being opened. Furthermore, both keyboard shortcuts have ceased to work.
What's causing the issue with opening and closing tabs using shortcuts?
We're currently using Protractor 2.1.0 and ChromeDriver 2.15 (also tested with the latest 2.16, with no success).
Insights and Additional Details:
Initially, I suspected it might be related to Chrome 44:
- Keys.ENTER, Keys.TAB, Keys.SPACE are not working on Chrome 44
- Input.dispatchKeyEvents handles some keys incorrectly
However, when testing on older Chrome versions using
BrowserStack
, the problem persisted.The functionality works flawlessly in Firefox.
- I can see the chord being sent to the
body
element in the logs onBrowserStack
, but it doesn't trigger any action in the browser. - I managed to get the code working on Windows. Therefore, the issue seems to be specific to Mac OS.
I attempted various methods of sending the keys. Here are some alternatives I tried:
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"));
I also switched to the beta channel and encountered the same issue on Chrome 46.
As a temporary solution to open a tab, I resorted to performing
CTRL/COMMAND + SHIFT + click
on a link within 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 challenge here is that I still can't close the tab as
CTRL/COMMAND + w
isn't functioning.This issue isn't limited to Protractor. Here's a snippet of Python code that opens google.com, enters "testing" in the search field, and sends
COMMAND + A
to select the text in the input box. While this behaves as expected in Firefox, it doesn't 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()