0

I have to click Dresses button on the web page (http://automationpractice.com/index.php), I can open the url and my next step is to click the Dress button, but I am getting following error when I run the selenium test via 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)


My Junit Class is like below:

public class RunnerWebTest extends DriverFactory {

    HomePage hp;

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

}

Home page class looks like:

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);
    }

}
3
  • dresses is null. Commented May 28, 2019 at 2:29
  • @shmosel what do you mean by Dresses null? or how I can click Dresses? Commented May 28, 2019 at 2:30
  • We will need to see what getDriver() does, I'm going to guess that for some reason your driver object has not been instantiated resulting in it being null hence the NullPointerException being thrown (It's just a guess without seeing the code though) Commented May 29, 2019 at 12:36

1 Answer 1

1

I was able to fix this error by changing:

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

to

  public HomePage(WebDriver driver) throws IOException {
        this.driver = driver;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.