用Robotium和Cucumber截取屏幕截图的正确方法

时间:2015-05-05 11:24:43

标签: android cucumber robotium android-testing

当一个场景使用 Robotium 黄瓜失败时,采用屏幕截图的最佳方法是什么?

我已经尝试过(没有成功,因为它没有执行 runTest 方法):

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

我在清单中设置了权限:

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

1 个答案:

答案 0 :(得分:0)

您的测试类似乎没问题。 runTest()方法用于捕获错误和失败,从而制作屏幕截图。

对于您的班级,只需添加如下测试:

public void testFailsForScreenShot(){
    fail();
}

比运行测试,你会找到一个截图。

问候

编辑:测试屏幕截图:

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