【问题标题】:How do I import espresso withID on android studio?如何在 android studio 上导入 espresso withID?
【发布时间】:2018-09-06 11:53:08
【问题描述】:

我正在尝试使用 espresso 为我的应用程序编写单元测试,但每当我尝试使用 withId 时,都找不到它。我该如何解决这个问题?

onView 工作正常,但是当我尝试 onView(withId) 时它不起作用。

这是我的代码:

package com.example.webowner.authenticate;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.rule.ActivityTestRule;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.;


import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.*;

public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<MainActivity>(MainActivity.class);

    @Test
    public void testLoginButton_shouldBeDisplayed()
    {
        //
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }
}

【问题讨论】:

  • 把你的完整代码和任何日志或崩溃(如果存在)。

标签: android android-studio android-espresso


【解决方案1】:

对于 AndroidX,签出包ViewMatchers

import androidx.test.espresso.matcher.ViewMatchers.withId

https://developer.android.com/reference/androidx/test/espresso/matcher/ViewMatchers

【讨论】:

    【解决方案2】:

    您应该导入以下内容:

    import static android.support.test.espresso.matcher.ViewMatchers.withId;
    

    希望这行得通!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      相关资源
      最近更新 更多