I get java.lang.NullPointerException at @FindBy WebDriver I get java.lang.NullPointerException at @FindBy WebDriver selenium selenium

I get java.lang.NullPointerException at @FindBy WebDriver


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


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

}