在测试过程中,如果断言语句执行失败,对浏览器显示的内容进行截屏操作。
第一步:先创建工具类DateUtil
package cn.om.testFailCaptureScreen; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { /* * 格式化输出日期 * @return 返回字符型日期 */ public static String format(Date date,String format){ String result=""; try{ if(date!=null){ DateFormat df=new SimpleDateFormat(format); result=df.format(date); } }catch(Exception e){ e.printStackTrace(); } return result; } /* * 返回年份 * @return 返回年份 */ public static int getYear(Date date){ Calendar c=Calendar.getInstance(); c.setTime(date); return c.get(Calendar.YEAR); } /* * 返回月份 * @return 返回月份 */ public static int getMonth(Date date){ Calendar calendar=Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH)+1; } /* * 返回月份中的第几天 * @return 返回月份中的第几天 */ public static int getDay(Date date){ Calendar calendar=Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.DAY_OF_MONTH); } /* * 返回小时 * @param date * 日期 * @return 返回小时 */ public static int getHour(Date date){ Calendar calendar=Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.HOUR_OF_DAY); } /* * 返回分钟 * @param date * 日期 * @return 返回分钟 */ public static int getMinute(Date date){ Calendar calendar=Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MINUTE); } /* * 返回秒 * @param date * 日期 * @return 返回秒 */ public static int getSecond(Date date){ Calendar calendar=Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.SECOND); } }
第二步:创建读取、创建文件的FileUtil类
package cn.om.testFailCaptureScreen; import java.io.File; import java.io.IOException; public class FileUtil { public static boolean createFile(String destFileName) throws IOException{ File file=new File(destFileName); if(file.exists()){ System.out.println("创建单个文件"+destFileName+"失败,目标文件已存在!"); return false; } if(destFileName.endsWith(File.separator)){ System.out.println("创建单个文件"+destFileName+"失败,目标文件已存在!"); return false; } //判断目标文件所在的目录是否存在 if(!file.getParentFile().exists()){ System.out.println("目录文件所在目录不存在,准备创建它!"); if(!file.getParentFile().mkdirs()){ System.out.println("创建目标文件所在目录失败!"); return false; } } if(file.createNewFile()){ System.out.println("单个文件"+destFileName+"创建成功!"); return true; }else{ System.out.println("单个文件"+destFileName+"创建失败!"); return false; } } public static boolean createDir(String destDirName) { File dir = new File(destDirName); if (dir.exists()) { System.out.println("创建目录" + destDirName + "失败,目标目录已存在!"); return false; } if (dir.mkdirs()) { System.out.println("创建目录" + destDirName + "成功!"); return true; } else { System.out.println("创建目录" + destDirName + "失败!"); return false; } } }
第三步:编写测试类实现功能
package cn.om.testFailCaptureScreen; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import java.io.IOException; import java.util.Date; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; public class TestFailCaptureScreen { WebDriver driver; String url; @Test public void testSearch() { driver.get(url); driver.findElement(By.id("query")).sendKeys("光荣之路自动化测试"); driver.findElement(By.id("stb")).click(); try { Assert.assertTrue(driver.getPageSource().contains("事在人为")); } catch (AssertionError e) { System.out.println("catch中代码被执行了"); TakesScreenshot(driver); } } public void TakesScreenshot(WebDriver driver) { Date date = new Date(); // 调用DateUtil类中的方法,生成截图所在的文件夹日期名称 String picDir = "F:\\workspace\\WebDriver API\\" + String.valueOf(DateUtil.getYear(date) + "-" + String.valueOf(DateUtil.getMonth(date) + "-" + String.valueOf(DateUtil.getDay(date)))); if (!new File(picDir).exists()) { FileUtil.createDir(picDir); } // 调用DateUtil类中的方法,生成截图文件的时间名称 String pic = picDir+"\\" + String.valueOf(DateUtil.getHour(date)) + "-" + String.valueOf(DateUtil.getMinute(date)) + "-" + String.valueOf(DateUtil.getSecond(date)) + ".jpg"; System.out.println(pic); File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); // 将截图文件内容写入到磁盘中,生成截图文件。 try { FileUtils.copyFile(srcFile,new File(pic)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @BeforeMethod public void beforeMethod() { System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); url = "http://www.sogou.com"; } @AfterMethod public void afterMethod() { driver.quit(); } }