2015-05-05 67 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