java - 如何在 selenium java 中找到当前项目的根目录
问题描述
我尝试使用以下代码查找当前项目的根目录,但它并没有准确地给我项目目录路径,而是给了我用户目录路径,谷歌搜索了这个并没有找到合适的解决方案。
如果你能在这方面帮助我,那将非常有帮助。
以下是我尝试过的代码:
下面代码的目的是将图像保存到当前项目路径中,当我使用下面的代码时,它不一定保存在项目路径中。
有什么建议么?
FileUtils.copyFile(scrFile, new File(
System.getProperty("user.dir") + "/test-output/ScreenshotsTC/" + "screenshots" + currentDate + ".png"));
System.out.println("File name is: " + testname + currentDate + ".png");
解决方案
尝试这个:
public String getPathFromWhereApplicationIsRunning() throws Exception {
try {
return new File(".").getCanonicalPath();
} catch (IOException ex) {
throw new Exception("Failed to get the absolute path of the process.",ex);
}
}
我多次将此方法用于独立目的(jar、maven、ant、eclipse、netbeans 等)
如果您将在服务器(war、tomcat、widfly、spring-boot 等)上运行 selenium 代码,这将不起作用。在这种情况下,它将返回:
- tomcat:bin文件夹
- spring-boot:启动的路径
java -jar ...
建议
为屏幕截图或任何其他文件位置使用环境变量。这将适用于任何阶段:
- 开发人员笔记本电脑/个人电脑
- 测试/登台/生产本地或云
System.getenv("SCREENSHOT_FOLDER") + "/test-output/ScreenshotsTC/" + "screenshots" + currentDate + ".png"));
最后只需在执行前导出变量:
#linux/mac
export SCREENSHOT_FOLDER=/foo/bar/
#windows
SET SCREENSHOT_FOLDER=/foo/bar/
推荐阅读
- spring-data-jpa - 动态映射类实体 x 数据库表(Spring-Data/SpringBoot)
- javascript - 如何在 JavaScript 的 urlopen 模块中传递动态标头?
- elasticsearch - elasticsearch idnex 的最佳实践
- laravel - 关闭工厂流明 8.2.3
- python - 用于附加列表或数据框的 Pandas 数据框
- python - 链接列表python中的相似元素以创建字典
- amazon-web-services - AWS IAM:允许用户/角色创建任何资源的策略,但只能使用特定标签
- x86 - MINIX 没有利用 x86 任务切换,对吧?
- python - 如何使 2 with 语句一起工作?
- php - 创建多个 FORM + 提交按钮以将数据插入 DB