android拍照功能实验,android – 如何让Spoon为Espresso测试拍摄截图...

这就是我现在这样做的方式:

public class MainScreenTest extends BaseStatelessBlackBoxEspressoTest {

public MainScreenTest() {

super(LaunchActivity.class);

}

public void testMainScreen() {

// Unfortunately this must be explicitly called in each test :-(

setUpFailureHandler();

onView(withId(R.id.main_circle)).

check(matches(isDisplayed()));

}

}

我的基础Espresso测试类设置自定义FailureHandler(我喜欢使用基类来保存许多其他常用代码):

public abstract class BaseStatelessBlackBoxEspressoTest extends BaseBlackBoxTest {

public BaseStatelessBlackBoxEspressoTest(Class clazz) {

super(clazz);

}

@Before

public void setUp() throws Exception {

super.setUp();

getActivity();

}

public void setUpFailureHandler() {

// Get the test class and method. These have to match those of the test

// being run, otherwise the screenshot will not be displayed in the Spoon

// HTML output. We cannot call this code directly in setUp, because at

// that point the current test method is not yet in the stack.

StackTraceElement[] trace = Thread.currentThread().getStackTrace();

String testClass = trace[3].getClassName();

String testMethod = trace[3].getMethodName();

Espresso.setFailureHandler(new CustomFailureHandler(

getInstrumentation().getTargetContext(),

testClass,

testMethod));

}

private static class CustomFailureHandler implements FailureHandler {

private final FailureHandler mDelegate;

private String mClassName;

private String mMethodName;

public CustomFailureHandler(Context targetContext, String className, String methodName) {

mDelegate = new DefaultFailureHandler(targetContext);

mClassName = className;

mMethodName = methodName;

}

@Override

public void handle(Throwable error, Matcher viewMatcher) {

try {

mDelegate.handle(error, viewMatcher);

} catch (Exception e) {

SpoonScreenshotAction.perform("espresso_assertion_failed", mClassName, mMethodName);

throw e;

}

}

}

}

/**

* Source: https://github.com/square/spoon/issues/214#issuecomment-81979248

*/

public final class SpoonScreenshotAction implements ViewAction {

private final String tag;

private final String testClass;

private final String testMethod;

public SpoonScreenshotAction(String tag, String testClass, String testMethod) {

this.tag = tag;

this.testClass = testClass;

this.testMethod = testMethod;

}

@Override

public Matcher getConstraints() {

return Matchers.anything();

}

@Override

public String getDescription() {

return "Taking a screenshot using spoon.";

}

@Override

public void perform(UiController uiController, View view) {

Spoon.screenshot(getActivity(view), tag, testClass, testMethod);

}

private static Activity getActivity(View view) {

Context context = view.getContext();

while (!(context instanceof Activity)) {

if (context instanceof ContextWrapper) {

context = ((ContextWrapper) context).getBaseContext();

} else {

throw new IllegalStateException("Got a context of class "

+ context.getClass()

+ " and I don't know how to get the Activity from it");

}

}

return (Activity) context;

}

public static void perform(String tag, String className, String methodName) {

onView(isRoot()).perform(new SpoonScreenshotAction(tag, className, methodName));

}

}

我很乐意找到一种避免在每次测试中调用setUpFailureHandler()的方法 – 如果您对如何避免这种情况有一个好主意,请告诉我!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值