整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          一款很酷炫的自動化測試報告框架 ExtentReport

          、引言

          在本文中,我將詳細介紹如何將 ExtentReports 測試報告與TestNG集成。

          二、ExtentReports 簡介

          主要特點:

          • 生成的報告簡潔美觀
          • 生成的單html方便 Jenkins 集成發郵件
          • 自帶集中展示歷史報告的服務端
          • 支持 Java 和 .Net

          TestNG 原生報告有點丑,信息整理有點亂。ExtentReports 是用于替換TestNG 原生報告。當然也可以使用 ReportNg,個人偏好 ExtentReports 樣式。

          官網已經給了很多demo了,大家可以參考練習,這里根據個人經驗進行了配置。

          • 官網:http://extentreports.com/
          • 客戶端:https://github.com/anshooarora/extentreports-java/commits/master
          • 服務端:https://github.com/anshooarora/extentx

          三、具體步驟

          Step-1:添加 Maven 依賴包

          引入pom.xml

          
                  <!--引入extentreports相關包-->
                  <dependency>
                      <groupId>com.aventstack</groupId>
                      <artifactId>extentreports</artifactId>
                      <version>3.1.5</version>
                      <scope>provided</scope>
                  </dependency>
                  <dependency>
                      <groupId>com.vimalselvam</groupId>
                      <artifactId>testng-extentsreport</artifactId>
                      <version>1.3.1</version>
                  </dependency>
                  <dependency>
                      <groupId>com.relevantcodes</groupId>
                      <artifactId>extentreports</artifactId>
                      <version>2.41.2</version>
                  </dependency>
                  <!--引入testng測試框架-->
                  <dependency>
                      <groupId>org.testng</groupId>
                      <artifactId>testng</artifactId>
                      <version>6.14.3</version>
                      <scope>compile</scope>
                  </dependency>

          Step-2:重寫 ExtentTestNgFormatter 類

          主要基于以下兩項原因:

          • 支持報告中展示更多狀態類型的測試結果,例如:成功、失敗、警告、跳過等。
          • 因為不支持cdn.rawgit.com訪問,故替css訪問方式。

          創建 MyExtentTestNgFormatter 類

          下載 ExtentReportes 源碼,找到 ExtentTestNgFormatter 類,Listener 目錄下創建 MyExtentTestNgFormatter.java 類直接繼承 ExtentTestNgFormatter 類。

          public class MyExtentTestNgFormatter extends ExtentTestNgFormatter {

          解決CDN無法訪問

          構造方法加入

          htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS); 

          具體代碼如下:

          ublic MyExtentTestNgFormatter() {
                setInstance(this);
                testRunnerOutput = new ArrayList<>();
                String reportPathStr = System.getProperty("reportPath");
                File reportPath;
          
                try {
                    reportPath = new File(reportPathStr);
                } catch (NullPointerException e) {
                    reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
                }
          
                if (!reportPath.exists()) {
                    if (!reportPath.mkdirs()) {
                        throw new RuntimeException("Failed to create output run directory");
                    }
                }
          
                File reportFile = new File(reportPath, "report.html");
                File emailReportFile = new File(reportPath, "emailable-report.html");
          
                htmlReporter = new ExtentHtmlReporter(reportFile);
                EmailReporter emailReporter = new EmailReporter(emailReportFile);
                reporter = new ExtentReports();
                //        如果cdn.rawgit.com訪問不了,可以設置為:ResourceCDN.EXTENTREPORTS或者ResourceCDN.GITHUB
                htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
                reporter.attachReporter(htmlReporter, emailReporter);
            }

          重寫 onstart 方法

          重寫onstart 方法功能。新建一個類名為MyReporter,一個靜態ExtentTest的引用。

          Listener 包下 MyReporter.java

          public class MyReporter {
                  public static ExtentTest report;
              }

          MyExtentTestNgFormatter.java

          public void onStart(ITestContext iTestContext) {
                  ISuite iSuite = iTestContext.getSuite();
                  ExtentTest suite = (ExtentTest) iSuite.getAttribute(SUITE_ATTR);
                  ExtentTest testContext = suite.createNode(iTestContext.getName());
                  // 將MyReporter.report靜態引用賦值為testContext。
                  // testContext是@Test每個測試用例時需要的。report.log可以跟隨具體的測試用例。另請查閱源碼。
                  MyReporter.report = testContext;
                  iTestContext.setAttribute("testContext", testContext);
              }

          自定義配置

          測試報告默認是在工程根目錄下創建 test-output/ 文件夾下,名為report.htmlemailable-report.html。可根據各自需求在構造方法中修改。

              public MyExtentTestNgFormatter() {
                  setInstance(this);
                  testRunnerOutput = new ArrayList<>();
                  // reportPath報告路徑
                  String reportPathStr = System.getProperty("reportPath");
                  File reportPath;
          
                  try {
                      reportPath = new File(reportPathStr);
                  } catch (NullPointerException e) {
                      reportPath = new File(TestNG.DEFAULT_OUTPUTDIR);
                  }
          
                  if (!reportPath.exists()) {
                      if (!reportPath.mkdirs()) {
                          throw new RuntimeException("Failed to create output run directory");
                      }
                  }
                  // 報告名report.html
                  File reportFile = new File(reportPath, "report.html");
                  // 郵件報告名emailable-report.html
                  File emailReportFile = new File(reportPath, "emailable-report.html");
          
                  htmlReporter = new ExtentHtmlReporter(reportFile);
                  EmailReporter emailReporter = new EmailReporter(emailReportFile);
                  reporter = new ExtentReports();
                  reporter.attachReporter(htmlReporter, emailReporter);
              }

          report.log

          report.log 支持多種玩法

          // 根據狀態不同添加報告。型如警告
          MyReporter.report.log(Status.WARNING, "接口耗時(ms):" + String.valueOf(time));

          直接從TestClass 中運行時會報 MyReporter.report 的空指針錯誤,需做判空處理。

          Step-3:配置監聽

          在測試集合 testng.xml 文件中導入 Listener 監聽類。

              <listeners>
                  <listener class-name="com.zuozewei.extentreportdemo.listener.MyExtentTestNgFormatter"/>
              </listeners> 

          Step-4:配置報告

          extent reporters支持報告的配置。目前支持的配置內容有title、主題等。 先在src/resources/目錄下添加 config/report/extent-config.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <extentreports>
              <configuration>
                  <timeStampFormat>yyyy-MM-dd HH:mm:ss</timeStampFormat>
                  <!-- report theme -->
                  <!-- standard, dark 個人喜好暗色 -->
                  <theme>dark</theme>
          
                  <!-- document encoding -->
                  <!-- defaults to UTF-8 -->
                  <encoding>UTF-8</encoding>
          
                  <!-- protocol for script and stylesheets -->
                  <!-- defaults to https -->
                  <protocol>https</protocol>
          
                  <!-- title of the document -->
                  <documentTitle>QA-接口自動化測試報告</documentTitle>
          
                  <!-- report name - displayed at top-nav -->
                  <reportName>QA-接口自動化測試報告</reportName>
          
                  <!-- report headline - displayed at top-nav, after reportHeadline -->
                  <reportHeadline>接口自動化測試報告</reportHeadline>
          
                  <!-- global date format override -->
                  <!-- defaults to yyyy-MM-dd -->
                  <dateFormat>yyyy-MM-dd</dateFormat>
          
                  <!-- global time format override -->
                  <!-- defaults to HH:mm:ss -->
                  <timeFormat>HH:mm:ss</timeFormat>
          
                  <!-- custom javascript -->
                  <scripts>
                      <![CDATA[
                  $(document).ready(function() {
          
                  });
                ]]>
                  </scripts>
          
                  <!-- custom styles -->
                  <styles>
                      <![CDATA[
          
                ]]>
                  </styles>
              </configuration>
          </extentreports>

          Step-5:配置系統系統

          config下新建 MySystemInfo類繼承 SystemInfo 接口

          public class MySystemInfo implements SystemInfo {
              @Override
              public Map<String, String> getSystemInfo() {
          
                  Map<String, String> systemInfo = new HashMap<>();
                  systemInfo.put("測試人員", "zuozewei");
          
                  return systemInfo;
              }
          }

          可用于添加系統信息,例如:db的配置信息,人員信息,環境信息等。根據項目實際情況添加。 至此,extentreports 美化報告完成。

          Step-6:添加測試用例

          public class TestMethodsDemo {
          
              @Test
              public void test1(){
                  Assert.assertEquals(1,2);
              }
          
              @Test
              public void test2(){
                  Assert.assertEquals(1,1);
              }
          
          
              @Test
              public void test3(){
                  Assert.assertEquals("aaa","aaa");
              }
          
          
              @Test
              public void logDemo(){
                  Reporter.log("這是故意寫入的日志");
                  throw new RuntimeException("故意運行時異常");
              }
          }

          Step-7:測試用例suite

          <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
          
          <suite name="測試demo" verbose="1" preserve-order="true">
              <parameter name="report.config" value="src/main/resources/report/extent-config.xml"/>
              <parameter name="system.info" value="com.zuozewei.extentreportdemo.config.MySystemInfo"/>
          
              <test name="測試demo" preserve-order="true">
                  <classes>
                      <class name="com.zuozewei.extentreportdemo.testCase.TestMethodsDemo"/>
                  </classes>
              </test>
          
              <listeners>
                  <listener class-name="com.zuozewei.extentreportdemo.listener.MyExtentTestNgFormatter"/>
              </listeners>
          </suite>

          四、測試報告

          1、HTML Resport 示例

          2、Email Report 示例

          五、工程目錄

          源碼地址:

          • https://github.com/zuozewei/blog-example/tree/master/Java-api-test/05-testreport/springboot-extentreport-demo1

          介:

          最新簡約美觀的網址網站引導頁HTML源碼 帶一言 隨機大圖

          圖片:

          1. 于 HTML 性能:在編寫 HTML 時,我們應該盡可能地使用最少的 HTML 標簽來構建頁面,這樣可以減少頁面的加載時間,提高頁面的性能。同時,也要注意在 HTML 中嵌套的深度不要太深,盡量減少嵌套的層級,避免重復加載數據。
          2. 標簽的作用:標簽用于定義文檔中的不同部分,比如頭部、主體、尾部等。在編寫 HTML 時,我們應該盡可能地使用標簽來組織頁面內容,這樣可以提高代碼的可讀性和維護性。
          3. 標簽的嵌套:在 HTML 中,標簽之間可以嵌套使用,這樣可以提高代碼的復用性和可維護性。但是,在嵌套時要注意層級關系,不要過深,否則會影響頁面的性能。
          4. 盒模型:盒模型是指一個元素在瀏覽器中占據的空間大小,包括內容、內邊距和邊框等部分。盒模型包含以下幾個關鍵部分:
          • content area(內容區域)
          • padding(內邊距)
          • border(邊框)
          • margin(外邊距)
          1. 合理使用 CSS 樣式:在編寫 HTML 時,我們應該盡可能地使用簡潔、規范的 CSS 樣式來布局頁面內容。這樣可以減少頁面的加載時間,提高頁面的性能。同時,也要注意不要使用過多的 CSS 屬性和類,否則會影響頁面的渲染效果和性能。
          2. 合理使用媒體查詢:媒體查詢是一種用于控制不同設備上顯示不同樣式的技術。在編寫 HTML 時,我們可以使用媒體查詢來根據設備的屏幕大小、分辨率等信息來調整頁面的布局和樣式。
          3. 壓縮代碼:在編寫 HTML 時,我們可以使用壓縮工具來壓縮代碼,這樣可以減少頁面的大小和加載時間,提高頁面的性能。
          4. 使用工具:在編寫 HTML 時,我們可以使用一些工具來幫助我們優化代碼,比如開發者工具、瀏覽器開發者工具等。這些工具可以幫助我們快速定位代碼中的問題和瓶頸,提高開發效率。
          5. 避免使用 Iframe
          6. 避免空鏈接屬性
          7. 避免節點深層級嵌套
          8. 縮減 HTML 文檔大小:提高下載速度最顯而易見的方式就是減少文件的大小,特別是壓縮內嵌在 HTML 文檔中的 JavaScript 和

          CSS 代碼,這能使得頁面體積大幅精簡。除此之外減少 HTML 文檔大小還可以采取下面幾種方法:1.刪掉 HTM 文檔對執行結果無影響的空格空行和注釋避免 Table 布局;2.使用 HTML5;3.顯式指定文檔字符集。

          13.顯式設置圖片的寬高

          14.避免腳本阻塞加載:

          當瀏覽器在解析常規的 script 標簽時,它需要等待 script 下載完畢,再解析執行,而后續的 HTML 代碼只能等待。為了避免阻塞加載,應把腳步放到文檔的末尾,如把 script 標簽插入在 body 結束標簽之前。

          #挑戰30天在頭條寫日記#


          主站蜘蛛池模板: 精品女同一区二区三区免费站| 亚洲精品国产suv一区88| 亚洲变态另类一区二区三区| 国产A∨国片精品一区二区| 日本精品无码一区二区三区久久久| 精品人妻无码一区二区色欲产成人| 国产精品女同一区二区| 国产av一区最新精品| 亚洲一区爱区精品无码| 亚洲av无码天堂一区二区三区 | 亚洲福利精品一区二区三区| 国产成人一区二区三区高清| 国产精品电影一区二区三区| 国产成人精品一区二三区熟女| 午夜爽爽性刺激一区二区视频| 国产精品夜色一区二区三区| 国模私拍福利一区二区| 精品福利视频一区二区三区| 国产成人AV一区二区三区无码| 无码人妻少妇色欲AV一区二区| 日本一区二区三区四区视频| 无码日韩人妻AV一区二区三区| 国产成人午夜精品一区二区三区| 中文字幕无线码一区| 国产精品女同一区二区| 怡红院一区二区三区| 成人一区二区免费视频| 一区二区在线免费观看| 波多野结衣AV一区二区三区中文 | 无码少妇一区二区三区浪潮AV| 久久无码人妻一区二区三区| 精品无人区一区二区三区在线| 冲田杏梨AV一区二区三区| 亚洲欧洲一区二区三区| 亚洲综合一区二区国产精品| 国产怡春院无码一区二区| 亚洲一区二区三区免费| 国产美女一区二区三区| 一区二区手机视频| 99在线精品一区二区三区| 国产精品香蕉在线一区|