章节十七章、2- 给执行失败的case截图

  • 2019 年 10 月 18 日
  • 筆記

一、案例演示

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 }

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 }

运行结果:在指定目录成功生成了错误截图文件

 

 

 

 

 

 

如果有不明白的小伙伴可以加群“555191854”问我,群里都是软件行业的小伙伴相互一起学习。

内容具有连惯性,未标注的地方可以看前面的博客,这是一整套关于ava+selenium自动化的内容,从java基础开始。

欢迎关注,转载请注明来源。