AWS Device Farm测试报告生成失败及本地保存问题咨询
Hey there, let's break down why your HTML test report works locally but fails on AWS Device Farm. The root issues are tied to environment-specific file paths and missing file-writing logic in your code. Here's how to fix each problem step by step:
1. Hardcoded Local APK Path Won't Work on Device Farm
Your code uses a fixed Windows path for the APK:
String filePath="C:\\Appium\\app-qc-debug.apk";
AWS Device Farm doesn't have a C: drive, and the APK's location is managed by the platform. Instead, use the DEVICE_FARM_APP_PATH environment variable provided by Device Farm, with a fallback for local testing:
// Get APK path from Device Farm env var, fallback to local path for testing String apkPath = System.getenv("DEVICE_FARM_APP_PATH"); if (apkPath == null || apkPath.isEmpty()) { apkPath = "C:\\Appium\\app-qc-debug.apk"; // Local fallback } ApkFile apkFile = new ApkFile(new File(apkPath));
2. You're Not Writing the HTML to a File (Or Using the Wrong Directory)
Looking at your code, you build the HTML content in htmlStringBuilder, but there's no logic to write that content to a file! Even if you had that logic locally, using a local path won't work on Device Farm.
Device Farm provides the DEVICE_FARM_TEST_REPORT_DIR environment variable—this is the only directory where your reports will be collected and stored after the test runs. Add this code at the end of your try block to write the report:
// Get Device Farm's report directory, fallback to local for testing String reportDir = System.getenv("DEVICE_FARM_TEST_REPORT_DIR"); if (reportDir == null || reportDir.isEmpty()) { reportDir = "C:\\Appium\\reports"; // Local fallback } // Create directory if it doesn't exist File reportDirectory = new File(reportDir); if (!reportDirectory.exists()) { reportDirectory.mkdirs(); } // Generate a unique report filename (avoid invalid characters) String safeDateTime = currentDateTime.replace("/", "-").replace(",", "").replace(":", "-"); String reportFilePath = new File(reportDir, "test-report-" + safeDateTime + ".html").getAbsolutePath(); // Write the HTML content to the file try (Writer writer = new OutputStreamWriter(new FileOutputStream(reportFilePath), "UTF-8")) { writer.write(htmlStringBuilder.toString()); } catch (IOException e) { e.printStackTrace(); }
3. Screenshot Path Compatibility
If your a.getScreenshotPath() uses a local absolute path, it won't work on Device Farm. Use the DEVICE_FARM_SCREENSHOT_DIR environment variable to save screenshots, and reference them correctly in your report:
// When saving screenshots, use this path String screenshotDir = System.getenv("DEVICE_FARM_SCREENSHOT_DIR"); if (screenshotDir == null || screenshotDir.isEmpty()) { screenshotDir = "C:\\Appium\\screenshots"; // Local fallback } // Save your screenshots to this directory
4. Ensure Dependencies Are Packaged
Make sure the net.dongliu.apk.parser library is included in your test suite package (e.g., via a fat JAR if using Maven/Gradle). Device Farm won't automatically download missing dependencies, so missing libraries will cause runtime errors.
Full Modified Code Snippet
Here's how your WriteResultToHtml method should look with these fixes:
public static void WriteResultToHtml(List<TestReportSteps> items, String getCurrentDateTime, String getCurrentTime) { try { // Fix 1: Get APK path (compatible with Device Farm and local) String apkPath = System.getenv("DEVICE_FARM_APP_PATH"); if (apkPath == null || apkPath.isEmpty()) { apkPath = "C:\\Appium\\app-qc-debug.apk"; } ApkFile apkFile = new ApkFile(new File(apkPath)); 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(); StringBuilder actualResult = new StringBuilder(); StringBuilder htmlStringBuilder = new StringBuilder(); // ... Keep your existing HTML building code here ... // Fix 2: Write HTML to Device Farm's report directory String reportDir = System.getenv("DEVICE_FARM_TEST_REPORT_DIR"); if (reportDir == null || reportDir.isEmpty()) { reportDir = "C:\\Appium\\reports"; } File reportDirectory = new File(reportDir); if (!reportDirectory.exists()) { reportDirectory.mkdirs(); } String safeDateTime = currentDateTime.replace("/", "-").replace(",", "").replace(":", "-"); String reportFilePath = new File(reportDir, "test-report-" + safeDateTime + ".html").getAbsolutePath(); try (Writer writer = new OutputStreamWriter(new FileOutputStream(reportFilePath), "UTF-8")) { writer.write(htmlStringBuilder.toString()); } } catch (Exception e) { e.printStackTrace(); } }
Quick Extra Tips
- Add debug logs to print the values of environment variables (like
DEVICE_FARM_APP_PATH) to help troubleshoot if things still go wrong. - Double-check your test suite packaging to ensure all classes and dependencies are included correctly.
内容的提问来源于stack exchange,提问作者Rizwan Ebrahim

