0

Hi people: I'm facing the next issue in Cucumber with 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)

This is my feature 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

This is the Page method that I use:

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

In my step definition file I have this:

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

        page_first.getURL();
    }

Reviewing the information above, the issue occurs in this line:

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

By the way, my config.properties file:

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

May anybody help me with this? Thanks in advance.

Alessio
  • 3,404
  • 19
  • 35
  • 48
nosequeweaponer
  • 511
  • 10
  • 38

2 Answers2

1

Have you tried only using the file path without the call to System.getProperty ? I doubt you have a property with a key being a file name

DarkRift
  • 224
  • 2
  • 11
0

When you use

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

you try to look up a system property actually named /Users/xxxxx/..../config.properties which most likely doesn't exist, which therefore returns null which is then uncritically used as an argument to new FileInputStream(null) which then immediately fails.

Your understanding of how System.getProperty works is incorrect. Consider reading in a Properties file from that location and then work with that.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347