使用Junit运行硒测试时出现空指针异常

时间:2019-05-28 02:26:15

标签: java selenium-webdriver junit

我必须单击网页(http://automationpractice.com/index.php)上的“连衣裙”按钮,我可以打开URL,下一步是单击“连衣裙”按钮,但是通过运行硒测试时出现以下错误junit:

java.lang.NullPointerException
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
    at org.openqa.selenium.interactions.PointerInput$Origin.fromElement(PointerInput.java:221)
    at org.openqa.selenium.interactions.Actions.moveInTicks(Actions.java:418)
    at org.openqa.selenium.interactions.Actions.click(Actions.java:324)
    at com.avantica.pageObject.HomePage.clickDresses(HomePage.java:29)
    at Runner.RunnerWebTest.homePageTest(RunnerWebTest.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)


我的Junit类如下:

public class RunnerWebTest extends DriverFactory {

    HomePage hp;

    @Test
    public void homePageTest() throws Exception {
        hp = new HomePage(getDriver());
        hp.getUrl();
        hp.clickDresses();
    }

}

首页类如下:

public class HomePage extends BasePage {

    ReadConfigFile config = new ReadConfigFile();

    public @FindBy(xpath = "(//a[contains(@class,'sf-with-ul')])[4]") WebElement dresses;

    public HomePage(WebDriver driver) throws IOException {
        super();
    }

    public HomePage getUrl() throws IOException {
        getDriver().get(config.getURL());
        return new HomePage(driver);
    }

    public HomePage clickDresses() throws Exception {
        System.out.println("Dresses button to click");
        System.out.println("driver=" + driver);
        Actions act = new Actions(driver);
        act.click(dresses).perform();
        return new HomePage(driver);
    }

}

1 个答案:

答案 0 :(得分:1)

我可以通过以下方法解决此错误:

  public HomePage(WebDriver driver) throws IOException {
        super();
    }

  public HomePage(WebDriver driver) throws IOException {
        this.driver = driver;
    }