Огурец Java (мавен) - FileInputStrem - java .lang.NullPointerException - PullRequest
0 голосов
/ 23 февраля 2020

Привет, люди: я сталкиваюсь со следующей проблемой в Cucumber с Maven / Selenium / Java

Given I go to Google                                  # Step_First.I_go_to_Google()
      java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.java:130)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at pages.Page_First.getURL(Page_First.java:38)
    at stepdefs.Step_First.I_go_to_Google(Step_First.java:22)
    at ✽.I go to Google (src/test/java/features/first.feature:8)

    When I query for "<search>" cucumber spring selenium        # Step_First.I_query_for_cucumber_spring_selenium(String)
    And click search                                            # Step_First.click_search()
    Then google page title should become the first page         # Step_First.google_page_title_should_become_the_first_page()

java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.java:130)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at pages.Page_First.getURL(Page_First.java:38)
    at stepdefs.Step_First.I_go_to_Google(Step_First.java:22)
    at ✽.I go to Google (src/test/java/features/first.feature:8)

Это моя функция file:

Feature: Navigation Test


  Scenario: Search google.com 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

Это метод Page , который я использую:

public void getURL() throws IOException {

        Properties Config = new Properties();

        //Declares a variable for reading properties from a resource bundle file (*.properties)
        Properties p = new Properties();

        //Always the Absolute path here.
        FileInputStream file = new FileInputStream(System.getProperty("/Users/xxxxxx/Documents/automation_projects/" +
                "zero/src/main/resources/data/config.properties"));
        Config.load(file);

        driver.get(Config.getProperty("url"));

        //The String inside the config.properties
        String url = p.getProperty("url");
    }

В моем файле определения шага у меня есть файл:

@Given("I go to Google")
    public void I_go_to_Google () throws IOException {

        page_first.getURL();
    }

При просмотре информации выше, проблема возникает в этой строке:

FileInputStream file = new FileInputStream(System.getProperty("/Users/xxxxxx/Documents/automation_projects/" +
                "zero/src/main/resources/data/config.properties"));

Кстати, мой config.properties файл:

browser = firefox
url = https://www.google.com

Может кто-нибудь помочь мне с этим? Заранее спасибо.

Ответы [ 2 ]

1 голос
/ 23 февраля 2020

Вы пробовали использовать только путь к файлу без вызова System.getProperty? Я сомневаюсь, что у вас есть свойство, ключом которого является имя файла

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

Когда вы используете

        FileInputStream file = new FileInputStream(System.getProperty("/Users/xxxxxx/Documents/automation_projects/" +
            "zero/src/main/resources/data/config.properties"));

, вы пытаетесь найти системное свойство с именем /Users/xxxxx/..../config.properties, которое, скорее всего, не существует, и поэтому возвращает null, что затем некритически используется в качестве аргумента для new FileInputStream(null), который сразу же перестает работать.

Ваше понимание того, как работает System.getProperty, неверно. Подумайте о том, чтобы прочитать файл Properties из этого места, а затем поработайте с ним.

...