首页 > 解决方案 > 属性文件值未出现在我的变量中

问题描述

目前正在学习自动化测试,我无法理解我的代码有什么问题。我正在尝试从 .properties 文件中获取信息并将其用作变量。我收到一个错误:

java.io.FileNotFoundException: ..\resources\config.properties (The system cannot find the path specified)

但是,我确信我的道路是正确的。尝试了不同的变体,例如//resources//config...甚至使用\\,仍然是相同的问题。

这是我尝试从 config.properties 文件中获取信息的代码:

@Test
public void myFirstTest() {
        LoginPage log = new LoginPage();

        try(FileReader reader = new FileReader("../resources/config.properties")) {
            Properties properties = new Properties();
            properties.load(reader);
            String username = (String) properties.get("username");
            String password = (String) properties.get("password");
            System.out.println("h" + username);
            log.insertUsername(username);
            log.insertPassword(password);
        } catch(Exception e) {
            e.printStackTrace();
        }
}

这就是我的样子config.properties

username = myUserName1
password = myTestPass1

这是我的文件的架构:

在此处输入图像描述

PS我正在尝试从测试中获取源文件->LabelsAndFoldersTest.java

标签: javaseleniumautomated-teststestng

解决方案


您提供的路径config.properties不正确

正如我可以看到你的项目结构的图像,你的项目src/resources下有一个文件夹,你有地方config.properties

将文件路径更改为src/resources/config.properties

try(FileReader reader = new FileReader("src/resources/config.properties"))

推荐阅读