Java driver.getScreenshotAs(OutputType.FILE)无法将所有屏幕截图保存到目标中
我使用以下方法在我的项目中截图:-Java driver.getScreenshotAs(OutputType.FILE)无法将所有屏幕截图保存到目标中,java,selenium,screenshot,extentreports,Java,Selenium,Screenshot,Extentreports,我使用以下方法在我的项目中截图:- public String getScreenShot(String imageName) { String snapShot = null; try { if (imageName.equals("")) { imageName = "blank"; } Calendar cal = Calendar.getInstance(); Date currentTimeStamp = cal.getTime();
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秒的间隔,则可以保存确切的no
目标文件夹中的屏幕截图
Thread.sleep(1000);
screen = getScreenShot("");
看起来执行速度如此之快,以至于跳过了目标文件夹中的保存部分。Thread.sleep()大大降低了性能。有人能提出更好的解决方案吗。提前感谢。可能有几个文件同名。我认为,您的
timeInFormat
格式最多为秒&您在一秒内拍摄了多个屏幕截图。要解决此问题,您可以将timeInFormat
的格式设置为毫秒或微秒。不过,还有其他黑客。换线就行了
String timeInFormat = formater.format(currentTimeStamp);
到
我希望有帮助。 有两件事你需要考虑如下:
screen = getScreenShot();
TakesScreenshot ts = (TakesScreenshot) driv;
File src = ts.getScreenshotAs(OutputType.FILE);
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;
}
int screen = getScreenShot();
- 如果使用包含文本blank的名称保存屏幕截图,可以将方法的签名更改为:
您可以从public String getScreenShot() //removing String imageName
/main()
调用此方法,如下所示:@Test
screen = getScreenShot();
TakesScreenshot ts = (TakesScreenshot) driv; File src = ts.getScreenshotAs(OutputType.FILE);
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; }
int screen = getScreenShot();
- 调用的
时,必须按如下方式强制转换WebDriver实例driv:getScreenshotAs()
screen = getScreenShot();
TakesScreenshot ts = (TakesScreenshot) driv; File src = ts.getScreenshotAs(OutputType.FILE);
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; }
int screen = getScreenShot();
- 您需要决定要将
作为des.toString()返回什么或字符串
asiint
- 带有小tweek的
方法的代码块如下所示:getScreenShot()
screen = getScreenShot();
TakesScreenshot ts = (TakesScreenshot) driv; File src = ts.getScreenshotAs(OutputType.FILE);
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; }
int screen = getScreenShot();
- 现在,您可以从
/main()
调用@Test
,如下所示:getScreenShot()
screen = getScreenShot();
TakesScreenshot ts = (TakesScreenshot) driv; File src = ts.getScreenshotAs(OutputType.FILE);
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; }
int screen = getScreenShot();
参考文献
您可以在中找到详细的讨论,请尝试此代码我希望您能解决此问题
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();
}是的,我使用了SimpleDateFormat Format=new SimpleDateFormat(“dd_-MM\u-yyy\u-hh\u-MM\u-ss”),它不能在同一秒内拍摄多个屏幕打印。现在我也在使用毫秒,即SimpleDateFormat Formatter=new SimpleDateFormat(“dd_-MM_-yyy_-hh_-MM_-ss.SSS”),它正在获取所有屏幕打印。非常感谢:)非常感谢你的努力和时间。。但现在我的函数正在工作,因为我还包括了毫秒SimpleDateFormat Format=new SimpleDateFormat(“dd_-MM_-yyy_-hh_-MM_-ss.SSS”),正如您所建议的,我删除了参数,并将图像名称改为“image_20_-04_-2018_-02_-27_-36.523.png”谢谢:)@RachitJain好消息!!!如果我的答案符合您的问题,请单击我的答案旁边的空心复选标记(位于VoteDown箭头下方)来输入答案,使复选标记变为绿色。