2015-05-05 26 views
2

这是采取截图,当一个场景中使用Robotium黄瓜失败的最佳方式?正确的方式,采取截图与Robotium和黄瓜

我曾尝试(没有成功,因为它不执行的runTest方法)与此:

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

@CucumberOptions(features = "features", tags = {"[email protected]"}) 
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"/> 
+0

你有设置在被测应用程序的AndroidManifest.xml中的许可标签? – Renas

+0

将测试方法的名称更改为以测试开始。所以在你的情况下,它可能是testRun()。 – Renas

+0

“所以你的情况可能是testRun()”nope--他的代码看起来很好! testRun方法被使用,因为断言错误和报告失败被抛出。 – PKAP

回答

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

这不起作用:( – David

+0

有什么在日志文件中?只是做了一个快速测试,它在这里工作... – PKAP

+0

你能告诉我一个例子吗? – David