null指针exception_using java selenium webdriver与TestNG

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

当执行下面的代码时,会发生空指针异常,因为类Pom_MainHerokuapp的驱动程序始终为null

测试用例:-

package testcases;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import poms.Pom_MainHerokuapp;
import testbase.TestBase;

public class MainHerokuapp extends TestBase {
    Pom_MainHerokuapp mainHerokuappObject;
    public MainHerokuapp() {
        mainHerokuappObject = new Pom_MainHerokuapp(driver);
    }

    @Test(priority = 0)
    public void TestMainpagetitle() {
        mainHerokuappObject.VerifyTitles();
    }

    @Test(priority = 1)
    public void TestABTestingText() {
        mainHerokuappObject.VerifyTextOfABTesting();
    }
}

TestBase类: -

package testbase;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class TestBase {
    public WebDriver driver;

    @BeforeTest
    public void setup() {
        driver = new FirefoxDriver();
        driver.get("https://the-internet.herokuapp.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
}

也:

package poms;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;

public class Pom_MainHerokuapp {
    public WebDriver driver;
    String text;
    String StringMaintext;
    String Stringsubtitle;

    @FindBy(xpath = "//html//body//div[2]//div//h1")
    WebElement Maintitle;

    @FindBy(xpath = "//html//body//div[2]//div//h2")
    WebElement Subtitle;

    @FindBy(linkText = "A/B Testing")
    WebElement ABTesting;

    @FindBy(xpath = "//html//body//div[2]//div//div//h3")
    WebElement ABTestingText;

    create constructor of this class
    public Pom_MainHerokuapp(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this); // Initialization all webelements  
    }

    public void VerifyTitles() {
        StringMaintext = Maintitle.getText();
        Stringsubtitle = Subtitle.getText();
        System.out.println(StringMaintext);
        System.out.println(Stringsubtitle);
        Assert.assertEquals(StringMaintext, "Welcome to the Internet");
        Assert.assertEquals(Stringsubtitle, "Available Examples");
    }

    public void VerifyTextOfABTesting() {
        ABTesting.click();
        text = ABTestingText.getText();
        System.out.println(text);
        Assert.assertEquals(text, "A/B Test Variation 1");
    }
}

错误是: -

失败:在org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)上的TestMainpagetitle java.lang.NullPointerException。 38)在com.sun.proxy。$ Proxy7.getText(未知来源)at poms.Pom_MainHerokuapp.VerifyTitles(Pom_MainHerokuapp.java:36)at testcases.MainHerokuapp.TestMainpagetitle(MainHerokuapp.java:28)at sun.reflect.NativeMethodAccessorImpl。位于org.testng.internal的java.lang.reflect.Method.invoke(未知来源)的sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)的sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)的invoke0(本地方法)。 org.testng.internal的org.testng.internal.InvokeTestMethod(Invoker.java:816)上的org.testng.internal.Invoker.invokeMethod(Invoker.java:639)中的MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) .invoker.invokeTestMethods(Invoker.java:1124)在org。在org.testng的org.testng.TestRunner.privateRun(TestRunner.java:774)的org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)上的testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) .TestRunner.run(TestRunner.java:624)org.testng.SuiteRunner.runTest(SuiteRunner.java:359)org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)org.testng.SuiteRunner.privateRun( SuiteRunner.java:312)org.testng.SuiteRunner.run(SuiteRunner.java:261)org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86 )org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)atg.testng.TestNG.runSuitesLocally(TestNG.java:1140)atg.testng.TestNG.run(TestNG.java:1048)atg.testng .remote.RemoteTestNG.run(RemoteTestNG.java:112)org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

FAILED:org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)中的testABTestingText java.lang.NullPointerException,位于org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java: 38)在com.sun.proxy。$ Proxy7.click(来源不明)在poms.Pom_MainHerokuapp.VerifyTextOfABTesting(Pom_MainHerokuapp.java:46)在testcases.MainHerokuapp.TestABTestingText(MainHerokuapp.java:33)在sun.reflect.NativeMethodAccessorImpl。 invoke0(本地方法)在sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)在sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明)在java.lang.reflect.Method.invoke(来源不明)在org.testng.internal。 MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)在org.testng.internal.Invoker.invokeMethod(Invoker.java:639)在org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)在org.testng.internal .Invoker.invokeTestMethods(Invoker.java:1124)在在org.testng.TestRunner.privateRun(TestRunner.java:774)org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)在org.testng.TestRunner.run(TestRunner.java:624)在org.testng.SuiteRunner.runTest(SuiteRunner.java:359)在org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)在org.testng.SuiteRunner .privateRun(SuiteRunner.java:312)在org.testng.SuiteRunner.run(SuiteRunner.java:261)在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker。的java:86)在org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)在org.testng.TestNG.runSuitesLocally(TestNG.java:1140)在org.testng.TestNG.run(TestNG.java:1048)在org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

selenium selenium-webdriver testng
2个回答
2
投票

这不难解释。测试类MainHerokuapp的默认构造函数将在运行时立即调用,即当driver仍为null时 - 在@BeforeTest方法之前,其中driver被设置。

MainHerokuappTestBase似乎是混淆而不是明确的分离,所以你最好将它们合并为一个。

另一种方法是通过删除构造函数,在那里移动@BeforeTest并调用父类来恢复对子类的控制。这肯定有效:

public class MainHerokuapp extends TestBase {
    Pom_MainHerokuapp mainHerokuappObject;

    @BeforeTest
    public void setup() {
        super.setup();
        mainHerokuappObject = new Pom_MainHerokuapp(driver);
    }

    @Test(priority = 0)
    public void TestMainpagetitle() {
        mainHerokuappObject.VerifyTitles();
    }

    @Test(priority = 1)
    public void TestABTestingText() {
        mainHerokuappObject.VerifyTextOfABTesting();
    }
}

public class TestBase {
    public WebDriver driver;

    // @BeforeTest
    public void setup() {
        driver = new FirefoxDriver();
        driver.get("https://the-internet.herokuapp.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
}

0
投票

我也没有提到静态但我写了公共静态WebDriver driver = null时也得到了同样的错误;在TestBase类中它运行正常。

© www.soinside.com 2019 - 2024. All rights reserved.