【问题标题】:Add Timestamp Variable to Folder Path Value in Application Properties将时间戳变量添加到应用程序属性中的文件夹路径值
【发布时间】:2017-05-26 15:35:23
【问题描述】:
所以我需要在 app.properties 文件中设置一个文件夹路径名值。我还想在当前时间戳之后命名它,这样当它用于创建文件时,它也会创建文件夹。我目前拥有的东西不起作用。
screenshot.events = STARTED,SUCCEEDED,FAILED,STEP
screenshot.path = C:/Automation/${timestamp}
webdriver.type=CHROME
【问题讨论】:
标签:
java
spring
spring-boot
properties
timestamp
【解决方案1】:
这里有 3 个选项:
1。启动时
您可以在启动 Spring Boot 应用程序时定义所需的 SystemProperty:
public static void main(String[] args) {
System.setProperty("timestamp",String.valueOf(System.currentTimeMillis()));
new SpringApplicationBuilder() //
.sources(Launcher.class)//
.run(args);
}
然后,按照您的方式在 application.properties 中定义您的属性:
screenshot.path = C:/Automation/${timestamp}
2。注射时
@Value("${screenshot.path}")
public void setScreenshotPath(String screenshotPath) {
this.screenshotPath =
screenshotPath.replace("${timestamp}", System.currentTimeMillis());
}
3。使用时 - 执行时的动态时间戳
@Value("${screenshot.path}")
private String screenshotPath;
...
new File(screenshotPath.replace("${timestamp}", System.currentTimeMillis());
//or the following without the need for ${timestamp} in screenshot.path
//new File(screenshotPath + System.currentTimeMillis());
【解决方案2】:
我会简化它。只需在 application.properties 中定义根路径即可:
screenshot.root.path = C:/Automation/
并在保存 Selenium 屏幕截图时以编程方式附加时间戳路径部分。