0

Meta -

OS: OSX El capitan Selenium Version: 2.53.0 Browser: Safari Browser Version: 9.1 SafariDriver version: 2.48.0 which is latest as per SeleniumHQ site.

Expected Behavior -

SafariDriver Launcher page/tab should get closed automatically and get switched to first tab where my URL is getting opened.

Actual Behavior -

Selenium is getting stuck on SafariDriver Launcher page[Tab] by not closing it and switching back to first Tab

Steps to reproduce -

Trying to execute n number of test cases from TestNG.xml, but few TCs are getting executed properly by closing SafariDriver Launcher page[tab]. But for few cases Selenium is getting stuck on SafariDriver Launcher page[Tab] by not closing it and switching back to first Tab where my URL will get opened.

Required Solution -

I wanna first check how many tabs are currently opened by:-

if(browser.equals("safari")){
capability=DesiredCapabilities.safari();
capability.setBrowserName("safari");
capability.setPlatform(Platform.MAC);
capability.setVersion("9.1");
capability.setJavascriptEnabled(true);
driver=new RemoteWebDriver(new URL(grid_url),capability);
driver.get(url);
Thread.sleep(2000);
String windowHandle = driver.getWindowHandle();
ArrayList tabs = new ArrayList (driver.getWindowHandles());
if(tabs.size()>1){

        <***Need a solution here to close second tab and switch to   first tab***>
                    driver.switchTo().window(tabs.get(0));
        }
}

1 Answers1

0

You can use this code to close the window you want to :

for(String handle : driver.getWindowHandles()) {
        if (!handle.equals(originalHandle)) { //the window handle you want to close
            driver.switchTo().window(handle);
            driver.close(); // would close the specific tab you wanted
        }
    }

In your case originalHandle could be the handle for the current window (tab 2) you are at.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Hey " ".this thanks for answer, Let me update you whether it suits my req – Praveen Pavu May 18 '16 at 07:28
  • @PraveenPavu : sure. wc – Naman May 18 '16 at 07:30
  • org.openqa.selenium.NoSuchWindowException: The driver is not focused on a window. You must switch to a window before proceeding. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 3 milliseconds – Praveen Pavu May 18 '16 at 07:34
  • @PraveenPavu : did you replace the `originalHandle` in code with the handle value of the tab you want to close? – Naman May 18 '16 at 07:36
  • Imgetting above exception now, Your code worked awsome...Its closing now the 2nd tab, only thing is its not focusing on first tab so that i can enter credentials on Login page. Let me try driver.switchTo().defaultContent(); and let you know – Praveen Pavu May 18 '16 at 07:36
  • Yeah i have replaced to : "windowHandle" as my case was - String windowHandle = driver.getWindowHandle(); – Praveen Pavu May 18 '16 at 07:37
  • `driver.switchTo().window(""); //this shall let you focus on the first tab` – Naman May 18 '16 at 07:37