zoukankan      html  css  js  c++  java
  • 章节十七、3- 高级报告中加入截图

    一、案例演示----设置case登录qq邮箱时不成功并把错误的截图加入到html测试报告中

    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 String  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         String destination = directory+filename;
    27 //        返回截图的路径
    28         return destination;
    29     }
    30 }
     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、testcase

     1 package extenreports;
     2 
     3 import java.io.IOException;
     4 import java.util.concurrent.TimeUnit;
     5 import org.openqa.selenium.By;
     6 import org.openqa.selenium.NoSuchElementException;
     7 import org.openqa.selenium.WebDriver;
     8 import org.openqa.selenium.WebElement;
     9 import org.openqa.selenium.chrome.ChromeDriver;
    10 import org.testng.Assert;
    11 import org.testng.ITestResult;
    12 import org.testng.annotations.AfterMethod;
    13 import org.testng.annotations.BeforeMethod;
    14 import org.testng.annotations.Test;
    15 
    16 import com.aventstack.extentreports.ExtentReports;
    17 import com.aventstack.extentreports.ExtentTest;
    18 import com.aventstack.extentreports.MediaEntityBuilder;
    19 import com.aventstack.extentreports.MediaEntityModelProvider;
    20 import com.aventstack.extentreports.Status;
    21 
    22 import utilities.Screenshots;
    23 
    24 
    25 public class SeleniumLoginTest {
    26     
    27     private WebDriver driver;
    28     private String baseUrl;
    29 //    使用ExtentReports高级报告,我们需要先声明以下两个变量(需要导入相对应的包,否则会报错)
    30     ExtentReports reports;
    31     ExtentTest test;
    32 
    33     @BeforeMethod
    34     public void beforeMethod() {
    35         baseUrl = "https://mail.qq.com";
    36 //初始化变量
    37         reports = ExtentFactory.getInstance();
    38 //        括号中填写的是测试的名字
    39         test = reports.createTest("Verify if login success");
    40         driver = new ChromeDriver();
    41 //        记录登录的日志信息
    42         test.log(Status.INFO,"Browser Maxanized ............");
    43         driver.manage().window().maximize();
    44         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    45         test.log(Status.INFO,"Browser started ............");
    46         driver.get(baseUrl);
    47         test.log(Status.INFO,"Web  Application Open ............");
    48 
    49     }
    50     
    51     @Test
    52     public void test1_validLoginTest() throws Exception {
    53         driver.switchTo().frame("login_frame");
    54         
    55         WebElement dl = driver.findElement(By.className("switch_btn"));
    56         dl.click();
    57         
    58         WebElement emailField = driver.findElement(By.id("u"));
    59         emailField.sendKeys("12345678");
    60         test.log(Status.INFO,"Enter User Name ............");
    61         
    62         WebElement passwordField = driver.findElement(By.id("p"));
    63         passwordField.sendKeys("12345678");
    64         test.log(Status.INFO,"Enter Password ............");
    65 
    66         
    67         WebElement goButton = driver.findElement(By.id("login_button"));
    68         goButton.click();
    69         test.log(Status.INFO,"Clicked on login button ............");
    70 
    71         WebElement welcomeText = null;
    72         
    73         try {
    74             welcomeText = driver.findElement(By.xpath("//b[text()='时光以北暮南城']"));
    75         }
    76         catch (NoSuchElementException e) {
    77             System.out.println(e.getMessage());
    78         }
    79         Assert.assertTrue(welcomeText != null);
    80         test.log(Status.PASS,"Verified Login Success............");
    81 
    82     }
    83     
    84     @AfterMethod
    85     public void afterMethod(ITestResult testResult) throws  IOException {
    86         if(testResult.getStatus() == ITestResult.FAILURE) {
    87 //            接收截图的路径
    88             String path = Screenshots.takeScreenshots(driver, testResult.getName());
    89 //            存放截图路径存放到html中
    90             MediaEntityModelProvider imgPath = MediaEntityBuilder.createScreenCaptureFromPath(path).build();
    91             test.log(Status.FAIL,"Verified Login Failed............",imgPath);
    92         }
    93         driver.quit();
    94 //        刷新
    95         reports.flush();
    96     }
    97 }

    3、运行结果(点击图片可放大)

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

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

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

  • 相关阅读:
    重排列
    最多分成多少块
    后面第一个大于
    走格子
    硬币游戏
    还是01串
    戴德兰
    个人所得税
    最长高地
    执行Commit时Oracle做哪些工作
  • 原文地址:https://www.cnblogs.com/luohuasheng/p/11720060.html
Copyright © 2011-2022 走看看