2
votes

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>
2
What do you mean by run as Maven Test ? How does your pom file look like ? Which plugin do you use for running?khmarbaise
In Eclipse. When I right click on the project and select Run As -> Maven Test it fails. But when I right click on testng.xml and select Run 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.kroe761
Are there any tests more? How does the testng.xml look like?Grzegorz Górkiewicz
As of right now, there are no more tests. This is step 1 of the project, getting the framework up and running. Then I would write more tests. I added the testng.xml as well. Part of the framework involves distributed testing using selenium grid, but right now I have all that removed so I can try to figure out how to get the test to run when invoking men test.kroe761
Once again, I can't figure out why the tests would fail when running via maven, but NOT fail when running via TestNGkroe761

2 Answers

4
votes

I'm super confused why they would work under one scenario and not another.

So was I when I first discovered that TestNG makes (good) use of multithreading.

Try changing @BeforeTest and @AfterTest annotations to @BeforeMethod and @AfterMethod. This would be the simplest solution.

Besides persisting the WebDriver in static ThreadLocal may be key to success.

Consider this:

public class TestBase {

    protected static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
    protected String abc;

    @BeforeMethod
    public void setUp(){
        System.out.println("I am in setUp method.");

        //WebDriver instantiation etc.
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized", "--disable-cache");
        webDriver.set(new ChromeDriver(options));
        webDriver.get().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @AfterMethod
    public void tearDown(){
        System.out.println("I am in tearDown method.");
        //You can clean up after tests.
        //webDriver.get().quit();
        webDriver.get().quit();
    }
}

In two separate files:

public class Test1 extends TestBase {

    @Test
    public void goToGoogle() {
        System.out.println("Google");
        String googleURL = "http://google.com/";
        webDriver.get().get(googleURL);
        assertTrue(webDriver.get().getCurrentUrl().contains("google"), "We are on Google!");
    }
}

and

public class Test2 extends TestBase {

    @Test
    public void goToStackOverflow() {
        System.out.println("SO!");
        String stackOverflowAddress = "http://stackoverflow.com/";
        webDriver.get().get(stackOverflowAddress);
        assertTrue(webDriver.get().getCurrentUrl().contains("stack"), "We are on SO!");
    }
}
1
votes

Mate you can use :

    <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-testng</artifactId>
        <version>3.0.0-M3</version>
      </dependency>

I got my maven tests executed like this.

Thanks