7

My scenario is to close the chrome browser and open a new one.

public String openNewBrowserWindow() {
    this.log("Opening new Browser window...");
    String stringHandles;
    Set<String> previousWindows = driver.getWindowHandles();
    String previousHandle = driver.getWindowHandle();
    ((JavascriptExecutor)driver).executeScript("window.open();");
    Set<String> newWindows = driver.getWindowHandles();
    newWindows.removeAll(previousWindows);
    String newHandle = ((String)newWindows.toArray()[0]);
    stringHandles = previousHandle + ";" + newHandle;
    return stringHandles;
}

What I did is this:

String handlesA = generic.openNewBrowserWindow();
String[] handleA = handlesA.split(";");
generic.closeBrowser();
generic.switchToWindow(handleA[1]);

This works on firefox but not in chrome. Do you guys have any suggestion?

Seimone
  • 71
  • 1
  • 1
  • 4

2 Answers2

6

Why not just:

driver.quit()
driver = new ChromeDriver()

What is your scenario really?

Riaz
  • 874
  • 6
  • 15
  • 3
    This is my scenario. Launch URL. Sign in with remember me checkbox ticked. Close the browser without signing out. Re-open browser and the user should be already sign in. The thing with driver.quit() is my session will end if I do that. Am I making sense, sorry for my poor story telling. First time here. :) – Seimone Jun 09 '16 at 09:48
5

@Seimone Whenever you want to intiate a Chrome browser, system property must be defined to the chromedriver.exe

System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
WebDriver driver = new ChromeDriver();

Also, If you want to close your current chrome browser window use the following one in your code.

driver.close();

If you want to close all your chrome browser window use the following one in your code.

driver.quit();

With reference to your scenario

  1. Open the url

  2. Login with signed in

  3. Close the browser

  4. Open the browser and enter the same url

  5. Check the same user is logged in

Try the below code and let me know your result.

String chromeDriver = "enter the chromedriver.exe path";
String chromeProfile = "C:/Users/MSTEMP/AppData/Local/Google/Chrome/User Data/Default"; //Local chrome profile path.
System.setProperty("webdriver.chrome.driver", chromeDriver);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("user-data-dir="+chromeProfile);
capabilities.setCapability("chrome.binary",chromeDriver);
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
WebDriver driver = new ChromeDriver(capabilities);

driver.get("https://www.gmail.com");
/*write your login credentials code with username, password and perform the
login operation with signed in*/
driver.close();

//Now invoke the chrome browser and enter the url alone.
driver = new ChromeDriver(capabilities);
driver.get("http://www.gmail.com");
//write the code for user signed verification.
Ashok kumar Ganesan
  • 1,098
  • 5
  • 20
  • 48
  • 1
    My scenario is I need to login to my application with "KEEP ME SIGN IN" enabled and then close the browser. Re-launch the browser with the same URL and the expected result is I am already signed in. Launch URL. Sign in with remember me ticked. Close the browser. Re-launch the browser. User don't need to sign in. – Seimone Jun 09 '16 at 09:42
  • @Seimone For your scenario, I have made the changes in my answer section. Look at the edited changes and come back with the result. – Ashok kumar Ganesan Jun 09 '16 at 10:28