Dino
Dino

Reputation: 158

How to make takeScreenshot() work on Robotium when calling JUnit via command line?

I am facing a problem with takeScreenshot() on a not-rooted phone.

When executing JUnit via command line, e.g.

adb -s 0123456789ABCDEF shell am instrument -w -e class com.my.android.app.LoginTestset#test_login_normal com.my.tests/android.test.InstrumentationTestRunner 

It is not storing the screenshots. I have tried to run the test case within Eclipse and the screenshot is saved successfully. Also I tried the above command on a rooted phone and the takeScreenshot() worked.

Please note that I have added the required permissions in AndroidManifest.xml at the app under test.

It seems there is a problem with permissions

Upvotes: 1

Views: 1043

Answers (1)

Dino
Dino

Reputation: 158

It seems there is a known issue in Robotium (4.1) which has been reported here.

Until there is an official fix, I would like to contribute my personal hack which worked for me even when JUnit starts via command line.

I called it JUnit during the execution of various tests as well as at the dearDown();

public void takeScreenshot(final String filename) {

    //hack -to ensure that the current view has been fully loaded
        while(view.equals(null)) {
            m_solo.sleep(500);
        }   

        View view = View view = m_solo.getCurrentViews().get(0).getRootView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();

        File directory = new File("/mnt/sdcard/Robotium-Screenshots/");
        directory.mkdirs();

        if (bitmap != null) {
            try {
                File outputFile = new File(directory, filename + ".jpg");
                FileOutputStream ostream = new FileOutputStream(outputFile);
                bitmap.compress(CompressFormat.JPEG, 100, ostream);
                ostream.close();
            } catch (Exception e) {
                logError(e.getMessage());
            }
        }
    }   

Upvotes: 1

Related Questions