1

I have three classes. One for getting all elements from the Webpage, one for performing actions with those elements and one for the test scripts. I get a null pointer exception when I call a function from the test script. I figured out this is because I use @FindBy annotation, but I don't know how to fix this.

Elements Class:

public class LoginPageElements {

    @FindBy(id="loginId")
    private static WebElement userNameTextBox;

    @FindBy(id="password")
    private static WebElement userPasswordTextBox;

    @FindBy(id="QTP_LoginButton")
    private static WebElement loginButton;

    public static WebElement getUserNameTextBox(WebDriver driver){
        WebElement a=driver.findElement(By.id("loginId"));
        return a;
    }

    public static WebElement getUserPasswordTextBox(){
        return userPasswordTextBox;
    }

    public static WebElement getLoginButton(){
        return loginButton;
    }
}

Actions Class:

public class LoginPageActions {

        public static void login(WebDriver driver,String username,String password){
            WebElement a=LoginPageElements.getUserNameTextBox(driver);
            a.sendKeys(username);
            LoginPageElements.getUserPasswordTextBox().sendKeys(password);
            LoginPageElements.getLoginButton().click();
        }

    }

Test script:

public class Sample {
     public static String driverPath = "D:/Selenium/Chrome Driver latest/chromedriver.exe";
     public static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", driverPath);

    ChromeOptions options = new ChromeOptions();
    options.addArguments("test-type");
    options.addArguments("start-maximized");
    options.addArguments("--js-flags=--expose-gc");
    options.addArguments("--enable-precise-memory-info");
    options.addArguments("--disable-popup-blocking");
    options.addArguments("--disable-default-apps");
    options.addArguments("--enable-automation");
    options.addArguments("test-type=browser");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions");
    options.setExperimentalOption("useAutomationExtension", false);

    driver = new ChromeDriver(options);

    driver.get("http://10.235.80.106:8080");

    LoginPageActions.login(driver,"acb", "adasd");
}

There is no exception when I pass the WebDriver object from the test script to the element class. The problem occurs when I use the elements initialized with FindBy annotations because of no WebDriver instantiation. How do I fix this? Thanks

6
  • Why are you using @FindBy and WebElement a=driver.findElement(By.id("loginId"));? Commented Jul 7, 2017 at 10:21
  • Copy in the stack trace for the null point exception to your question.. Commented Jul 7, 2017 at 10:23
  • 3
    I do not mean to offend you @kaushik3993 but you are missing knowledge about Object Oriented Programming. Try to understand what a constructor is and how does static keyword affects your code. Commented Jul 7, 2017 at 10:28
  • @Plog I was just testing to see if @FindBy is the problem. I haven't learnt PODM completely yet. Commented Jul 7, 2017 at 11:31
  • @RafałLaskowski Thanks for your suggestion. But can you tell me why I shouldn't use static methods there? I could have passed driver through a constructor to achieve that but since those were static methods, I was looking for a better solution. Commented Jul 7, 2017 at 11:37

2 Answers 2

3

You can continue to use the @FindBy annotations you just need to make sure you initialise the WebElements. To do this you should initialise your LoginPageElements using PageFactory:

LoginPageElements loginPageElements = PageFactory.initElements(webDriver, LoginPageElements.class);

where webDriver is an instance of the WebDriver you are using to run the selenium tests.

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

Comments

2

You need to declare the WebDriver instance and add the constructor in LoginPageElements & LoginPageActions class as:

  1. LoginPageElements class:

    WebDriver driver;
    
    //constructor
    public LoginPageElements(WebDriver loginDriver)
    {
        this.driver=loginDriver;
    }
    
  2. LoginPageActions class:

    WebDriver driver;
    
    //constructor
    public LoginPageActions(WebDriver loginDriver)
    {
        this.driver=loginDriver;
    }
    

Let me know if this Answers your Question.

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.