Im just learning testng and am having trouble executing groups. When I run the Alltests class individually I have no issues and it completes the tests successfully. But when I try to run it through the testng.xml file I get the following error.
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.support.ui.WebDriverWait.until(java.util.function.Function)" because "this.wait" is null
Ive tried a few things to see if I could identify the issue but was unsuccessful. I tried a print statement in the setup method to see if it even went through that part but it didnt print. So it doesnt even go through the baseclass?
I also tried a few alternative tests to see if there were any issues with testng. I ran a few print statements in a set up and extended class and it worked. But when I tried it with groups i got a similar error. So i think there might be an issue with how ive done the groups?
Please check my code and let me know if I have made any mistakes.
My BaseClass
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import java.time.Duration;
public class BaseClass {
protected WebDriver driver;
protected WebDriverWait wait;
Duration timeout = Duration.ofSeconds(50);
@BeforeMethod
public void setup() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
wait = new WebDriverWait(driver, timeout);
driver.get("http://localhost/Work%20management%20system/");
System.out.println("Driver and wait initialized.");
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
My Alltests class
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.Assert;
import org.testng.annotations.Test;
public class Alltests extends BaseClass{
private void loginProcess(String username, String password){
WebElement login = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/center[2]/a")));
login.click();
WebElement usernamefield = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/form/input[1]")));
usernamefield.sendKeys(username);
WebElement passwordfield = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/form/input[2]")));
passwordfield.sendKeys(password);
WebElement submit = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/form/button")));
submit.click();
}
@Test(groups = {"loginTests"})
public void shouldfail(){
loginProcess("[email protected]", "doughnuts");
boolean isErrorMessagePresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div"))).isDisplayed();
boolean isAnnouncementAbsent = driver.findElements(By.xpath("/html/body/div/div[3]/div")).isEmpty();
Assert.assertTrue(isErrorMessagePresent, "Error message should be present");
Assert.assertTrue(isAnnouncementAbsent, "Announcement button should be absent");
if (isErrorMessagePresent && isAnnouncementAbsent){
System.out.print("Test case passed access denied");
}
}
@Test(groups = {"loginTests"})
public void shouldAlsoFail(){
loginProcess("[email protected]", "danny1234");
boolean isErrorMessagePresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div"))).isDisplayed();
boolean isAnnouncementAbsent = driver.findElements(By.xpath("/html/body/div/div[3]/div")).isEmpty();
Assert.assertTrue(isErrorMessagePresent, "Error message should be present");
Assert.assertTrue(isAnnouncementAbsent, "Logout button should be absent");
if (isErrorMessagePresent && isAnnouncementAbsent){
System.out.print("Test case passed, access denied");
}
}
@Test(groups = {"loginTests"})
public void willFail(){
loginProcess("[email protected]", "danny128934");
boolean isErrorMessagePresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div"))).isDisplayed();
boolean isAnnouncementAbsent = driver.findElements(By.xpath("/html/body/div/div[3]/div")).isEmpty();
Assert.assertTrue(isErrorMessagePresent, "Error message should be present");
Assert.assertTrue(isAnnouncementAbsent, "Announcement button should be absent");
if (isErrorMessagePresent && isAnnouncementAbsent){
System.out.print("Test case passed access denied");
}
}
@Test(groups = {"loginTests"})
public void accessAllowed(){
loginProcess("[email protected]", "danny1234");
boolean isAnnouncementPresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div[3]/div"))).isDisplayed();
Assert.assertTrue(isAnnouncementPresent, "Announcements should be present");
if (isAnnouncementPresent){
System.out.print("Test case passed access is granted");
}
}
}
My tesng.xml file
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test Group">
<groups>
<run>
<include name="loginTests"/>
</run>
</groups>
<classes>
<class name="Alltests"/>
</classes>
</test>
</suite>
and my pom file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>LoginTests</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version> 4.21.0 </version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.8.1</version>
</plugin>
</plugins>
</build>
</project>