0

大家好:我正在使用 Maven/Selenium/Java 在 Cucumber 中遇到下一个问题

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)

这是我的功能文件:

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

有人可以帮我吗?提前致谢。

4

2 回答 2

1

您是否尝试过仅使用文件路径而不调用 System.getProperty ?我怀疑你有一个键是文件名的属性

于 2020-02-23T12:33:00.417 回答
0

当你使用

        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文件,然后使用它。

于 2020-02-23T21:08:11.263 回答