0

I am trying to write an automation framework using Java and selenium. While running Junit tests, Firefox browser starts but it cannot go to my local site. I get a NullPointer Exception. I am new to Java, any help will be greatly appreciated. Thanks

  java.lang.NullPointerException
at WordpressFramework.LoginPage.GoTo(LoginPage.java:18)
at WordpressTest.LoginTests.Admin_User_Can_Log_In(LoginTests.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

public class LoginTests {

        @Before
        public void Init()
        {
            Driver_Setup.Initialize();
        }

        @Test
        public void Admin_User_Can_Log_In(){

            LoginPage.GoTo();
            LoginPage.LoginAs("rizwan").WithPassword("*****").Login();

public class Driver_Setup {

private static WebDriver instance;

public static WebDriver getWebDriver() {
    return instance;
}

public static void setWebDriver(WebDriver instance) {
    Driver_Setup.instance = instance;
     }


           public static void Initialize()
            {   WebDriver webdriver=new   FirefoxDriver();                

      webdriver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);    }
}

public class LoginPage {

public static void GoTo() {


Driver_Setup.getWebDriver().navigate().to("http://localhost:57464/wp-login.php");


}

public static LoginCommand LoginAs(String username)
{

     return new LoginCommand(username);

}

public static class LoginCommand
{
    private   String username;
    private String password;

    public LoginCommand(String username)
    {
        this.username = username;
    }

    public LoginCommand WithPassword(String password) {
        this.password= password;
                return this;
    }

    public void Login() {
      WebElement loginInput = Driver_Setup.getWebDriver().findElement(By.id("user_login"));
      loginInput.sendKeys(username);
      WebElement passwordInput = Driver_Setup.getWebDriver().findElement(By.id("user_pass"));

      WebElement loginButton = Driver_Setup.getWebDriver().findElement(By.id("wp-submit"));

      loginButton.click();
    }
  }

1 Answer 1

3

Looks like the problem is inside Driver_Setup class - getWebDriver returns null since you have not called setWebDriver() to set the instance value.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for the info. Sorry, I am new to Java and trying to follow along a tutorial that is in C#. Would it be possible for you to say where and how to call setWebDriver() in the code
@RizwanRahmanRenesa I think it's just that you need to replace WebDriver webdriver =new FirefoxDriver() with instance = new FirefoxDriver().
Thank you so much for the solution.You are a life saver :)

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.