2

我正在使用以下方法在我的项目中截屏:-

public String getScreenShot(String imageName) {
String snapShot = null;
try {
    if (imageName.equals("")) {
        imageName = "blank";
    }
    Calendar cal = Calendar.getInstance();
    Date currentTimeStamp = cal.getTime();
    String timeInFormat = formater.format(currentTimeStamp);
    File src = driv.getScreenshotAs(OutputType.FILE);
    String screenShotLocation = System.getProperty("user.dir") + "/screenPrints/";
    File des = new File(screenShotLocation + imageName + "_" + timeInFormat + ".png");
    FileUtils.copyFile(src, des);
    snapShot = des.toString();
    i++;        //i counts the no of times control coming into getScreenShot method 
    System.out.println(i);


} catch (Exception e) {
    System.out.println("The ScreenShot could not be taken\n" + e);
        e.printStackTrace();
    }
    return snapShot;
}

我用下面的代码行调用上述方法: -

screen = getScreenShot("");

它进入 getScreenShot() 方法 158 次,并在控制台中将 i 值显示为 158。但它能够将 134 个屏幕打印保存在目标文件夹中。如果我们在调用此方法之前包含 1 秒的间隔,它可以在目标文件夹中保存确切的截图数量。

Thread.sleep(1000);
screen = getScreenShot("");

似乎执行速度如此之快,以至于它跳过了目标文件夹中的保存部分。Thread.sleep() 大大降低了性能。有人可以为此提出更好的解决方案。提前致谢。

4

3 回答 3

2

可能有几个文件具有相同的名称。我认为,您timeInFormat的格式最多可以达到几秒钟,并且您将在一秒钟内拍摄多个屏幕截图。要解决此问题,您可以格式化timeInFormat为毫秒或微秒。不过,还有其他 hack。换行就好

String timeInFormat = formater.format(currentTimeStamp);

String timeInFormat = formater.format(currentTimeStamp) + String.valueOf(i);

我希望这会有所帮助。

于 2018-04-20T07:49:21.430 回答
1

您需要考虑以下几点:

  • 如果您使用包含文本空白的名称保存屏幕截图,您可以将方法的签名更改为:

    public String getScreenShot() //removing String imageName
    

    main()您可以从/调用此方法,@Test如下所示:

    screen = getScreenShot();
    
  • 当您调用getScreenshotAs()of-coarse 时,您必须按如下方式转换WebDriver实例驱动器:

    TakesScreenshot ts = (TakesScreenshot) driv;
    File src = ts.getScreenshotAs(OutputType.FILE);
    
  • 您需要决定要返回什么 a Stringas des.toString(); 或者int我一样

  • 具有较小 tweeks的方法的代码块getScreenShot()如下:

    public int getScreenShot() {
    
        String snapShot = null; //possibly not needed, we are returning int i
        int i = 0;
        String imageName = "blank";
        try {
            Calendar cal = Calendar.getInstance();
            Date currentTimeStamp = cal.getTime();
            String timeInFormat = formater.format(currentTimeStamp);
            TakesScreenshot ts = (TakesScreenshot) driv;
            File src = ts.getScreenshotAs(OutputType.FILE);
            String screenShotLocation = System.getProperty("user.dir") + "/screenPrints/";
            File des = new File(screenShotLocation + imageName + "_" + timeInFormat + ".png");
            FileUtils.copyFile(src, des);
            snapShot = des.toString(); //not needed as we are not using, returning int i
            i++; //i counts the no of times control coming into getScreenShot method 
            System.out.println(i);
        } catch (Exception e) {
            System.out.println("The ScreenShot could not be taken\n" + e);
            e.printStackTrace();
        }
        return i;
    }
    
  • 现在你可以调用getScreenShot()from main()/@Test如下:

    int screen = getScreenShot();
    

参考

您可以在如何使用 Selenium WebDriver 截屏中找到详细讨论

于 2018-04-20T08:15:06.193 回答
0

试试这段代码,希望能解决问题

public String captureScreen(String fileName) {
if(fileName =="") {
    fileName="Screenshot";  }

File destFile=null;
Calendar calendar =Calendar.getInstance() ;
SimpleDateFormat formater= new SimpleDateFormat("dd_MM_yyy_hh_mm_ss");
File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

try {

     String reportDirectory= new File(System.getProperty("user.dir")).getAbsolutePath()+"./src/main/java/com/test/automation/Demo/screenshot/";
     destFile= new File((String)reportDirectory + fileName +"-" + formater.format(calendar.getTime())+ ".png");
     FileUtils.copyFile(srcFile,destFile );

    }
catch(IOException e) 
  {
    e.printStackTrace();
  }
   return destFile.toString();

}

于 2018-05-25T05:04:56.520 回答