Is there a way to close a tab in WebDriver or Protractor?

后端 未结 8 1251
悲&欢浪女
悲&欢浪女 2020-12-01 06:37

Is there a way to physically close a tab via Protractor or WebDriver?

I ask because while I know how to switch tabs programmatically, but it does not bring the acti

相关标签:
8条回答
  • 2020-12-01 06:44

    C# Version of Sakshi's answer:

       var tabs = driver.WindowHandles;
       if (tabs.Count > 1)
       {
           driver.SwitchTo().Window(tabs[1]);
           driver.Close();
           driver.SwitchTo().Window(tabs[0]);
       }
    
    0 讨论(0)
  • 2020-12-01 06:45

    I am using the command below to close the current tab after opening the link in new tab

    Instance.Driver2.SwitchTo().Window(Instance.Driver2.WindowHandles[1]).Close();
    

    Then, you can switch to the last tab by issue the command:

    Instance.Driver2.SwitchTo().Window(Instance.Driver2.WindowHandles[0]);
    
    0 讨论(0)
  • 2020-12-01 06:49

    Close all tabs except first one and switch to first tab:

    var tabs = driver.WindowHandles; // 
    
    foreach (var tab in tabs)
    {
         // "tab" is a string like "CDwindow-6E793DA3E15E2AB5D6AE36A05344C68"
         if (tabs[0] != tab) 
         {                                    
             driver.SwitchTo().Window(tab); 
             driver.Close();
         }
    }
    
    driver.SwitchTab(tabs[0]); // Switch to first tab
    driver.SwitchTo().DefaultContent(); // Switch to default frame
    

    Pay attention to last two lines, they are need to avoid such errors like OpenQA.Selenium.NoSuchWindowException: no such window: target window already closed from unknown error: web view not found

    0 讨论(0)
  • 2020-12-01 06:53

    You can use driver.close and then switch to active tab driver.SwitchTo().Window(m_driver.WindowHandles.First()); or any another available tab

    0 讨论(0)
  • 2020-12-01 06:53

    As Sakshi Singla answered, browser.driver.close() is worked for me ,Please see the sample spec.js for a Protractor Jasmine.

    describe('Window handle', function() {  
          //Each single it function is a test script
          it('Navigae to the site', function() {
            browser.driver.ignoreSynchronization = true;
            browser.waitForAngularEnabled(false);     
            browser.driver.get('http://demo.automationtesting.in/Windows.html');    
    
          });
    
          it('handle the new window', function() {
              //This will open a new popup
                element(by.buttonText('click')).click();
              var winHandles=browser.getAllWindowHandles();
              winHandles.then(function(handles) 
              {
                  var parentWindow=handles[0];
                  var popUpWindow=handles[1];
                  browser.switchTo().window(popUpWindow);
    //verify title in the new window
                  expect(browser.getTitle()).toEqual('Sakinalium | Home');
                  element(by.linkText('Contact')).click();
    //To close the popup
                   browser.driver.close();
                 //verify title in the parent window
                  browser.switchTo().window(parentWindow);
                  expect(browser.getTitle()).toEqual('Frames & windows');             
                  element(by.linkText('Open Seperate Multiple Windows')).click();
                  browser.sleep(7500);
              })
              });
    
    
    })
    
    0 讨论(0)
  • 2020-12-01 06:54

    First of all, selenium does not provide a reliable cross-browser API to work with browser tabs. A common approach to open or close a tab (although not quite reliable) is to invoke browser shortcuts for Chrome:

    • open tab: CTRL/COMMAND + T
    • close tab: CTRL/COMMAND + W

    In protractor, find the body element and "send keys" to it:

    var body = element(by.tagName("body"));
    body.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "t"))
    body.sendKeys(protractor.Key.chord(protractor.Key.CONTROL, "w"))
    

    Or, using browser.actions():

    browser.actions().keyDown(protractor.Key.CONTROL).sendKeys('t').perform();
    browser.actions().keyDown(protractor.Key.CONTROL).sendKeys('w').perform();
    

    Also, to open a new tab, there is an interesting hack (introduced here), which basically injects a new a element into the page and invokes click mouse event:

    function openNewTab (url) {
        return browser.driver.executeScript(function(url) {(
            function(a, url){
                document.body.appendChild(a);
                a.setAttribute('href', url);
                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'), url)
        );
        }, url)
    };
    

    There is also window.close() function, but it would not close the tab if it was not opened via window.open() (reference). In other words, if this is a tab you manually open, then you can use window.open() -> window.close() approach with the help of browser.executeScript().

    0 讨论(0)
提交回复
热议问题