Java 使用 appium 捕获 Android 屏幕截图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23119724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:52:14  来源:igfitidea点击:

Capturing Android screenshot with appium

javaandroidiosappium

提问by plosco

I currently have something working to capture screenshots on iOS for appium tests using java and junit. Before a test finishes it runs this code to get the last visible thing on the screen before killing the connection

我目前有一些工作可以使用 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.
    }
}

This sorta works for android but lately it has completely killed the running test but this might have to do with the device it is trying to get the snapshot of. Before it would save the file but the image would come up blank or broken.

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

Anyone know how to get image/screen capturing working on android using appium? Perhaps something with UIAutomator?

任何人都知道如何使用 appium 在 android 上获取图像/屏幕捕获?也许是 UIAutomator 的东西?

采纳答案by DIEDHIOU

You can use this method:

您可以使用此方法:

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);
}

It works fine for me.

这对我来说可以。

回答by Houston Haynes

I happened to find this via Google search with nearly the same problem - Appium screenshots in the Android emulator come up blank. I'm using both the 'native' method much like you describe above - and - the method within the ATU framework.

我碰巧通过谷歌搜索找到了这个几乎相同的问题 - 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));

and both come back blank/transparent. The marginally good news is that the dimension of that blank/transparent image is exactly the size I want to capture - avoiding the status bar where date diffs would cause error on image comparison. That said, it's not much use without actual viewable pixels (for eventual matching of object area grabs to validated baseline images).

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

I tried setting context to "NATIVE_APP" and nothing seems to help. I am - in a word - vexed. If you've made any progress with this I'd love to read/hear how you got it working. Maybe the next step is to go to the Appium Google Group.

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

EDIT: I found the core issue in my case - using HAXM acceleration causes the blank screen shots. Note this alsoaffects test run on physical devices if the base device profile set in the test's capabilities is defined with "Host GPU" selected.

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