Razor Razor - 6 months ago 44
Java Question

NullPointerException in Selenium WebDriver Instance When Using in TestNG

I have created a Selenium test suite using TestNG for my website.

The name of my project is Test My Website. In order to execute my Selenium test script and create a test report, I execute the TestNG.xml file directly from the command prompt. For different modules of my website, I have created different Java classes for different modules and have kept them in one package. The source code of my files is given as follows:

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test My Website">
<test name="My Website Test">
<packages>
<package name="testmywebsite" />
</packages>
</test>
</suite>


TestLogin.java

public class TestLogin {
public static WebDriver driver;

@BeforeTest
public void setup() {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}

@Test
void loginTest() {
//Code to perform the login test
}

@AfterClass
public void setupWorkHistory() {
TestModule1.driver = driver;
}

}


TestModule1.java

public class TestModule1 {
public static WebDriver driver;

@Test
void module1Test() {
//Code to perform the module 1 test
driver.getTitle();
}

@AfterClass
public void setupModule2() {
TestModule2.driver = driver;
}
}


TestModule2.java

public class TestModule2 {
public static WebDriver driver;

@Test
void module2Test() {
//Code to perform the module 2 test
driver.getTitle();
}

@AfterTest
public void tearDown() {
driver.close();
driver.quit();
}
}


Note that in the first two Java classes I have added the
setup<Next Class's name>()
method to pass my
driver
instance.

The problem here is that the driver instance gets successfully passed from TestLogin.java to TestModule1.java. However it throws a
NullPointerException
in the
module2Test()
method and hence it shows a failed TestNG report for
module2Test()
despite creating the
setupModule2()
method in TestModule1.java and adding the
@AfterClass
annotation to it.

Can anyone tell me why exactly is this happening here? Replies at the earliest will be highly appreciated. Thank you.

Answer

Okay I found a solution to the problem myself.

For that I edited the source code of my TestNG.xml file and my Java class files. They are given as follows:

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test RockON">
    <test name="Rockon Test" allow-return-values="true">
        <classes>
            <class name="testmywebsite.TestLogin" />
            <class name="testmywebsite.TestModule1" />
            <class name="testmywebsite.TestModule2" />
        </classes>
    </test>
</suite>

TestLogin.java

public class TestLogin {
    public static WebDriver driver;

    @BeforeTest
    public void setup() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }

    @Test
    void loginTest() {
        //Code to perform the login test
    }

    public static WebDriver getDriver() {
        return driver;
    }

}

TestModule1.java

public class TestModule1 {
    public static WebDriver driver;

    @BeforeClass
    public void setup() {
        driver = TestLogin.getDriver();
    }

    @Test
    void module1Test() {
        //Code to perform the module 1 test
    }

    public static WebDriver getDriver() {
        return driver;
    }

}

Module2.java

public class TestModule2 {
    public static WebDriver driver;

    @BeforeClass
    public void setup() {
        driver = TestModule1.getDriver();
    }

    @Test
    void module2Test() {
        //Code to perform the module 2 test
    }

    public static WebDriver getDriver() {
        return driver;
    }

}

What I did is called the classes individually from my XML file and added the allow-return-values="true" attribute to my tests, which allows the classes to allow return types. In my Java classes I instantiated my WebDriver instance in every class in a @BeforeClass method. Hence I was able to prevent my WebDriver instance from throwing a NullPointerException.

Comments