SergeySh
SergeySh

Reputation: 13

How to open new session of chromedriver after driver.quit?

I'm trying to run two feature files (Cucumber + JUnit) and I need to close browser between features. But after closing, second feature couldn't init new session and I'm getting

org.openqa.selenium.remote.SessionNotFoundException: Session ID is null.

Using WebDriver after calling quit(). How can I avoid this error?

I have below code for the same :

 @Before
public void initPage(){
    loginPage = PageObjectFactory.getPageObject(LoginPage.class);
    loginPage.loadPage();
}

@Given("^user login to stg \"([^\"]*)\" with \"([^\"]*)\" and \"([^\"]*)\"$")
public void userLoginToStgWithAnd(String arg0, String arg1, String arg2) throws Throwable {

    Properties prop = new Properties();
    InputStream input = null;
    input = new FileInputStream("src/main/resources/application.properties");
    prop.load(input);

    arg0 = System.getProperty("url");
    arg1 = prop.getProperty("user.username");
    arg2 = System.getProperty("user.password");
    Map<IElement, String> map = new LinkedHashMap<>();
    map.put(loginPage.stgPassword, arg2);
    map.put(loginPage.stgUsername, arg1);
    driver = WebDriverConfig.setChromeProfile();
    driver.get(arg0);
    Thread.sleep(3000);
    loginPage.waitForJStoLoad();
    loginPage.fillForm(map);
    loginPage.clickStgLogin();
    Thread.sleep(3000);
}

@And("^user logout$")
public void userLogout() throws Throwable {
    loginPage.waitForJStoLoad();
    loginPage.waitForAngular();
    loginPage.openUserProfile();
    loginPage.clickLogout();
    loginPage.waitForJStoLoad();
    driver.close();
}

Upvotes: 1

Views: 11533

Answers (2)

Mohsin Awan
Mohsin Awan

Reputation: 1172

You can do it like this:

driver.quit();
driver = null; 
driver = new ChromeDriver();

Upvotes: 3

murali selenium
murali selenium

Reputation: 3927

if you want to close browser then you can use

 driver.close() //in java, this will close only active browser, session will not killed.

This link will helps you for more info How to close the whole browser window by keeping the webDriver active?

Upvotes: 0

Related Questions