Selenium测试保存截图

时间:2016-05-31 09:54:47

标签: java selenium screenshot

我目前有一个Selenium测试,它通过11种不同语言的网站运行,并截取每个部分的屏幕截图。图片本身会保存在属性文件中的相应文件夹中

  

screenshotsEnglish.dir = /截图/语言/英     screenshotsSwedish.dir = /截图/语言/瑞典

我想要做的不同是每次运行测试套件时,都会创建一个新的基本文件夹,其中包含当前日期戳,后跟与之前相同的结构。我怎样才能做到这一点?我无法在属性文件中对其进行硬编码,因为日期会不断变化。

总体上更好地了解它的运作方式

public static Object[][] dp() {
return new Object[][]{
    {
        "https://example-URL.net/example.php?lang=EN",
        "screenshotsEnglish.dir"
    },
    {
        "https://example-URL.net/example.php?lang=SV",
        "screenshotsSwedish.dir"
    }
};
}

@Test(dataProvider = "dp")
public void t(String url, String directory) {
    driver.get(url);
    Properties settings = PropertiesLoader.fromResource("settings.properties");  
    String screenshotDir = settings.getProperty(directory);
    screenShooter = new ScreenShooter(driver, screenshotDir, "en");
    /*...*/
}

测试用Java编写,带有TestNG

我希望我已经清楚并且一如既往地感谢所有帮助

1 个答案:

答案 0 :(得分:1)

为什么不喜欢这样(没有尝试编译,只是快速尝试,所以可以使用拼写错误):

String screenshotDir = settings.getProperty(directory); //this is what you already have
String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date()); //added: get current date
screenshotDir = screenshotDir + "/" + date; //added: create modified path
new File(screenshotDir).mkdirs(); //added: make the new directory
screenShooter = new ScreenShooter(driver, screenshotDir, "en"); //this is what you already have