How can i open a new tab using protractor and Chro

2020-02-12 17:17发布

问题:

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

回答1:

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.



回答2:

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')));
});


回答3:

This piece of code works for me in TypeScript with protractor.

import {browser} from 'protractor';

export class Browser {
  public async openPageInNewTab(url: string) {
    await this.createNewBrowserTab();
    await this.switchToTabNumber(1);
    await browser.get(url);
  }

  public createNewBrowserTab() {
    browser.executeScript('window.open()');
  }

  public async switchToTabNumber(number: number) {
    return browser.getAllWindowHandles().then(function (handles) {
      const newWindowHandle = handles[number];
      browser.switchTo().window(newWindowHandle);
    });
  }

}


回答4:

I think problem with "open a new tab" in ChromeDriver, I found a bug like this: https://code.google.com/p/chromedriver/issues/detail?id=903&q=new%20tab&colspec=ID%20Status%20Pri%20Owner%20Summary