How can i open a new tab using protractor and Chrome browser

前端 未结 4 1325
误落风尘
误落风尘 2020-12-11 09:18

Here is a code (new tab doesn\'t open):

//open new tab in Chrome

browser.actions().sendKeys(protractor.Key.CONTROL +\'t\         


        
相关标签:
4条回答
  • 2020-12-11 09:28

    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

    0 讨论(0)
  • 2020-12-11 09:29

    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);
        });
      }
    
    }
    
    0 讨论(0)
  • 2020-12-11 09:42

    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.

    0 讨论(0)
  • 2020-12-11 09:43

    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')));
    });
    
    0 讨论(0)
提交回复
热议问题