Here is a code (new tab doesn't open):
//open new tab in Chrome
browser.actions().sendKeys(protractor.Key.CONTROL +'t').perform();
If we used code with 'a' - everything is fine:
//select all on the page
browser.actions().sendKeys(protractor.Key.CONTROL +'a').perform();
protractor v.1.3.1
Chrome v.37
ChromeDriver v.2.10
WebDriver v.2.43
If you really don't want to add an element to your DOM, then you can try this:
let url = https://google.com;
return browser.executeScript("return window.open(arguments[0], '_blank')", url);
//opens google.com in a new tab (works fine with Chrome. P.S. have only tested
// Chrome with Protractor).
I had tried the above statement with a browser.wait(), see if you really need the wait as browser.executeScript() returns a promise itself, can just utilize the promise's success.
Also, I have observed that although it seems that the focus of the browser has changed to the newly opened tab, I was unable to access the elements of the new tab. To do that:
browser.getAllWindowHandles().then((handles) => {
browser.switchTo().window(handles[1]); // pass the index, here assuming that
// there are only two tabs in the browser
})
To know more about window.open(), you can visit this.
Selenium doesn't provide a way to do this so a workaround seems to be the only way. Assuming you're in Windows or Linux, your CTRL+T idea should be written as below, however that hack failed for me:
browser.actions().keyDown(protractor.Key.CONTROL).sendKeys('t').perform();
Even attempting to do it on an element:
$$('input').first().sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "t"));
Good news is the following hack does seem to work, feel free to replace location.href with the url you want to open:
browser.driver.executeScript(function() {
(function(a){
document.body.appendChild(a);
a.setAttribute('href', location.href);
a.dispatchEvent((function(e){
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
return e;
}(document.createEvent('MouseEvents'))))}(document.createElement('a')));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With