Rajesh
Rajesh

Reputation: 4769

Exception while using Uiautomator to capture image/screenshot

While using the UiautomatorTestcase class to capture the screenshot of my main activity I am facing the following exception.

The sample code which I'm using

public class MyActivityTest extends UiAutomatorTestCase {

    public UiautomatorAdaptxt() {
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void setUp() throws Exception {
        // TODO Auto-generated method stub
        super.setUp();

    }

    public void testOpenMainActivity() throws UiObjectNotFoundException {
        -->Here I use the code to open my Activity<--
        getUiDevice().takeScreenshot(storePath);
    }
}

The exception I'm facing with is

java.lang.NoSuchMethodError: com.android.uiautomator.core.UiDevice.takeScreenshot at .testOpenMainActivity(MyActivityTest .java:31) at java.lang.reflect.Method.invokeNative(Native Method) at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAutomatorTestRunner.java :124) at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutomatorTestRunner.java:8 5) at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.java:76) at com.android.commands.uiautomator.Launcher.main(Launcher.java:83) at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method) at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235) at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 3237

Answers (1)

vidstige
vidstige

Reputation: 13085

You need an Android 4.2 device or later to take screenshots this way.

This is because the takeScreenshot method was added in Android 4.2 or API Level 17 as the target is called.

See the official documentation.

What's happening is that when you compile on your local machine you compile against android-17 or later containing this method, hence compilation succeeds. However, when deployed on a target android device at android-16, this method is missing. When the test code tries to call this method it throws the NoSuchMethodError exception.

As a workaround, you can take screenshots using adb like so

adb shell screencap -p /data/local/tmp/screen-capture.png
adb pull /data/local/tmp/screen-capture.png <localfile.png>
adb shell rm /data/local/tmp/screen-capture.png

Upvotes: 2

Related Questions