我正在使用AWS设备场。我在本地系统上运行的测试脚本按预期工作,并在指定路径的本地系统中生成报告。现在,当我在设备场中运行代码时,报告无法获得生成。我错过了什么?
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();
}
}
}
答案 0 :(得分:2)
代码引用的路径不存在于服务器场的设备主机中。用于android测试的Device Host是一台Linux机器,根据我的经验,我们可以访问tmp
目录。通过使用Device Farm的custom artifacts功能和tmp目录,这应该是可能的。尝试将html文件的路径更改为:
htmlFilepath = "/tmp/reports/testfile.html";
然后,使用Web控制台,明确标记要导出的目录。
睾丸完成后,您应该会看到customer artifacts
的链接。
此外,您可能对其他测试报告选项感兴趣,例如
而不是从头开始编写自己的。
HTH
-James
答案 1 :(得分:0)
对于在尝试将测试日志/报告保存在function print() {
var canvas = document.getElementById('LineWithLine');
var imgcanvas = canvas.toDataURL();
var myWindow = window.open();
myWindow.document.write("<img src = '" + imgcanvas + "'/>");
myWindow.document.close();
myWindow.focus();
myWindow.print();
myWindow.close();
}
$('#Print').on('click', function() {
print();
});
或任何其他路径中时收到java.io.FileNotFoundException:(Permission denied)
异常的用户。您可以使用$WORKING_DIRECTORY
保存您的工件,这对我有用。
$DEVICEFARM_LOG_DIR
使用此目录存储所有工件。