【问题标题】:Unable to find property file Java and TestNG找不到属性文件 Java 和 TestNG
【发布时间】:2018-01-04 05:00:49
【问题描述】:

已经尝试过这个解决方案,但还没有运气:

Cannot load properties file from resources directory

我的代码:

protected WebDriver driver;
public static Properties prop;    

@BeforeSuite
    public void setup()
    {

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-notifications");

        System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
        driver = new ChromeDriver(options);

        //Calling property file function here
        ReadData();

        driver.get(prop.getProperty("URL"));

    }

    public void ReadData()
        {  prop = new Properties();
            try {
                prop.load(new FileInputStream("D:\\projectname\\src\\main\\resources\\DataFile.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }}

获取异常:

java.io.FileNotFoundException

文件已经存在,我已经尝试了很多差异。文件路径如完整路径,路径从src 等开始。

项目结构:

http://prntscr.com/g1bpq2

更新:

  1. 当我从文件路径中删除 .properties 扩展名时它可以工作。
  2. 我仍然不确定为什么 intellij 没有将文件扩展名添加为 .properties。因为我确信我已经创建了 properties 类型的文件。

【问题讨论】:

  • 你在用Maven吗?
  • 跟随@Zeeshan Siddiqui - 文件扩展名是否有可能隐藏在您正在查看的目录中。根据设置,文件扩展名可能不显示。 Coujkd 值得检查子目录设置以查看扩展名和隐藏文件。
  • ^ 明智的问题
  • @BusinessPlanQuickBuilder - 现在可以使用了。谢谢。
  • 很高兴听到@DebanjanB 的好建议

标签: java maven selenium-webdriver testng


【解决方案1】:

你需要在java中使用\\来给出绝对路径,如下所示:-

D:\\Demo_Login\\src\\main\\resources\\DataFile.properties

你可以用File.separator代替\\

您可以执行以下操作:-

        File src=new File("src\\lib\\phantomjs\\phantomjs.exe");

        System.setProperty("phantomjs.binary.path",src.getAbsolutePath());

您可以使用. 尝试以下类似的操作,该目录从项目根目录开始:-

System.setProperty("webdriver.chrome.driver","./src"+File.separator+"lib"+File.separator+"chromedriver.exe")

像这样以src 开头:-

"src/main/resources/DataFile.properties"

希望对你有帮助:)

【讨论】:

【解决方案2】:

这是您问题的答案:

而不是:

prop.load(new FileInputStream("D:\\projectname\\src\\main\\resources\\DataFile.properties"));

你可以试试:

prop.load(new FileInputStream("./src/main/resources/DataFile.properties"));

如果这能回答您的问题,请告诉我。

【讨论】:

  • @HelpingHands 这个问题得到回答了吗?谢谢
  • 没有朋友。出现错误:java.io.FileNotFoundException: .\src\main\resources\DataFile.properties(系统找不到指定的文件)
  • 所以很明显你在 "D:\\projectname\\src\\main\\resources" 没有名为 DataFile.properties 的文件\\" 子目录。在那里放置一个适当的文件并检查答案。谢谢
  • 你是对的。我试图访问没有 .properties 的文件,它可以工作。但是我仍然不确定为什么即使我添加了文件类型属性,intelliJ 也不添加扩展名 .properties。
  • 很高兴能为您提供帮助 :) 您可能需要配置一些 intelliJ 设置。谢谢
猜你喜欢
  • 2014-07-29
  • 2019-04-17
  • 2011-11-03
  • 2019-05-10
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 2023-02-01
相关资源
最近更新 更多