【问题标题】:Capturing Android screenshot with appium使用 appium 捕获 Android 屏幕截图
【发布时间】:2014-05-31 22:23:20
【问题描述】:

我目前正在使用 java 和 junit 在 iOS 上为 appium 测试捕获屏幕截图。在测试完成之前,它会运行此代码以在终止连接之前获取屏幕上最后可见的内容

@After
public void tearDown() throws Exception {
    if (platform.equals(iOS)) {
        captureScreenshot(testName.getMethodName());
    }
    driver.quit();
}

@SuppressWarnings("Augmenter")
public void captureScreenshot(String testName) {
    String imagesLocation = "target/surefire-reports/screenshot/" + platform + "/";
    new File(imagesLocation).mkdirs(); // Insure directory is there
    String filename = imagesLocation + testName + ".jpg";

    try {
        Thread.sleep(500);
        WebDriver augmentedDriver = new Augmenter().augment(driver);
        File scrFile = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File(filename), true);
    } catch (Exception e) {
        System.out.println("Error capturing screen shot of " + testName + " test failure.");
        // remove old pic to prevent wrong assumptions
        File f = new File(filename);
        f.delete(); // don't really care if this doesn't succeed, but would like it to.
    }
}

这种排序适用于 android,但最近它完全终止了正在运行的测试,但这可能与它试图获取快照的设备有关。在它保存文件之前,但图像会出现空白或损坏。

有人知道如何使用 appium 在 android 上进行图像/屏幕捕获吗?也许与 UIAutomator 相关?

【问题讨论】:

  • 你试过没有'true'参数吗:FileUtils.copyFile(scrFile, new File(filename), true);并且还使用最新的appium?我正在使用非常相似的代码,对我来说效果很好

标签: java android ios appium


【解决方案1】:

我碰巧通过谷歌搜索找到了这个几乎相同的问题 - Android 模拟器中的 Appium 屏幕截图显示为空白。我正在使用与您在上面描述的非常相似的“本机”方法 - 以及 ATU 框架内的方法。

WebElement appArea = wd.findElementByXPath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]");
ATUReports.add("Main Screen Area Screenshot", LogAs.INFO, new CaptureScreen(appArea));

两者都返回空白/透明。稍微好消息是,该空白/透明图像的尺寸正是我想要捕获的尺寸 - 避免日期差异会导致图像比较错误的状态栏。也就是说,如果没有实际的可见像素(用于最终将对象区域抓取与经过验证的基线图像匹配),它并没有多大用处。

我尝试将上下文设置为“NATIVE_APP”,但似乎没有任何帮助。我 - 总之 - 烦恼。如果您在这方面取得了任何进展,我很乐意阅读/听听您是如何工作的。也许下一步是去 Appium Google Group。

编辑:在我的案例中,我发现了核心问题 - 使用 HAXM 加速会导致屏幕截图为空白。请注意,如果在测试功能中设置的基本设备配置文件是在选择“主机 GPU”的情况下定义的,这会影响物理设备上的测试运行。

【讨论】:

    【解决方案2】:

    你可以用这个方法:

    public void screenshot(String path_screenshot) throws IOException{
        File srcFile=driver.getScreenshotAs(OutputType.FILE);
        String filename=UUID.randomUUID().toString(); 
        File targetFile=new File(path_screenshot + filename +".jpg");
        FileUtils.copyFile(srcFile,targetFile);
    }
    

    对我来说很好用。

    【讨论】:

    • 对我也很有效。干杯!
    猜你喜欢
    • 2012-01-20
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 2020-12-22
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    相关资源
    最近更新 更多