Selenium - Java - Фабрика страниц: чтение из файла свойств и передача значения в Selenium (SendKeys) - PullRequest
0 голосов
/ 29 февраля 2020

Я застрял в этой ситуации:

Я пытаюсь получить некоторые значения из файла свойств (first.properties) и передать Selenium (метод sendKeys). Для этого у меня есть следующее.

Я создал интерфейс для хранения строк файла свойств.

public interface Constants {

    String key_search = "search";
}

Мой файл свойств:

search = "Selenium Cucumber"

Это класс, который читает все свойства в программе (у меня более 1 файла свойств)

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

В моем классе Page Factory у меня есть следующий код, который передаст параметр в указанное поле c через 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);
    }
}

Это шаг класса определения шага, в котором он передает параметр

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

        page_first.search(search);
    }

Когда я запускаю программу с IntelliJ, отображается следующий выпуск :

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)

Если кто-нибудь может мне помочь ... С уважением.

ОБНОВЛЕНО: Обновлен файл чтения свойств, и здесь я публикую свой файл функций.

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

1 Ответ

0 голосов
/ 29 февраля 2020

кажется, что вы не передаете никаких значений в

Page_First.search

И вы упомянули чтение из файла свойств и передачу значения для отправки ключей, но MultiplePropertyReader.ReadProps() тоже не используется.

Вероятно, это должно выглядеть так:

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