【问题标题】:Android espresso and account pickerAndroid espresso 和帐户选择器
【发布时间】:2014-03-09 07:47:38
【问题描述】:

我在使用 Espresso 进行仪器测试时遇到问题。 我有一个活动,当应用程序启动时会弹出帐户选择器(主要活动)。 如果客户点击取消(在对话框中),选择器会再次弹出;如果用户点击添加,则在活动结果中获取结果。

我不知道如何使用 espresso 创建一个包含该选择器的简单测试。 当我使用 MainActivity 创建 Instrumentation 测试时,我收到了以下消息: RESUMED阶段没有活动...

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity>{
    MainActivity myActivity;
    public MainActivityTest(){
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    public void testAccountPicker(){
        onView(withText("Choose an account")).check(matches(isDisplayed()));
    }
}

有人遇到过类似的问题吗?

感谢您提前回答。

【问题讨论】:

  • 您能发布您的活动生命周期方法吗?
  • Bolhoso,我只是在 onCreate 方法中有这个:Intent googlePicker = AccountPicker.newChooseAccountIntent(null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null); startActivityForResult(googlePicker, Utils.PICK_ACCOUNT_REQUEST); 并且选择帐户的逻辑.. 其他生命周期方法没有改变。

标签: android android-espresso accountpicker


【解决方案1】:

这是一个艰难的:)。这里的问题是,一旦流程离开您的应用程序(Google Account Picker 是一个外部应用程序),Espresso 就会结束测试。 Account Picker 是包com.google.android.gms 中的一个活动,因此是外部的。一旦开始,您的测试就完成了,您将永远无法匹配对话框中的任何内容。

您有三种可能的解决方案来使您的测试可行:

  • 在您的应用程序上使用类路径替换来伪造意图;或
  • 修复您的应用程序“可测试性”;或
  • 使用依赖注入,如Dagger

我将展示如何使用类路径替换。该技术非常简单:您应该将 Intent 创建隔离在一个单独的类中,例如 IntentsFactory,并在测试期间覆盖该类。

假设你的工厂在com.yourapp.factories.IntentsFactory,它是这样的:

public class IntentsFactory {
    public static Intent getAccountPickerIntent (Context context) {
        return AccountPicker.newChooseAccountIntent(null, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null);
    }
}

您应该在您的测试应用程序(比如com.yourapp.tests)中创建一个具有相同名称和方法的包,但返回一个不同的 Intent,一个模拟/虚拟的:

public class IntentsFactory {
    public static Intent getAccountPickerIntent (Context context) {
        return new Intent(context, MyDummyAccountPickerActivity.class);
    }
}

无论何时执行您的测试,它们都会使用类路径中“最近的”类,即测试中的 IntentsFactory。流程不会返回将流程发送到另一个应用程序的意图,而是会转到您项目的一个类,并且 Espresso 不会结束测试。

这里唯一需要注意的是,您必须创建MyDummyAccountPickerActivity,它将返回一个结果和一个类似于框架类返回的Bundle。活动应该存在于您的应用程序清单中,您必须指示您的模拟器 Dalvik 运行时允许使用以下命令行替换类路径(查看此 thisthis 链接)替换:

adb shell setprop dalvik.vm.dexopt-flags v=n,o=v
adb shell stop installd
adb shell start installd

然后执行你的测试。

我在测试相机时遇到了类似的问题,已经彻底讨论了in Espresso forum

【讨论】:

  • 像往常一样“昨天需要完成”我让测试暂停了一段时间。但我明白你的建议,一定会尝试的。回复晚了非常抱歉。谢谢
【解决方案2】:

看来,您必须在根视图上进行操作,在您的情况下是“帐户选择器”。试试这个:

public void testAccountPicker(){
    onView(withText("Choose an account"))
        .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
        .check(matches(isDisplayed()));
}

【讨论】:

  • 嗨 Denys,我仍然收到错误消息:“阶段中没有活动已恢复。您是否忘记启动活动。(test.getActivity() 或类似的)?”
  • 好的,您在应用程序中没有自定义帐户选择器,而是 Google 帐户选择器,对吧?在那种情况下,我认为您无法处理它,因为您在您的应用程序之外。在 android-test-kit-discuss Google 群组中提问这个问题 - groups.google.com/forum/#!forum/android-test-kit-discuss
  • 是的,你是对的。我使用谷歌帐户选择器,但不是我自己的。感谢您的回复。
【解决方案3】:

您可以通过多种方式使用 Espresso Intents https://google.github.io/android-testing-support-library/docs/espresso/intents/ 进行测试

您可以使用 expected() 语法验证是否已发送 Intent 以打开帐户选择器。您还可以使用 Intenting().respondWith() 语法通过选择器返回的结果来验证活动的行为。

如果你真的想直接与选择器交互,你可以使用 UIAutomator API:https://developer.android.com/topic/libraries/testing-support-library/index.html#UIAutomator

UIAutomator 可以在 Espresso 测试中使用。

【讨论】:

    猜你喜欢
    • 2015-02-19
    • 2017-05-16
    • 2017-08-05
    • 1970-01-01
    • 2013-10-16
    • 2014-01-07
    • 2018-08-17
    • 2013-01-01
    • 1970-01-01
    相关资源
    最近更新 更多