当我从 testng xml 文件执行测试用例时,所有测试均已成功执行,但尝试使用 xml 中的组运行时,它会抛出空指针异常。
**java.lang.NullPointerException:无法调用“org.openqa.selenium.WebDriver.get(String)”,因为“org.testpackage.TC370_Quiz.getDriver()”的返回值为空**
public class BaseClass {
public ThreadLocal<WebDriver> driver = new ThreadLocal<>();
public void setDriver(WebDriver driver) {
this.driver.set(driver);
}
public WebDriver getDriver() {
return this.driver.get();
}
// Creating object for ReadConfig class
ReadConfig readconfig = new ReadConfig();
// Calling the method from ReadConfig Class
public String baseURL = readconfig.getApplicationUrl();
// Launching browser
@Parameters("browser")
@BeforeMethod
public void launch(ITestContext context, @Optional("chrome") String browser, Method method)
throws MalformedURLException {
if (browser.equalsIgnoreCase("chrome")) {
ChromeOptions options = new ChromeOptions();
setDriver(new ChromeDriver(options));
public class QuizPage extends BaseClass {
WebDriver driver;
public SmartQuizPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
@FindBy(xpath = "//label[text()='Quiz']")
private WebElement sQuiz;
public QuizPage quizPage;
public QuizPage getQuizPage(WebDriver driver) {
return (quizPage == null) ? quizPage = new QuizPage(driver) : quizPage;
}
@Test(priority = 377, groups = { "smoke" },enabled = true)
public void testingOnStartButtonInHindiInstructionPage() throws InterruptedException {
getDriver().get(baseURL);
extentTest.info("Website launched");
PageObjectManager pom = new PageObjectManager();
pom.getResources(getDriver()).mouseHoverOnResources();
pom.getQuizPage(getDriver()).clickQuiz();
extentTest.info("Clicked quiz");
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false" thread-count="2">
<parameter name="author" value="Jey" />
<test name="Test" parallel="classes" thread-count="1" enabled="true">
<parameter name="browser" value="chrome"></parameter>
<groups>
<run>
<include name="smoke"></include>
</run>
</groups>
<classes>
<class name="org.testpackage.TC370_Quiz"></class>
</classes>
</test>
</suite>
当您调试此错误时,您确定测试到达:
if (browser.equalsIgnoreCase("chrome")) {
ChromeOptions options = new ChromeOptions();
-->>>> setDriver(new ChromeDriver(options)); this row?