如何修复Java selenium TestNG中的空指针异常?

问题描述 投票:0回答:1

当我从 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");

在我的 TestNG xml 文件中:

<?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> 
java selenium-webdriver testng.xml
1个回答
0
投票

当您调试此错误时,您确定测试到达:

if (browser.equalsIgnoreCase("chrome")) {
ChromeOptions options = new ChromeOptions();
-->>>> setDriver(new ChromeDriver(options)); this row?
© www.soinside.com 2019 - 2024. All rights reserved.