Intellij Idea 从xml文件中以组形式运行测试用例时出现NullPointer异常

pvabu6sv  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(116)

我是selenium的新手。我尝试从XML文件运行两个类。我将测试分组在这两个类中。但是当我在XML文件中添加标记时,它会给出一个空指针异常。但是当我使用运行它时,它工作正常。
xml文件

<?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>

字符串
错误日志

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


我尝试在@BeforeMethod中设置驱动程序。但是当使用标记在xml文件中命名组时,它不会初始化。

ahy6op9u

ahy6op9u1#

您在测试类中遇到的NullPointerException很可能是由于您在类中初始化WebDriver示例和测试方法的方式造成的。问题是WebDriver示例在任何测试方法之外初始化,导致它在执行测试方法时为null。要修复此问题,您应该在每个测试方法中或使用TestNG的@BeforeMethod注解初始化WebDriver示例。

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
        }
    }

字符串

eyh26e7m

eyh26e7m2#

这个问题的发生是因为我没有将@BeforeSuite和@AfterSuite方法添加到一个组中并将它们添加到XML文件中。我以为无论我是否将它们包含在XML文件中,这两个方法都会运行!只有'exclude',它会运行,因为它会运行除了提供的组中的方法之外的所有技术。

相关问题