Commit b5683058 by dujunli

fix: 修复ExtentReports CDN资源不可访问问题 (xm-sportstest)

parent 5254f398
...@@ -10,9 +10,12 @@ ...@@ -10,9 +10,12 @@
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
<properties> <properties>
<spring.version>4.2.3.RELEASE</spring.version> <spring.version>5.3.23</spring.version>
<log4j.version>1.2.17</log4j.version> <log4j.version>2.19.0</log4j.version>
<slf4j.version>1.7.9</slf4j.version> <slf4j.version>1.7.9</slf4j.version>
<testng.version>7.6.1</testng.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.compile.encoding>UTF-8</project.compile.encoding> <project.compile.encoding>UTF-8</project.compile.encoding>
...@@ -96,7 +99,7 @@ ...@@ -96,7 +99,7 @@
<version>5.1.6</version> <version>5.1.6</version>
</dependency> </dependency>
<!--spring4.2.3.RELEASE--> <!--spring5.3.23-->
<!--spring --> <!--spring -->
<dependency> <dependency>
...@@ -258,6 +261,13 @@ ...@@ -258,6 +261,13 @@
<version>22.0</version> <version>22.0</version>
</dependency> </dependency>
<!-- 为了兼容Java11,必须引入此依赖-->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
...@@ -267,11 +277,14 @@ ...@@ -267,11 +277,14 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version> <version>3.0.0-M7</version>
<configuration> <configuration>
<!-- <forkMode>0</forkMode>-->
<!-- <argLine>-Dfile.encoding=UTF-8 -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=256m</argLine> &lt;!&ndash; 解决maven test命令时console出现中文乱码乱码 &ndash;&gt;-->
<testFailureIgnore>true</testFailureIgnore> <testFailureIgnore>true</testFailureIgnore>
<testSourceDirectory>src/main/java</testSourceDirectory>
<testClassesDirectory>target/classes</testClassesDirectory>
<includes>
<include>**/Test*.java</include>
</includes>
<!--动态指定执行的xml文件。${xmlFileName}maven文件--> <!--动态指定执行的xml文件。${xmlFileName}maven文件-->
<suiteXmlFiles> <suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile> <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
...@@ -289,12 +302,13 @@ ...@@ -289,12 +302,13 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version> <!-- 制定maven版本,制定依赖java11-->
<version>3.9.0</version>
<configuration> <configuration>
<!--源码版本 --> <!--源码版本 -->
<source>1.8</source> <source>11</source>
<!--编译的版本 --> <!--编译的版本 -->
<target>1.8</target> <target>11</target>
<!--指定编码 --> <!--指定编码 -->
<encoding>utf-8</encoding> <encoding>utf-8</encoding>
</configuration> </configuration>
...@@ -318,12 +332,15 @@ ...@@ -318,12 +332,15 @@
<!--配置resources 资源--> <!--配置resources 资源-->
<resources> <resources>
<resources>
<resource> <resource>
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<includes> <includes>
<include>**/*.properties</include> <include>**/*.properties</include>
<include>**/*.xml</include> <include>**/*.xml</include>
<include>**/*.xls</include> <include>**/*.xls</include>
<include>**/*.css</include>
<include>**/*.js</include>
</includes> </includes>
<filtering>false</filtering> <filtering>false</filtering>
</resource> </resource>
......
...@@ -2,7 +2,6 @@ package com.xiaomai.client; ...@@ -2,7 +2,6 @@ package com.xiaomai.client;
import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.ResourceCDN;
import com.aventstack.extentreports.Status; import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.model.TestAttribute; import com.aventstack.extentreports.model.TestAttribute;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter; import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
...@@ -12,7 +11,7 @@ import com.xiaomai.utils.GetFilesNum; ...@@ -12,7 +11,7 @@ import com.xiaomai.utils.GetFilesNum;
import org.testng.*; import org.testng.*;
import org.testng.xml.XmlSuite; import org.testng.xml.XmlSuite;
import java.io.File; import java.io.*;
import java.util.*; import java.util.*;
...@@ -100,6 +99,37 @@ public class ExtentTestNGIReporterListener implements IReporter { ...@@ -100,6 +99,37 @@ public class ExtentTestNGIReporterListener implements IReporter {
} }
extent.flush(); extent.flush();
replaceCDNWithLocal();
}
private void replaceCDNWithLocal() {
String htmlFilePath = OUTPUT_FOLDER + FILE_NAME;
File htmlFile = new File(htmlFilePath);
if (!htmlFile.exists()) return;
try {
StringBuilder content = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
String line;
while ((line = reader.readLine()) != null) content.append(line).append("\n");
reader.close();
String htmlContent = content.toString();
// 替换 CSS 资源 (包含模板硬编码的 hash)
htmlContent = htmlContent.replace("http://extentreports.com/resx/dist/css/extent.css", "extent.css");
htmlContent = htmlContent.replace("https://cdn.rawgit.com/anshooarora/extentreports-java/b4a58fcfd1e137bd9287244035a7c80d3d73b3af/dist/css/extent.css", "extent.css");
// 替换 JS 资源 (包含模板硬编码的 hash)
htmlContent = htmlContent.replace("http://extentreports.com/resx/dist/js/extent.js", "extent.js");
htmlContent = htmlContent.replace("https://cdn.rawgit.com/anshooarora/extentreports-java/fca20fb7653aade98810546ab96a2a4360e3e712/dist/js/extent.js", "extent.js");
htmlContent = htmlContent.replace("https://cdn.rawgit.com/anshooarora/extentreports-java/b4a58fcfd1e137bd9287244035a7c80d3d73b3af/dist/js/extent.js", "extent.js");
BufferedWriter writer = new BufferedWriter(new FileWriter(htmlFile));
writer.write(htmlContent);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
private void init() { private void init() {
...@@ -110,10 +140,8 @@ public class ExtentTestNGIReporterListener implements IReporter { ...@@ -110,10 +140,8 @@ public class ExtentTestNGIReporterListener implements IReporter {
} }
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME); ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
// 设置静态文件的DNS copyResourceFile("extent.css", OUTPUT_FOLDER + "extent.css");
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS); copyResourceFile("extent.js", OUTPUT_FOLDER + "extent.js");
//怎么样解决cdn.rawgit.com访问不了的情况
htmlReporter.config().setResourceCDN(ResourceCDN.EXTENTREPORTS);
htmlReporter.config().setDocumentTitle("自动化测试报告"); htmlReporter.config().setDocumentTitle("自动化测试报告");
// 递归获取所有api数 // 递归获取所有api数
int apiNum = GetFilesNum.show(new File("src/main/java/com/xiaomai/cases")); int apiNum = GetFilesNum.show(new File("src/main/java/com/xiaomai/cases"));
...@@ -127,6 +155,19 @@ public class ExtentTestNGIReporterListener implements IReporter { ...@@ -127,6 +155,19 @@ public class ExtentTestNGIReporterListener implements IReporter {
extent.setReportUsesManualConfiguration(true); extent.setReportUsesManualConfiguration(true);
} }
private void copyResourceFile(String resourceName, String targetPath) {
try (InputStream is = getClass().getClassLoader().getResourceAsStream(resourceName);
OutputStream os = new FileOutputStream(targetPath)) {
if (is != null) {
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) os.write(buffer, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void buildTestNodes(ExtentTest extenttest, IResultMap tests, Status status) { private void buildTestNodes(ExtentTest extenttest, IResultMap tests, Status status) {
//存在父节点时,获取父节点的标签 //存在父节点时,获取父节点的标签
String[] categories = new String[0]; String[] categories = new String[0];
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment