New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use UIAutomator to get system Screenshot #2080

Closed
fastlanebot opened this Issue Feb 18, 2016 · 19 comments

Comments

Projects
None yet
6 participants
@fastlanebot

fastlanebot commented Feb 18, 2016

Original issue by @NitroG42 - Imported from fastlane/screengrab#75

Screengrab looks awesome, but there is just a little quirk, the systembar is not shown (except for the color), and also the button bar (of buttonless devices like nexus) are only a black rectangle.

I don't know if you have some ways to workaround that (like adding the bar manually using command like convert), but if you don't, you could use UIAutomator.

It provides a method takeScreenshot(File) from the class UIDevice that take a complete screenshot of the device, system bar and button bar included.

It requires a minSdk 18, but it's easy to work around that (using a product flavor, for example).

@fastlanebot

This comment has been minimized.

fastlanebot commented Feb 19, 2016

@amostyaev commented

Great idea!

Also current implementation doesn't capture shadow below the toolbar. UiDevice.takeScreenshot(File) does that.

@fastlanebot

This comment has been minimized.

fastlanebot commented Feb 22, 2016

@trevor-e commented

Yea, seems like screengrab doesn't capture shadows anywhere, like for CardView too. Maybe it would make sense to expose how screengrab creates the screenshot files? Right now they're all private static methods which I had to copy over to my project in order to use UiDevice.

@fastlanebot

This comment has been minimized.

fastlanebot commented Feb 25, 2016

@mfurtak commented

Thanks for the reports and suggestions, all. This is definitely on our short list of things to address!

@fastlanebot

This comment has been minimized.

fastlanebot commented Mar 1, 2016

@simplysoft commented

We had also to workaround the issue by coping some private code from the Screengrab file and the use UiAutomator to take the screenshot. In the end the actual screenshot method looks like this:

Bitmap bitmap = InstrumentationRegistry.getInstrumentation().getUiAutomation().takeScreenshot();

File file = screenshotFile(name);
OutputStream fos = null;
try {
    fos = new BufferedOutputStream(new FileOutputStream(file));
    bitmap.compress(Bitmap.CompressFormat.PNG, FULL_QUALITY, fos);
    Chmod.chmodPlusR(file);
} finally {
    bitmap.recycle();
    if (fos != null) {
        fos.close();
    }
}

while screenshotFile was taken from the Screengab file to match the convetion, this way the tool it will happily take the UiAutomator screenshot instead of the own ones:

private static File screenshotFile(String screenshotName) throws IOException {
        File screenshotDirectory = getFilesDirectory(InstrumentationRegistry.getTargetContext(), Locale.getDefault());
        String screenshotFileName = System.currentTimeMillis() + NAME_SEPARATOR + screenshotName + EXTENSION;
        return new File(screenshotDirectory, screenshotFileName);
    }
@fastlanebot

This comment has been minimized.

fastlanebot commented Mar 7, 2016

@VictorJava commented

Hi,
I have the same problem.

I following suggestion simplysoft(previous message).
This works, but when I have DialogFragment and ProgressDialog in the same time - I cant capture ProgressDialog. It isnt visible on screen.
In another words. If I have two dialogs the top isn`t captured.

Could you help me?

@mfurtak

This comment has been minimized.

Contributor

mfurtak commented Apr 6, 2016

Hi @simplysoft - I've been taking a closer look at this lately, and I have a question for you:

Using your work-around, have you ever run into a scenario where you're taking a screenshot early in the lifecycle of a test and you capture something that isn't the app screen? For example, capturing a shot of your home screen, or an incompletely displayed activity?

If so, how have you worked around it?

@trevor-e

This comment has been minimized.

trevor-e commented Apr 6, 2016

For what it's worth, my screenshot code looks like:

public static void screenshot(String screenshotName) throws Exception {
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
        File e = getFilesDirectory(context, Locale.getDefault());
        String screenshotFileName = System.currentTimeMillis() + "_" + screenshotName + ".png";
        File screenshotFile = new File(e, screenshotFileName);

        UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).takeScreenshot(screenshotFile);
}

I haven't noticed either of the issues you described. I generated ~80 screenshots in my last run and they are all fine.

@simplysoft

This comment has been minimized.

simplysoft commented Apr 6, 2016

We don't have noticed such problems, but we only take the first screenshot after some clicks.

@mfurtak

This comment has been minimized.

Contributor

mfurtak commented Apr 6, 2016

@trevor-e Cool, that syntax does wind up a little nicer, and it effectively makes the same UiAutomation.takeScreenshot() call internally.

Out of curiosity, are either of you using the locale switching capabilities of screengrab?

@simplysoft

This comment has been minimized.

simplysoft commented Apr 6, 2016

Yes, we are automatically creating screenshots for about 40 locales

@mfurtak

This comment has been minimized.

Contributor

mfurtak commented Apr 6, 2016

@simplysoft Wow, excellent! I'm glad it's working well for you 👍

@trevor-e

This comment has been minimized.

trevor-e commented Apr 6, 2016

Yes, I'm also using LocaleTestRule and it works great, clever use of test rule + instrumentation args. :)

@mfurtak

This comment has been minimized.

Contributor

mfurtak commented Apr 6, 2016

@trevor-e Great - thanks! 😄 I guess I need to figure out why I'm getting inconsistent results.

More questions, if you don't mind... are you using emulators or real devices?

@trevor-e

This comment has been minimized.

trevor-e commented Apr 6, 2016

I have been using a real device, but I think I remember it working fine on an emulator (Genymotion).

@simplysoft

This comment has been minimized.

simplysoft commented Apr 6, 2016

we are only using the emulator for taking screenshot

@sachingarg05

This comment has been minimized.

sachingarg05 commented Jun 7, 2016

We ran into issues where screengrab will only capture black images for an OpenGL game. Using the (awesome) code snippet by @trevor-e and @simplysoft fixed the problem for us too. Trying to capture a screenshot 'immediately' on activity launch sometimes gives empty screenshots, but we can workaround that by adding a delay.

Any update on when this will be fixed in screengrab?

@mfurtak mfurtak referenced this issue Aug 5, 2016

Merged

screengrab screenshot strategies #5621

2 of 2 tasks complete
@mfurtak

This comment has been minimized.

Contributor

mfurtak commented Aug 5, 2016

@sachingarg05 I am working on getting a UiAutomation-based implementation in right now. 👍

Once I have it on master, if folks are able to give it a try, that would be a big help!

mfurtak added a commit that referenced this issue Aug 8, 2016

Add UiAutomator screenshot strategy (#5621)
Refactors `Screengrab` into more collaborators, while keeping the existing `Screengrab` interface working.

The goal is to introduce a new strategy for screenshot capture based on [UiAutomation](#2080 (comment)). It allows capture on Android N, and more correctly captures multi-layer/window situations (dialogs, etc.) Some verification is needed to determine if it also helps Google Maps & video player use-cases.

The new strategy depends on `UiAutomation` which has an API >= 18 requirement. We enforce that in code, and override the manifest merger checks that would force the (optional) API level requirement onto consuming apps. So, our `minSdkVersion` remains **8** for the library.

The `JUnit3StyleTests` are removed, as they are not a recommended configuration.

The example application and tests have been slightly expanded to include a button that opens a dialog to verify multi-window capture by `UiAutomatorScreenshotStrategy`
@mfurtak

This comment has been minimized.

Contributor

mfurtak commented Aug 8, 2016

Hi all, the changes from #5621 are now on master. If you have time to give it a try before I release it, that would be amazing. 💯

The README has more info about how enable the UiAutomator strategy, but note that v0.5.0 hasn't been released yet.

@fastlane-bot

This comment has been minimized.

fastlane-bot commented Oct 7, 2016

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest fastlane version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@mfurtak mfurtak closed this Oct 11, 2016

@fastlane fastlane locked and limited conversation to collaborators Jan 9, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.