Correct way to take screenshot with Robotium and C

2019-07-08 02:38发布

Which is the best way to take a screenshot when one scenario fails using Robotium and Cucumber?

I have tried (without success, because it doesn't execute runTest method) with this:

import cucumber.api.CucumberOptions;
import cucumber.api.java.After;
import cucumber.api.java.Before;

@CucumberOptions(features = "features", tags = {"~@ignore"})
public class CustomInstrumentationTestCase extends ActivityInstrumentationTestCase2<LaunchActivity> {

    protected Solo solo;

    public CustomInstrumentationTestCase() {
        super(LaunchActivity.class);
    }

    @Before
    public void before() throws Exception {
        //...
    }

    @After
    public void after() throws Exception {
        //...
    }

    @Override
    protected void runTest() throws Throwable {
        try {
            super.runTest();
        } catch (Throwable t) {
            final String testCaseName = String.format("%s.%s", getClass().getName(), getName());
            solo.takeScreenshot(testCaseName);
            Log.w("Boom! Screenshot!", String.format("Captured screenshot for failed test: %s", testCaseName));

            throw t;
        }
    }
}

And I have set the permissions in the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

1条回答
啃猪蹄的小仙女
2楼-- · 2019-07-08 03:10

Your testclass seems fine. The runTest() methode is used to catch errors and failures and as a result make a screenshot.

To your class simply add a test like this:

public void testFailsForScreenShot(){
    fail();
}

Than run the test and you will find a screenshot.

Greetings

EDIT: Test for Screenshot:

import android.util.Log;
import com.robotium.solo.Solo;

public class TestScreenshot extends     ActivityInstrumentationTestCase2<LauncherActivity> {

private Solo solo;

public TestScreenshot(){
    super(LauncherActivity.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();
    solo = new Solo(getInstrumentation(), getActivity());
}

//Test if "Test" fails and takes Screenshot
public void testFailForScreenshot(){
    fail();
}


@Override
public void runTest() throws Throwable {
    try {
        super.runTest();
    } catch (Throwable t) {
        String testCaseName = String.format("%s.%s", getClass().getName(), getName());
        solo.takeScreenshot(testCaseName);
        Log.w("Screenshot taken.", String.format("Captured screenshot for failed test: %s", testCaseName));

        throw t;
    }
}
查看更多
登录 后发表回答