Correct way to take screenshot with Robotium and Cucumber

若如初见. 提交于 2020-01-05 03:07:24

问题


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:


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


来源:https://stackoverflow.com/questions/30051678/correct-way-to-take-screenshot-with-robotium-and-cucumber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!