I am running automated Selenium tests with TestNG. When I run as TestNG Suite, everything works just fine. But when I run as Maven Test, they fail. I'm super confused why they would work under one scenario and not another. Here is a general layout of the tests:
Base.java
public class Base {
public WebDriver driver = null;
public WebDriver getLocalDriver() {
driver = new FirefoxDriver();
return driver;
}
TestBase.java
public class TestBase extends Base {
@BeforeTest
public void beforeTest() {
Base b = new Base();
driver = b.getLocalDriver();
}
@AfterTest
public void afterTest() {
driver.quit();
}
LoginTests.java
public class LoginTests extends TestBase {
@Test
public void TestOne() {
driver.get("http://www.company.com");
// ^^^ driver == null right here (line 28)
}
Error message:
java.lang.NullPointerException
at com.company.automation.ecom.tests.LoginTests.TestOne(LoginTests.java:28)
edit: pom.xml
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<groupId>com.company.automation</groupId>
<artifactId>ecom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ecom</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<!--<scope>test</scope> -->
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
</project>
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ECOM Suite" parallel="tests" thread-count="2">
<test verbose="2" name="Chrome">
<parameter name="browserName" value="chrome"/>
<parameter name="nodeURL" value="http://10.11.25.170:5552/wd/hub"/>
<packages>
<package name="com.company.*"></package>
</packages>
</test>
<test verbose="2" name="Firefox">
<parameter name="browserName" value="firefox"/>
<parameter name="nodeURL" value="http://10.11.25.170:5551/wd/hub"/>
<packages>
<package name="com.company.*"></package>
</packages>
</test>
</suite>
run as Maven Test
? How does your pom file look like ? Which plugin do you use for running? – khmarbaiseRun As
->Maven Test
it fails. But when I right click ontestng.xml
and selectRun As
->TestNG Suite
it passes. I'm trying to get the project to run in maven so I will have more options when integrating the project with Jenkins. Also, added the pom.xml. – kroe761testng.xml
look like? – Grzegorz Górkiewicz