我正在使用AWS设备群。我的测试脚本在本地系统上运行时按预期工作,并在指定路径下的本地系统中生成报告。现在,当我在设备群中运行代码时,报告不会生成。我是否遗漏了什么?
这是我用来将测试用例写入html报告的测试代码。
package testOutput;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import report.TestReportSteps;
public class TestResultHtml {
public static void WriteResultToHtml(List<TestReportSteps> items, String getCurrentDateTime, String getCurrentTime) {
try {String filePath="C:\\\\Appium\\\\app-qc-debug.apk";
ApkFile apkFile = new ApkFile(new File(filePath));
ApkMeta apkMeta = apkFile.getApkMeta();
String Version=apkMeta.getVersionName();
DateFormat df = new SimpleDateFormat("dd/MM/yy, HH:mm:ss");
Date dateobj = new Date();
String currentDateTime = df.format(dateobj);
StringBuilder color = new StringBuilder();
StringBuilder status = new StringBuilder();
// define a HTML String Builder
StringBuilder actualResult = new StringBuilder();
StringBuilder htmlStringBuilder = new StringBuilder();
// append html header and title
htmlStringBuilder.append(
"<html><head><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css\" integrity=\"sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm\" crossorigin=\"anonymous\">\r\n"
+ "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js\" integrity=\"sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl\" crossorigin=\"anonymous\"></script><title>Appium Test </title></head>");
// append body
htmlStringBuilder.append("<body>");
// append table
//if (count == 0)
{
htmlStringBuilder.append("<table class=\"table table-striped table-bordered\">");
htmlStringBuilder.append("<tr><th style=\"background-color:#a6a6a6\">Date Time</th><td>"
+ currentDateTime
+ "</td><th style=\"background-color:#a6a6a6\">Environment Tested</th><td>QC</td></tr>"
+ "<tr><th style=\"background-color:#a6a6a6\">OS</th><td>Android</td><th style=\"background-color:#a6a6a6\">Application</th><td>app-qc-debug.apk</td></tr>"
+ "<tr><th style=\"background-color:#a6a6a6\">Script Name</th><td colspan=\""
+ 3
+ "\">Cityvan Workflow</td>"
+ "<th style=\"background-color:#a6a6a6\">Build Version</th><td>"+Version+"</td></tr><tr><th style=\"background-color:#a6a6a6\">Objective</th><td colspan=\""
+ 3 + "\">To verify that cityvan app is working as expected</td><tr><tr></table>");
}
// append row
htmlStringBuilder.append("<table class=\"table table-striped\">");
htmlStringBuilder.append(
"<thead style=\"background-color:#007acc\"><tr><th><b>TestObjective</b></th><th><b>StepName</b></th><th><b>StepDescription</b></th><th><b>ExpectedResult</b></th><th><b>ActualResult</b></th><th><b>Status</b></th><th><b>Screenshot</b></th></tr></thead><tbody>");
// append row
for (TestReportSteps a : items) {
if (!a.getActualResultFail().isEmpty()) {
status.append("Fail");
color.append("red");
actualResult.append(a.getActualResultFail());
} else {
status.append("Pass");
color.append("green");
actualResult.append(a.getActualResultPass());
}
if (a.getScreenshotPath()!=null)
{
htmlStringBuilder.append("<tr><td>" + a.getTestObjective() + "</td><td>" + a.getStepName()
+ "</td><td>" + a.getStepDescription() + "</td><td>" + a.getExpectedResult() + "</td><td>"
+ actualResult + "</td><td style=\"color:" + color + ";font-weight:bolder;\">" + status
+ "</td><td><a href=\"" + a.getScreenshotPath() + "\">Click here</a></td></tr>");
}
else
{
htmlStringBuilder.append("<tr><td style=\"font-weight:bold\">" + a.getTestObjective() + "</td><td>"
+ a.getStepName() + "</td><td>" + a.getStepDescription() + "</td><td>"
+ a.getExpectedResult() + "</td><td>" + actualResult + "</td><td style=\"color:" + color
+ ";font-weight:bolder;\">" + status + "</td><td></td></tr>");
}
actualResult.delete(0, actualResult.length());
color.delete(0, color.length());
status.delete(0, status.length());
}
// close html file
htmlStringBuilder.append("</tbody></table></body></html>");
// write html string content to a file
String htmlFilepath = "";
htmlFilepath = "D:\\FinalAppiumWorkspace\\AppiumMavenProject2\\src\\test\\java\\testOutput\\HtmlReport\\" + getCurrentDateTime + "\\testfile"
+ getCurrentTime + "\\testfile.html";
WriteToFile(htmlStringBuilder.toString(), htmlFilepath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void WriteToFile(String fileContent, String fileName) throws IOException, FileNotFoundException {
File file = new File(fileName);
file.getParentFile().mkdirs();
PrintWriter out = null;
if (file.exists() && !file.isDirectory())
{
out = new PrintWriter(new FileOutputStream(new File(fileName), true));
out.append(fileContent);
out.close();
} else
{
// write to file with OutputStreamWriter
OutputStream outputStream = new FileOutputStream(file.getAbsoluteFile(), false);
Writer writer = new OutputStreamWriter(outputStream);
writer.write(fileContent);
writer.close();
}
}
}
2条答案
按热度按时间mwg9r5ms1#
代码引用的路径在场的设备主机中不存在。Android测试的设备主机是Linux计算机,根据我的经验,我们可以访问
tmp
目录。通过使用设备场的custom artifacts功能和tmp目录,这应该是可能的。请尝试将html文件的路径更改为:然后,使用Web控制台明确标记要导出的目录。
测试完成后,您应该会看到
customer artifacts
的链接。此外,您可能对测试报告的其他选项感兴趣,例如
而不是从头开始编写自己的代码。
高温
xzlaal3s2#
对于那些在尝试将测试日志/报告保存到
$WORKING_DIRECTORY
或任何其他路径时收到java.io.FileNotFoundException:(Permission denied)
异常的用户,您可以使用$DEVICEFARM_LOG_DIR
来保存您的工件,这对我很有效。使用此目录存储任何工件。