Reputation: 47
I'm trying to execute written in Selenium with Junit. Its just one test. The test runs but I'm getting this failure message - 1) testTripPlannerJUnit(com.example.tests.TripPlannerJUnit) org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? I understand the problem is with teardown but how can i correct it.
package com.example.tests;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.github.bonigarcia.wdm.ChromeDriverManager;
public class TripPlannerJUnit {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
ChromeDriverManager.getInstance().setup();
driver = new ChromeDriver();
baseUrl = "https://www.google.com.au/.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testTripPlannerJUnit() throws Exception {
driver.get("https://transportnsw.info/trip#/");
driver.findElement(By.id("search-input-From")).click();
driver.findElement(By.id("search-input-From")).clear();
driver.findElement(By.id("search-input-From")).sendKeys("North Sydney Station");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='North Sydney Station'])[1]/following::li[1]")).click();
driver.findElement(By.id("search-input-To")).click();
driver.findElement(By.id("search-input-To")).clear();
driver.findElement(By.id("search-input-To")).sendKeys("Town Hall Station");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Town Hall Station'])[1]/following::ul[1]")).click();
driver.findElement(By.id("search-button")).click();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
Upvotes: 0
Views: 6148
Reputation: 4507
In the @After
, you have written driver.quit()
first and then you have a if
condition followed by the implicit wait
on the driver, however till the time your code reaches the implicit wait line, the driver instance has already been quit by the driver.quit()
line.
So to correct it, please try:
@After
public void tearDown() throws Exception {
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.quit();
}
Have just moved the driver.quit()
line to the end of the code and one more suggestion, in your @After
, you have put an implicit wait, but you are not interacting with any webElement there, so the implicit wait line of code can be removed as it is not required.
Upvotes: 1