小编eli*_*lin的帖子

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

我正在使用 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);
}
Run Code Online (Sandbox Code Playgroud)

布局:

<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"/>
Run Code Online (Sandbox Code Playgroud)

测试:

@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 …
Run Code Online (Sandbox Code Playgroud)

android android-espresso

5
推荐指数
1
解决办法
2606
查看次数

标签 统计

android ×1

android-espresso ×1