最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i open a new tab using protractor and Chrome browser - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Sep 29, 2014 at 11:47 Oleg V asked Sep 29, 2014 at 11:14 Oleg VOleg V 411 gold badge1 silver badge4 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

If you really don't want to add an element to your DOM, then you can try this:

let url = https://google.;
return browser.executeScript("return window.open(arguments[0], '_blank')", url);
//opens google. 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')));
});

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

}

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

发布评论

评论列表(0)

  1. 暂无评论