Espresso.pressBack()不调用onBackPressed() [英] Espresso.pressBack() does not call onBackPressed()

查看:127
本文介绍了Espresso.pressBack()不调用onBackPressed()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Espresso课堂上:

In Espresso class:

 @Rule
    public IntentsTestRule<MainActivity> mIntentsRule = new IntentsTestRule<>(
            MainActivity.class);

@Test
public void test_backButton(){
onView(withId(R.id.NEXT_ACTIVITY)).perform(scrollTo(), click());

Espresso.pressBack();
}

活动中:

 @Override
public void onBackPressed() {
    Log.d("TEST_pressBack", "inside onBackPressed()");
    do_something();
    super.onBackPressed();

}
   @Override
public void finish() {
    Log.d("TEST_pressBack", "inside finish()");
    super.finish();

}

当我调用Espresso测试方法时,执行将直接转到finish().

When I call the Espresso test method the execution goes directly to finish().

当我按下Activity中的后退按钮(用手)时,执行首先在onBackPressed()中执行,然后执行finish(). 如何使用Espresso测试功能onBackPressed()? 谢谢!

When I press the back button (with my hand) in the Activity the execution goes firstly in onBackPressed() and then to finish(). How can I test the function onBackPressed() with Espresso? Thanks!

这是我的错误.问题是在活动"中我想打电话给按回",屏幕键盘被打开了.当软键盘打开时,按钮不会调用onBackPressed,而是使键盘不显示.我连续尝试了两个pressBack(),它正常工作:

It is my error. The problem was that in Activity in which I wanted to call pressBack the onscreen keyboard was opened. When the soft keyboard is open then the press-button does not call onBackPressed but instead makes the keyboard non-displayed. I tried with two pressBack() in a row and it worked correctly:

    @Rule
    public IntentsTestRule<MainActivity> mIntentsRule = new IntentsTestRule<>(
            MainActivity.class);

@Test
public void test_backButton(){
onView(withId(R.id.NEXT_ACTIVITY)).perform(scrollTo(), click());

Espresso.pressBack();

//The extra pressBack()
Espresso.pressBack();

}

推荐答案

如果您不在root活动中,则Espresso.pressBack()方法看起来确实可以按照您期望的方式工作.看看它的实现注释:

It looks like the Espresso.pressBack() method does just work the way you expect it if you are not in the root activity. When you take a look at it's implementation comment:

  /**
  * Press on the back button.
  *
  * @throws PerformException if currently displayed activity is root activity, since pressing back
  *         button would result in application closing.
  */
  public static void pressBack() {
        onView(isRoot()).perform(ViewActions.pressBack());
  }

我已经对其进行了测试,如果您在不是您的根目录活动的活动中进行此操作,它会很好地工作.如果您想在那里做,我建议您改用ui-automator(ui-automator在浓缩咖啡测试中完全可用):

I tested it and it works fine if you are doing this in an activity that is not your root activity. If you want to do it there, I would suggest you use ui-automator instead (ui-automator is perfectly usable inside espresso tests):

将此添加到您的gradle中:

Add this to your gradle:

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

然后在您的测试中执行此操作:

And then do this in your test:

UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mDevice.pressBack();

这篇关于Espresso.pressBack()不调用onBackPressed()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆