当前位置: 首页 > 知识库问答 >
问题:

Android Espresso perform(click())不起作用

朱浩大
2023-03-14

我正在使用espresso和Mockito为一个Android应用程序编写测试。在一些情况下,当我试图对视图或按钮执行单击操作时,单击操作在测试完成之前没有注册。

public void leavePageClicked(View v) {
    Log.i(TAG, "Leaving page with url: "+url);
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(browserIntent);
}
<Button
        android:id="@+id/leaveApplicationButton"
        style="@style/purple_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="45dp"
        android:onClick="leavePageClicked"
        android:text="@string/leave_application"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/urlTextView"/>

测试:

@Rule
public IntentsTestRule<LeavingPageActivity> intentsTestRule =
            new IntentsTestRule<LeavingPageActivity>(LeavingPageActivity.class, true, false);


@Before
public void setup() {
    Context targetContext = InstrumentationRegistry.getInstrumentation()
            .getTargetContext();
    Intent intent = new Intent(targetContext, LeavingPageActivity.class);
    intent.putExtra(LeavingPageActivity.EXTRA_URL, "http://www.google.com");
    intentsTestRule.launchActivity(intent);
}

@After
public void cleanup() {
    intentsTestRule.finishActivity();
}

@Test
public void testLeavePageClick() {
   intending(allOf(
           hasAction(Intent.ACTION_VIEW)
   )).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
    onView(withId(R.id.leaveApplicationButton)).perform(click());
    intended(allOf(
            hasAction(Intent.ACTION_VIEW),
            hasData("http://www.google.com")
    ));
}

log.i(标记,“用URL离开页面:”+URL);从不在logcat中写入。

11-22 13:31:36.339 24319-24337/com.app.test I/TestRunner: started: testLeavePageClick(com.app.test.LeavingPageActivityTest)
11-22 13:31:36.339 24319-24319/com.app.test I/MonitoringInstr: Activities that are still in CREATED to STOPPED: 0
11-22 13:31:36.339 24319-24337/com.app.test I/ActivityTestRule: Launching activity: ComponentInfo{com.app.test/com.app.test.activities.LeavingPageActivity}
11-22 13:31:36.339 24319-24381/com.app.test D/MonitoringInstr: execStartActivity(context, ibinder, ibinder, activity, intent, int, bundle
11-22 13:31:36.379 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: PRE_ON_CREATE
11-22 13:31:36.389 24319-24319/com.app.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-22 13:31:36.559 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: CREATED
11-22 13:31:36.559 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: STARTED
11-22 13:31:36.559 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: RESUMED
11-22 13:31:36.569 24319-24319/com.app.test D/Atlas: Validating map...
11-22 13:31:36.599 24319-24383/com.app.test I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: TEST SBA AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.00.006.014 + c1105519 ()
                                                                           OpenGL ES Shader Compiler Version: E031.25.03.00
                                                                           Build Date: 01/23/15 Fri
                                                                           Local Branch: 
                                                                           Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.00.006.014
                                                                           Local Patches: NONE
                                                                           Reconstruct Branch: NOTHING
11-22 13:31:36.699 24319-24337/com.app.test D/InputManagerEventInjectionStrategy: Creating injection strategy with input manager.
11-22 13:31:36.729 24319-24319/com.app.test I/ViewInteraction: Performing 'single click' action on view with id: com.app.test:id/leaveApplicationButton
11-22 13:31:37.000 24319-24319/com.app.test I/ViewInteraction: Checking 'android.support.test.espresso.intent.Intents$2@148c4653' assertion on view is a root view.
11-22 13:31:37.040 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: PAUSED
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: failed: testLeavePageClick(com.app.test.LeavingPageActivityTest)
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: ----- begin exception -----
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

                                                                           IntentMatcher: (has action: is "android.intent.action.VIEW" and has data: is <http://www.google.com>)

                                                                           Matched intents:[]

                                                                           Recorded intents:[]
                                                                               at dalvik.system.VMStack.getThreadStackTrace(Native Method)
                                                                               at java.lang.Thread.getStackTrace(Thread.java:580)
                                                                               at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:90)
                                                                               at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
                                                                               at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
                                                                               at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:291)
                                                                               at android.support.test.espresso.intent.Intents.intended(Intents.java:185)
                                                                               at android.support.test.espresso.intent.Intents.intended(Intents.java:167)
                                                                               at com.app.test.LeavingPageActivityTest.testLeavePageClick(LeavingPageActivityTest.java:74)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
                                                                               at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
                                                                               at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
                                                                               at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
                                                                               at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
                                                                               at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
                                                                               at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
                                                                               at org.junit.rules.RunRules.evaluate(RunRules.java:20)
                                                                               at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
                                                                               at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
                                                                               at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
                                                                               at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                                               at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                                               at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                               at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                                               at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                                               at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                               at org.junit.runners.Suite.runChild(Suite.java:128)
                                                                               at org.junit.runners.Suite.runChild(Suite.java:27)
                                                                               at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                                               at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                                               at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                                               at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                                               at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                                               at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                                               at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                                                                               at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                                                                               at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
                                                                               at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
                                                                               at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1976)
                                                                            Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

                                                                           IntentMatcher: (has action: is "android.intent.action.VIEW" and has data: is <http://www.google.com>)

                                                                           Matched intents:[]

                                                                           Recorded intents:[]
                                                                               at junit.framework.Assert.fail(Assert.java:50)
                                                                               at android.support.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:85)
                                                                               at android.support.test.espresso.intent.Intents.internalIntended(Intents.java:280)
                                                                               at android.support.test.espresso.intent.Intents$2.check(Intents.java:188)
                                                                               at android.support.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:415)
                                                                            at android.su
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: ----- end exception -----
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: finished: testLeavePageClick(com.app.test.LeavingPageActivityTest)

共有1个答案

萧展鹏
2023-03-14

这是不理想的,我认为在测试中使用睡眠是失败的,但如果必须这样做,可以在单击之前添加睡眠,给模拟器时间来解决。注意:我只会在使用Firebase测试云时看到问题中描述的行为。所有本地模拟器都没有出现此问题。

在单击按钮之前添加睡眠允许我在firebase测试云上总是失败的测试通过:

        try {
        android.support.test.espresso.intent.Intents.init();
        ...

        scrollView.scrollTo(0, viewToScrollTo.getTop());
        sleep(1000);
        onView(withId(R.id.view_quote_button)).check(matches(isDisplayed()));
        onView(withId(R.id.view_quote_button)).perform(click());

        android.support.test.espresso.intent.Intents.intended(allOf(
                hasAdultsQuoteEntry(adultCount),
                hasChildrenEntry(childrenCount)));

        ...
 类似资料:
  • 我有一些 在量角器中,我们搜索并找到元素,检查文本是否符合我们的期望,然后对该元素调用。测试在Chrome中运行良好,但在IE中就好像没有点击发生一样。破坏了测试。 IE 11是否支持点击

  • 问题内容: 我最近刚刚将我的Selenium IDE代码导出到了Selenium Web驱动程序。我发现很多在IDE中可用的命令要么无法运行,要么seleniumWeb驱动程序声称根本不支持。到目前为止,我一次只解决了一个问题,这并不理想。 目前,我正在研究为什么以前在Selenium IDE中无法使用Web驱动程序单击按钮的原因。我的浏览器是FF 13,我的操作系统是Ubuntu。 代码段 我以

  • 问题内容: 我正在使用http://datatables.net/ 我在onclick事件上触发ajax调用,下面是ajax调用代码: 我试图运行一个onclick事件,该事件通常在第一页上正常工作,但是一旦我转到第2页(或其他任何页面),它就会停止工作。 我正在使用jquery-1.10.2.min.js和1.9.4版本的数据表 问题答案: 因为事件仅附加到现有元素。 您应该将其更改为: 在jQ

  • 问题内容: 我有以下代码的视图: 目标是无需使用href即可过渡到新状态。不幸的是,上面的代码不会触发。 我还尝试为此视图在控制器中包括$ state: 但是仍然没有。transictionTo也似乎不起作用。 有人对如何进行这项工作有任何想法吗? 编辑:我只能通过分配使其工作: 在我的控制器内。这看起来很丑。如果没有将$ state分配给作用域,是否真的没有其他方法可以访问$ state? 问题

  • 问题内容: 我有一个像这样的控制器: 当我尝试使用ng-click调用upload_variant_image函数时,它仅在绑定到静态DOM时才有效(当DOM加载时),我有这样的链接: 但是此元素是在DOM加载后动态添加的,因此ng-click不起作用。 但是当我在Rails中像这样更新DOM时,它不起作用: 问题答案: 我警告您,如果您通过Angular外部方法来操纵DOM,则可能不会完全接受A

  • 问题内容: 这里有角的菜鸟。我正在创建一个指令以递归显示问题树和子问题树。我正在模板中使用链接,该链接在范围内调用函数。由于某种原因,它不会调用该方法。 这是代码和小提琴http://jsfiddle.net/madhums/n9KNv/ HTML: Javascript: 任何帮助理解这一点将不胜感激。 问题答案: 您遇到范围问题。由于您在指令中使用隔离范围,因此它不再有权访问具有的控制器范围。