一、案例演示
1、首先我们把截图的方法单独进行封装方便以后调用。
1 package utilities; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import org.apache.commons.io.FileUtils; 7 import org.openqa.selenium.OutputType; 8 import org.openqa.selenium.TakesScreenshot; 9 import org.openqa.selenium.WebDriver; 10 11 public class Screenshots { 12 13 // 为了方便调用,我们写成静态的方法 14 // 首先,我们设置在调用该方法时需要传入两个参数:浏览器类型、文件名 15 public static void takeScreenshots(WebDriver driver,String filename) throws IOException { 16 // 截图文件名 17 filename = filename + ".png"; 18 // 截图存放路径 19 String directory = "C:\\Users\\acer\\Desktop\\dd\\Screenshots\\"; 20 // 截图 21 // 强制转换driver为TackScreenShot类型,然后调用getScreenshotAs("截完图后的输出类型")方法进行截图 22 File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 23 // 将文件复制到指定目录 24 FileUtils.copyFile(sourceFile, new File(directory+filename)); 25 } 26 }
1 package extenreports; 2 3 import java.io.File; 4 import java.util.Date; 5 6 import com.aventstack.extentreports.ExtentReports; 7 import com.aventstack.extentreports.ResourceCDN; 8 import com.aventstack.extentreports.reporter.ExtentHtmlReporter; 9 import com.aventstack.extentreports.reporter.configuration.ChartLocation; 10 import com.aventstack.extentreports.reporter.configuration.Theme; 11 12 public class ExtentFactory { 13 public static ExtentReports getInstance() { 14 15 Date date = new Date(); 16 String form = String.format("%tF", date); 17 String hour = String.format("%tH", date); 18 String minute = String.format("%tM", date); 19 String second = String.format("%tS", date); 20 // 生成的路径以及文件名 21 final String OUTPUT_FOLDER = "C:\\Users\\acer\\Desktop\\dd\\ExtentReports\\"; 22 final String FILE_NAME = "index" + form + hour + minute + second + ".html"; 23 24 // 文件夹不存在的话进行创建 25 File reportDir = new File(OUTPUT_FOLDER); 26 if (!reportDir.exists() && !reportDir.isDirectory()) { 27 reportDir.mkdir(); 28 } 29 30 ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME); 31 // 设置静态文件的DNS 32 htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS); 33 // 怎么样解决cdn.rawgit.com访问不了的情况 34 htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS); 35 htmlReporter.config().setDocumentTitle("标品页面功能自动化测试报告"); 36 htmlReporter.config().setReportName("标品页面功能自动化测试报告"); 37 htmlReporter.config().setChartVisibilityOnOpen(true); 38 htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP); 39 htmlReporter.config().setTheme(Theme.STANDARD); 40 htmlReporter.config().setCSS(".node.level-1 ul{ display:none;} .node.level-1.active ul{display:block;}"); 41 htmlReporter.config().setEncoding("gbk"); 42 ExtentReports extent = new ExtentReports(); 43 extent.attachReporter(htmlReporter); 44 extent.setReportUsesManualConfiguration(true); 45 extent.setSystemInfo("Selenium Version", "3.141.59"); 46 extent.setSystemInfo("Platform", "Windows"); 47 48 return extent; 49 } 50 }
2、以qq邮箱登录为例,我们故意输入错误的密码让case执行失败然后进行截图。
1 package extenreports; 2 3 import org.testng.annotations.Test; 4 5 import com.aventstack.extentreports.ExtentReports; 6 import com.aventstack.extentreports.ExtentTest; 7 import com.aventstack.extentreports.Status; 8 9 import utilities.Screenshots; 10 11 import org.testng.annotations.BeforeMethod; 12 import org.testng.annotations.AfterMethod; 13 import org.testng.annotations.BeforeClass; 14 15 import java.io.IOException; 16 import java.util.concurrent.TimeUnit; 17 18 import org.openqa.selenium.By; 19 import org.openqa.selenium.NoSuchElementException; 20 import org.openqa.selenium.WebDriver; 21 import org.openqa.selenium.WebElement; 22 import org.openqa.selenium.chrome.ChromeDriver; 23 import org.testng.Assert; 24 import org.testng.ITestResult; 25 26 public class TestNG_Screenshots { 27 28 private WebDriver driver; 29 private String baseUrl; 30 31 32 // 每个方法执行前执行一次 33 @BeforeMethod 34 public void tearUp() { 35 baseUrl = "https://mail.qq.com"; 36 driver = new ChromeDriver(); 37 driver.manage().window().maximize(); 38 driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); 39 driver.get(baseUrl); 40 } 41 42 @Test 43 public void test1_invalidCredentials() { 44 driver.switchTo().frame("login_frame"); 45 46 WebElement dl = driver.findElement(By.className("switch_btn")); 47 dl.click(); 48 49 WebElement emailField = driver.findElement(By.id("u")); 50 emailField.sendKeys("12345678"); 51 52 WebElement passwordField = driver.findElement(By.id("p")); 53 passwordField.sendKeys("***************"); 54 55 56 WebElement goButton = driver.findElement(By.id("login_button")); 57 goButton.click(); 58 59 WebElement welcomeText = null; 60 try { 61 welcomeText = driver.findElement(By.xpath("//b[text()='********']")); 62 } 63 catch (NoSuchElementException e) { 64 System.out.println(e.getMessage()); 65 } 66 Assert.assertTrue(welcomeText != null); 67 } 68 69 // 每个方法执行结束后执行一次 70 @AfterMethod 71 // 使用testng中的ITestResult接口来捕获testcase的运行结果 72 public void tearDown(ITestResult testResult) throws IOException { 73 // 如果获取到的testcase的状态等于failure(失败) 74 if(testResult.getStatus() == ITestResult.FAILURE) { 75 // 就调用自己封装好的截图方法,以testcase方法的名字给文件命名 76 Screenshots.takeScreenshots(driver, testResult.getName()); 77 } 78 driver.quit(); 79 } 80 }
运行结果:在指定目录成功生成了错误截图文件