</p><p> /**</p><p> * @author Young</p><p> */</p><p> public void takeScreenShot() {</p><p> SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");</p><p> Calendar cal = Calendar.getInstance();</p><p> Date date = cal.getTime();</p><p> String dateStr = sf.format(date);</p><p> String path = this.getClass().getSimpleName() + "_" + dateStr + ".png";</p><p> takeScreenShot((TakesScreenshot) this.getDriver(), path);</p><p> }</p><p>
</p><p> /**</p><p> * @author Young</p><p> * @param drivername</p><p> * @param path</p><p> */</p><p> public void takeScreenShot(TakesScreenshot drivername, String path) {</p><p> // this method will take screen shot ,require two parameters ,one is</p><p> // driver name, another is file name</p><p> String currentPath = System.getProperty("user.dir"); // get current work</p><p> log.info(currentPath);</p><p> File scrFile = drivername.getScreenshotAs(OutputType.FILE);</p><p> // Now you can do whatever you need to do with it, for example copy</p><p> try {</p><p> log.info("save snapshot path is:" + currentPath + path);</p><p> FileUtils.copyFile(scrFile, new File(currentPath + "\\" + path));</p><p> } catch (Exception e) {</p><p> log.error("Can't save screenshot");</p><p> e.printStackTrace();</p><p> } finally {</p><p> log.info("screen shot finished");</p><p> }</p><p> }</p><p>