I get java.lang.NullPointerException at @FindBy WebDriver

6.7k views Asked by At

i'm new in Selenium WebDriver, i'm trying to create some tests, but everytime when i use @FindBy in my test is not working( java.lang.NullPointerException), instead, if i use findElement, is work fine.

Error:

java.lang.NullPointerException
at 123.TestFormular.testFormular(TestFormular.java:38)
at 123.CompletareFlexi.completareFlexiCampuri(CompletareFlexi.java:29)
at 123.TestUnu.testulUnuCompletareFlexi(TestUnu.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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)

Here is my code: My Abstract Page:

package 123;

import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import parametrii.Utilitara;

public class Abstract {

    public Abstract() {
        super();
    }

    protected WebDriver driver;

    public Abstract(WebDriver driver) {
       this.driver=driver;
    }

    @Before
    public void setApp(){
        Utilitara util = new Utilitara(driver);
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.navigate().to("http://xxxxx.logon.jsp");
    }

    @After
    public void closeApp(){
        //driver.quit();
    }
}

My LogIn Page:

package 123;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class TestLogIn extends Abstract {
    public TestLogIn(WebDriver driver) {
        super(driver);
    }


    //@Test
    public void logareXXX() {
        driver.findElement(By.cssSelector("form input[type='text'][name='username']")).sendKeys("XX");
        driver.findElement(By.cssSelector("form input[name='password']")).sendKeys("XX");
        driver.findElement(By.cssSelector("form input[name='submit']")).click()
        //Assert.assertEquals(driver.findElement(By.tagName("h3")).getText(), "xxx");
        //driver.findElement(By.cssSelector("[href='logoff.do']")).click();
        //driver.findElement(By.cssSelector("#info")).getText();
        //System.out.println();
        //Assert.assertEquals("Informatii generale", driver); ("announcement")
    }
}

My way to form:

package 123;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class TestFormular extends Abstract{

    public TestFormular(WebDriver driver) {
        super(driver);
    }

    @Test
    public CompletareFlexi testFormular() throws Throwable { 
        TestLogIn TestLogIn = new TestLogIn(driver);
        TestLogIn.logareXX();

        //driver.findElement(By.linkText("xxxx")).click();
        //adaugarePresocring.click();

        Select Produs = new Select(driver.findElement(By.className("valid")));
        Produs.selectByIndex(2);                
        driver.findElement(By.cssSelector(".submitButton")).click();

        driver.findElement(By.cssSelector("form input[name='xxxx']")).sendKeys("xxxx");
        driver.findElement(By.cssSelector("form input[type='button'][onclick='xxxx()']")).click();
        //Alert alert =
                driver.switchTo().alert().accept();;
        //System.out.println(alert.getText());
        //alert.accept();

        driver.findElement(By.cssSelector("form input[type='button'][onclick='xxx();']")).click();

        PrintareProblem thr = new PrintareProblem( "PrintareProblem", 5000);
        driver.findElement(By.cssSelector(".button")).click();

        return PageFactory.initElements(driver, CompletareFlexi.class);
    }
}

Here Test FAIL, here i use @FindBy:

package 123;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import parametrii.Utilitara;

public class CompletareFlexi extends Abstract{

    //FindBy
    @FindBy(css="input[type='text'][name='price2']")  
    WebElement price2two;

    public CompletareFlexi(WebDriver driver) {
        super(driver);
    }

    public TestFormular  completareFlexiCampuri() throws Throwable{
        Utilitara util = new Utilitara(driver);
        TestFormular TestFormular = new TestFormular(driver);
        TestFormular.testFormular();
        price2two.sendKeys(Utilitara.randomNumar(5));

        //driver.findElement(By.cssSelector("input[type='text'][name='xxxxx']")).sendKeys(Utilitara.randomNumar(5));
        return PageFactory.initElements(driver, TestFormular.class);
    }
}

Here is my TEST:

package 123;

import org.junit.Test;
import org.openqa.selenium.WebDriver;

public class TestUnu extends Abstract {

    @Test
    public void testulUnuCompletareFlexi() throws Throwable {
        CompletareFlexi CompletareFlexi = new   CompletareFlexi(driver);
        CompletareFlexi.completareFlexiCampuri();
    }
}
2

There are 2 answers

3
StrikerVillain On BEST ANSWER

The WebDriver is not properly initialized because of this @FindBy is giving null pointer exception. I think the main issue is that you have used class Abstract in the wrong way. I see no use in inheriting the Abstract class in all the other classes. I understand that you needed to pass the WebDriver instance to all classes. But using inheritance for this is not the right way. Inhertance should always follow the is-a condition. Modify your classes as shown below:

Abstract Page:

package 123;

import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import parametrii.Utilitara;

public abstract class Abstract {

    protected WebDriver driver;

    // This constructor is not required
    //public Abstract(WebDriver driver) {
      // this.driver=driver;
    //}

    @Before
    public void setApp(){
        Utilitara util = new Utilitara(driver);
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.navigate().to("http://xxxxx.logon.jsp");
    }

    @After
    public void closeApp(){
        //driver.quit();
    }
}

LogIn Page:

package 123;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class TestLogIn {
    private WebDriver driver;

    public TestLogIn(WebDriver driver) {
        this.driver = driver;
    }


    //@Test
    public void logareXXX() {
        driver.findElement(By.cssSelector("form input[type='text'][name='username']")).sendKeys("XX");
        driver.findElement(By.cssSelector("form input[name='password']")).sendKeys("XX");
        driver.findElement(By.cssSelector("form input[name='submit']")).click()
        //Assert.assertEquals(driver.findElement(By.tagName("h3")).getText(), "xxx");
        //driver.findElement(By.cssSelector("[href='logoff.do']")).click();
        //driver.findElement(By.cssSelector("#info")).getText();
        //System.out.println();
        //Assert.assertEquals("Informatii generale", driver); ("announcement")
    }
}

way to form:

package 123;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class TestFormular{
    private WebDriver driver;       

    public TestFormular(WebDriver driver) {
        this.driver = driver;
    }

    @Test
    public CompletareFlexi testFormular() throws Throwable { 
        TestLogIn TestLogIn = new TestLogIn(driver);
        TestLogIn.logareXX();

        //driver.findElement(By.linkText("xxxx")).click();
        //adaugarePresocring.click();

        Select Produs = new Select(driver.findElement(By.className("valid")));
        Produs.selectByIndex(2);                
        driver.findElement(By.cssSelector(".submitButton")).click();

        driver.findElement(By.cssSelector("form input[name='xxxx']")).sendKeys("xxxx");
        driver.findElement(By.cssSelector("form input[type='button'][onclick='xxxx()']")).click();
        //Alert alert =
                driver.switchTo().alert().accept();;
        //System.out.println(alert.getText());
        //alert.accept();

        driver.findElement(By.cssSelector("form input[type='button'][onclick='xxx();']")).click();

        PrintareProblem thr = new PrintareProblem( "PrintareProblem", 5000);
        driver.findElement(By.cssSelector(".button")).click();

        return PageFactory.initElements(driver, CompletareFlexi.class);
    }
}

CompletareFlexi class

package 123;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import parametrii.Utilitara;

public class CompletareFlexi {
    private WebDriver driver;

    //FindBy
    @FindBy(css="input[type='text'][name='price2']")  
    WebElement price2two;

    public CompletareFlexi(WebDriver driver) {
        this.driver = driver
    }

    public TestFormular  completareFlexiCampuri() throws Throwable{
        Utilitara util = new Utilitara(driver);
        TestFormular TestFormular = new TestFormular(driver);
        TestFormular.testFormular();
        price2two.sendKeys(Utilitara.randomNumar(5));

        //driver.findElement(By.cssSelector("input[type='text'][name='xxxxx']")).sendKeys(Utilitara.randomNumar(5));
        return PageFactory.initElements(driver, TestFormular.class);
    }
}

**EDIT: ** You are calling CompletareFlexi class here using a normal invocation. You have to use PageFactory initialization instead.

Here is my TEST:

package 123;

import org.junit.Test;
import org.openqa.selenium.WebDriver;

public class TestUnu extends Abstract {

    @Test
    public void testulUnuCompletareFlexi() throws Throwable {
        /*
        * Change in code here
        */
        CompletareFlexi CompletareFlexi = PageFactory.initElements(driver, CompletareFlexi.class);
        CompletareFlexi.completareFlexiCampuri();
    }
}
0
Dharam On

I think you need to initialize the driver using initElements inside the constructor

Below is modified code that could solve your problem, Sorry i am new to this blog poor at formatting.

My Test

public class CompletareFlexi

{

WebDriver driver;

public CompletareFlexi(WebDriver driver){
    PageFactory.initElements(driver, this);
    this.driver = driver;

}
//FindBy
@FindBy(css="input[type='text'][name='price2']")  
WebElement price2two;



public CompletareFlexi completareFlexiCampuri() {
    price2two.sendKeys(Utilitara.randomNumar(5));

   return PageFactory.initElements(driver, CompletareFlexi.class);
}

}

And then create a class for running your test like below:

@Test

public void testulUnuCompletareFlexi()

{

CompletareFlexi input = new CompletareFlexi(driver) input.completareFlexiCampuri();

}