我是硒新手。我尝试从 xml 文件运行两个类。我将测试分组在这两个类中。但是当我在 xml 文件中添加标签时,它给出了一个空指针异常。但是当我运行它时它工作正常。驱动程序首先没有初始化。
01班
package DemoPractise;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
public class Annotation {
WebDriver driver = new ChromeDriver();;
@BeforeSuite
public void browserSetup(){
driver = new ChromeDriver();
driver.manage().window().maximize();
// driver.get("https://www.lambdatest.com/selenium-playground/");
}
@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(){
driver.close();
}
}
02班
package DemoPractise;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
public class annotation2 {
WebDriver driver;
@BeforeSuite
public void browserSetup1(){
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@Test(groups = {"regression","optional"})
public void test3_JQueryDatePicker() throws InterruptedException {
driver.get("https://www.lambdatest.com/selenium-playground/");
driver.findElement(By.linkText("JQuery Date Picker")).click();
TimeUnit.SECONDS.sleep(2);
}
@Test(groups = "smoke")
public void test4_JQueryListBox() throws InterruptedException {
driver.get("https://www.lambdatest.com/selenium-playground/");
TimeUnit.SECONDS.sleep(2);
driver.findElement(By.linkText("JQuery List Box")).click();
TimeUnit.SECONDS.sleep(2);
}
@AfterSuite
public void tearDown1(){
driver.close();
}
}
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 文件中的组时,它不会初始化。
您在测试类中遇到的 NullPointerException 可能是由于您初始化 WebDriver 实例和类中的测试方法的方式造成的。问题是 WebDriver 实例是在任何测试方法之外初始化的,导致执行测试方法时它为 null。 要解决此问题,您应该在每个测试方法中初始化 WebDriver 实例或使用 TestNG 的 @BeforeMethod 注释。
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
}
}