正确的方法来与Robotium和黄瓜截图 [英] Correct way to take screenshot with Robotium and Cucumber
本文介绍了正确的方法来与Robotium和黄瓜截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Robotium 和 Cucumber ,当一个方案失败时,采用屏幕截图的最佳方法是什么?
Which is the best way to take a screenshot when one scenario fails using Robotium and Cucumber?
我试过了(没有成功,因为它不执行 runTest 方法):
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;
}
}
}
在清单中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
推荐答案
runTest()方法用于捕获错误和失败,并作为结果进行截图。
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.
问候
编辑:测试截图:
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;
}
}
这篇关于正确的方法来与Robotium和黄瓜截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文