NullPointer Exception when running testcases as groups from xml file

75 views Asked by At

I am new in selenium. I tried to run two classes from an XML file. I grouped tests in these two classes. But when I add tag in XML file it gives a nullpointer exception. But when I run it with , it works fine.

xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Annotation running">

    <test name="Annotation Test Runner With Group!" >
        <groups>
            <run>
                <include name="smoke"/>
            </run>
        </groups>
        <classes>
            <class name="DemoPractise.Annotation"/>
            <class name="DemoPractise.annotation2"/>

        </classes>
    </test>

</suite>

Erro Log

java.lang.NullPointerException
    at DemoPractise.Annotation.test2_BtsListBox(Annotation.java:31)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
    at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:664)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:227)
    at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
    at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:957)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:200)
    at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
    at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.testng.TestRunner.privateRun(TestRunner.java:848)
    at org.testng.TestRunner.run(TestRunner.java:621)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:443)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397)
    at org.testng.SuiteRunner.run(SuiteRunner.java:336)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
    at org.testng.TestNG.runSuites(TestNG.java:1114)
    at org.testng.TestNG.run(TestNG.java:1082)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105)


java.lang.NullPointerException
    at DemoPractise.annotation2.test4_JQueryListBox(annotation2.java:32)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
    at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:664)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:227)
    at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
    at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:957)
    at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:200)
    at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
    at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.testng.TestRunner.privateRun(TestRunner.java:848)
    at org.testng.TestRunner.run(TestRunner.java:621)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:443)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397)
    at org.testng.SuiteRunner.run(SuiteRunner.java:336)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
    at org.testng.TestNG.runSuites(TestNG.java:1114)
    at org.testng.TestNG.run(TestNG.java:1082)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105)


===============================================
Annotation running
Total tests run: 2, Passes: 0, Failures: 2, Skips: 0
===============================================


Process finished with exit code 0

I tried setuping the driver in @BeforeMethod. But it isn't inializing when using tag to name the groups in xml file.

2

There are 2 answers

0
Kaiser On

This problem happened because I didn't add the @BeforeSuite and @AfterSuite methods to a group and add them to the XML file. I thought these two methods would run whether I included them in the XML file or not! With only 'exclude,' it runs because it runs all the techniques except the methods in the provided group.

3
Nael Marwan On

The NullPointerException you are encountering in your test class is likely due to the way you have initialized the WebDriver instance and the test methods in your class. The problem is that the WebDriver instance is being initialized outside of any test method, causing it to be null when the test methods are executed. To fix this issue, you should initialize the WebDriver instance within each test method or using TestNG's @BeforeMethod annotation.

WebDriver driver;

    @BeforeSuite
    public void browserSetup() {
        WebDriverManager.chromedriver().setup(); // You should setup WebDriverManager for Chrome
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test(groups = "original")
    public void test1_BtsDatePicker() {
        driver.get("https://www.lambdatest.com/selenium-playground/");
        driver.findElement(By.linkText("Bootstrap Date Picker")).click();
    }

    @Test(groups = {"regression", "smoke"})
    public void test2_BtsListBox() {
        driver.get("https://www.lambdatest.com/selenium-playground/");
        driver.findElement(By.linkText("Bootstrap List Box")).click();
    }

    @AfterSuite
    public void tearDown() {
        if (driver != null) {
            driver.quit(); // Use quit to close the browser and release resources
        }
    }