public class TestBase extends HelperMethods {
protected static final Logger LOGGER = Logger.getLogger(TestBase.class.getName());
protected final Faker faker = new Faker();
protected WebDriver driver;
protected WebDriverWait wait;
protected DashboardPage dashboard;
@BeforeMethod
@Parameters({"url"})
public void setup(String url) {
driver = Driver.get(); // Initialize the driver
wait = new WebDriverWait(driver, Duration.ofSeconds(10));
driver.manage().window().maximize();
driver.get(url);
dashboard = new DashboardPage();
}
@AfterMethod
public void logOut() {
try {
dashboard.logout(); // Ensure the driver session is valid before logging out
} catch (NoSuchSessionException e) {
LOGGER.warning("WebDriver session is already closed: " + e.getMessage());
}
}
@AfterClass
public void tearDown() {
Driver.closeDriver(); // Close the driver session
}
}
public class TestBaseCustomer extends TestBase {
protected LoginPage loginPage = new LoginPage();
@BeforeMethod
protected void setup() {
loginPage.login(Credentials.User1, getDirectPageCode());
}
}
public class ReceiptSearch extends TestBaseCustomer {
private ReceiptSearchPage belegSuchen = new ReceiptSearchPage ();
@Test
public void test1() {...}
}
public class inventoryAccountDisplay extends TestBaseCustomer {
private InventoryAccountDisplayPage inventoryaccount = new InventoryAccountDisplayPage();
@Test
public void test1() {...}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Customer XYZ">
<parameter name="url" value="https://customerXYZ.com"/>
<classes>
<class name="ABC.tests.customerXYZ.accounting.ReceiptSearch "/>
<class name="ABC.tests.customerXYZ.accounting.inventoryAccountDisplay "/>
</classes>
</test>
<!-- More tests can be added similarly -->
</suite>
When I run my test cases from testng.xml
, the first class runs perfectly fine. However, when it moves to the second one, the login throws a NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
. When I debug, I can log in and see that the driver is not null; it holds a ChromeDriver
instance. The website is open, but the login data cannot be entered, and I get the NoSuchSessionException
. Where is the problem?