'Selenium - Java - Page Factory: Read from Property file and pass value in Selenium (SendKeys)

I am stuck in this situation:

I try to get some values from a property file (first.properties) and passing to Selenium (sendKeys method) For this I have the next.

I have created an interface for keeping the Strings of a property file.

public interface Constants {

    String key_search = "search";
}

My property file:

search = "Selenium Cucumber"

This is the class that read all the properties in the program (I have more than 1 property file)

    public class MultiplePropertyReader {


    public static String ReadProps() {

        Properties properties = new Properties();


        try {
         //   properties.load(new FileInputStream("src/main/resources/data/zero.properties"));
            properties.load(new FileInputStream("src/main/resources/data/first.properties"));


            //First properties fields
            System.out.println("::: First Feature Property File 1 Data :::");
            System.out.println(properties.getProperty("search"));

            //Get the properties (First properties)
           String search = properties.getProperty(key_search);
           return search;


        } catch (Exception e) {
            System.out.println("No properties file found...");
            e.printStackTrace();
        }
        return null;
    }
}

In my Page Factory class, I have the next code which will pass the parameter to a specific field via sendKeys

public class Page_First extends BasePage {


    public Page_First() throws IOException {
        PageFactory.initElements(driver, this); }


    //////////////////////////////////////////////WEB ELEMENTS//////////////////////////////////////////////////////////

    @FindBy(name = "q")
    private WebElement searchText;

    @FindBy(name="btnK")
    private WebElement searchButton;


    //////////////////////////////////////////////BASE METHODS//////////////////////////////////////////////////////////

    public void startNavigation() {

      log.info("Accessing to Google");

    }

    public void search(String search) {

        searchText.sendKeys(search);
    }

    public void enterButton (){

        clickElement(searchButton);
    }
}

This is the step of the Step Definition class where it passes the parameter

@When("I query for \"([^\"]*)\" cucumber spring selenium")
    public void I_query_for_cucumber_spring_selenium (String search)  {

        page_first.search(search);
    }

When I run the program with IntelliJ, the next issue is displayed:

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy15.sendKeys(Unknown Source)
    at pages.Page_First.search(Page_First.java:39)
    at stepdefs.Step_First.I_query_for_cucumber_spring_selenium(Step_First.java:28)
    at ✽.When I query for "<search>" cucumber spring selenium(first.feature:10)

If somebody can help me... Best regards.

UPDATED: Property file reader was updated and here I post My feature file.

Feature: Navigation Test

  As a user, I would like to make a search in Google
  So I want to navigate in the page

  Scenario: Search google to verify google search is working

    Given I go to Google
    When I query for "<search>" cucumber spring selenium
    And click search
    Then google page title should become the first page


Solution 1:[1]

it seems that you are not passing any values to

Page_First.search

And you have mentioned reading from property file and passing value to send keys, But MultiplePropertyReader.ReadProps() is not used too.

Probably it should be like:

    public class MultiplePropertyReader {


        public static String ReadProps() {

            Properties properties = new Properties();


            try {
             //   properties.load(new FileInputStream("src/main/resources/data/zero.properties"));
                properties.load(new FileInputStream("src/main/resources/data/first.properties"));


                //First properties fields
                System.out.println("::: First Feature Property File 1 Data :::");
                System.out.println(properties.getProperty("search"));


                //Get the properties (First properties)
               String search = properties.getProperty(key_search);
return search;

            } catch (Exception e) {
                System.out.println("No properties file found...");
                e.printStackTrace();
            }
        }
    }

============================= Page_First==============================

public class Page_First extends BasePage {


    public Page_First() throws IOException {
        PageFactory.initElements(driver, this); }


    //////////////////////////////////////////////WEB ELEMENTS//////////////////////////////////////////////////////////

    @FindBy(name = "q")
    private WebElement searchText;

    @FindBy(name="btnK")
    private WebElement searchButton;


    //////////////////////////////////////////////BASE METHODS//////////////////////////////////////////////////////////

    public void startNavigation() {

      log.info("Accessing to Google");

    }

    public void search(String search) {

        searchText.sendKeys(MultiplePropertyReader.ReadProps());
    }

    public void enterButton (){

        clickElement(searchButton);
    }
}

Solution 2:[2]

Use custom-page-factory

<dependency>
  <groupId>com.github.hemanthsridhar</groupId>
  <artifactId>custom-page-factory</artifactId>
  <version>3.0.0</version>
</dependency>

Usage

@FilePath(value = PageObjectsConfig.ERROR_MSG_PAGE)
class HomePage {

  private WebDriver driver;
  public HomePage(WebDriver driver){
      this.driver = driver;
      PageFactory.initElements(new SearchWithFieldDecorator(new FileBasedElementLocatorFactory(driver, this)), this);
      }

@SearchBy
private WebElement userName;

@SearchBy
private WebElement password;

}

If we have a Property file for example

userName_xpath=//label[text()='username']
password_id=pwd

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Deepak Kumar
Solution 2 Hemanth