标签: android-testing

单击 Espresso 中具有相同 id 的多个视图之一

我的布局 (A) 多次包含另一个布局 (B)。布局 B 包含一个 id 的按钮R.id.my_button。因此,布局 A 包含许多具有相同 id 的按钮。

如何使用 espresso 测试点击这些按钮中的任何一个?

onView(withId(R.id.my_button)).perform(click());在这种情况下并没有真正做任何事情。

android android-testing android-espresso

2
推荐指数
1
解决办法
3418
查看次数

如何使用 Espresso 检查 BottomNavigationItemView 的断言?

BottomNavigationItemView实现ItemView具有该setChecked()方法的接口。

我试图用 Espresso 断言检查了一个 itemView 但我得到了同样的错误,无论我的期望值是什么,isChecked()或者isNotChecked().

我的测试:

ViewInteraction buttonHome = onView(
    allOf(withId(R.id.bottomHome),
          childAtPosition(
              childAtPosition(
                  withId(R.id.bottom_navigation),
                  0),
              0),
          isDisplayed()));
    buttonHome.check(matches(isNotChecked()));
Run Code Online (Sandbox Code Playgroud)

错误信息

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with checkbox state: is <true>' doesn't match the selected view.
Expected: with checkbox state: is <true>
Got: "BottomNavigationItemView{id=2131493015, res-name=bottomHome, visibility=VISIBLE, width=360, height=168, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}"
Run Code Online (Sandbox Code Playgroud)

我怎么能断言 aBottomNavigationItemView是当前选定的项目BottomNavigationView

android android-testing android-espresso bottomnavigationview

2
推荐指数
2
解决办法
2993
查看次数

为什么 Gradle 运行测试两次?

如果我以通常的方式运行测试,一切都很好。问题是当我在 gradle 的帮助下运行测试时,它们被执行两次。我使用 grandle 来分开执行小型和中型测试。

这是gradle控制台的数据:

Executing tasks: [connectedAndroidTest]

Configuration on demand is an incubating feature.
Incremental java compilation is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preProductionDebugBuild UP-TO-DATE
:app:checkProductionDebugManifest
:app:preProductionDebugAndroidTestBuild UP-TO-DATE
:app:preProductionReleaseBuild UP-TO-DATE
:app:preUiTestDebugAndroidTestBuild UP-TO-DATE
:app:preUiTestDebugBuild UP-TO-DATE
:app:preUiTestReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2421Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72421Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2421Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUi2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUtils2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportFragment2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportMediaCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2421Library UP-TO-DATE
:app:preparePlDroidsonroidsGifAndroidGifDrawable123Library UP-TO-DATE
:app:prepareProductionDebugDependencies
:app:compileProductionDebugAidl UP-TO-DATE
:app:compileProductionDebugRenderscript UP-TO-DATE
:app:generateProductionDebugBuildConfig UP-TO-DATE
:app:generateProductionDebugResValues UP-TO-DATE
:app:generateProductionDebugResources UP-TO-DATE
:app:mergeProductionDebugResources UP-TO-DATE
:app:processProductionDebugManifest UP-TO-DATE
:app:processProductionDebugResources UP-TO-DATE
:app:generateProductionDebugSources …
Run Code Online (Sandbox Code Playgroud)

android gradle android-testing android-gradle-plugin

2
推荐指数
1
解决办法
1439
查看次数

Espresso - 单击带有 app:showAsAction="ifRoom" 的菜单项

我想使用 Espresso 单击以下菜单项:

<item
    android:id="@+id/action_save"
    android:icon="@drawable/vector_image_save"
    android:orderInCategory="4"
    android:title="@string/menu_action_save"
    app:showAsAction="ifRoom"/>
Run Code Online (Sandbox Code Playgroud)

由于ifRoom,在某些设备中,菜单在操作栏中显示为图标,而在较小的设备中,它显示在“更多选项”下。

我可以使用以下代码点击操作栏中的“保存”图标: onView(withId(R.id.action_save)).perform(click());

如果保存在“更多选项”下,我可以使用以下代码点击保存: openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext()); onView(withText(R.string.menu_action_save)).perform(click());

我想要一种在两种情况下都适用的测试方法。

android android-testing android-espresso

2
推荐指数
1
解决办法
1800
查看次数

滑动直到元素在 Android Espresso 中可见

我有一个带有数据输入表单的长垂直屏幕。我需要向上滑动(向下滚动)此屏幕,直到屏幕上显示表单的字段之一。我尝试在屏幕上方的元素上使用swipeUp()scrollTo() ViewActions,但这对我没有帮助,因为这些元素在滑动后隐藏在屏幕上。

testing android android-testing android-espresso

2
推荐指数
1
解决办法
5291
查看次数

Espresso 单击 Textview 内的特定子字符串

我有一个包含文本视图的布局,如下所示,它将在文本块中包含一些自动生成的链接。当用户单击链接时,会打开一个包含相关信息的窗口。

应用程序的屏幕截图

但是,我根本不知道如何让 Espresso 单击链接。尝试过很多类似的变体

matches(withText(containsString("45")))

但这要么不起作用,要么让浓缩咖啡只需单击文本框本身,这不会执行任何操作。

有什么办法可以做到这一点吗?在 Espresso 的限制范围内是否有可能?

android android-testing android-espresso

2
推荐指数
1
解决办法
3200
查看次数

kotlin + 改造 + 协程 + androidTest

如何androidTest为样品retrofit请求创建?

样本

data class TestDataClass(
    val id: String,
    val employee_name: String,
    val employee_salary: String,
    val employee_age: String,
    val profile_image: String)

enum class NetworkState { LOADING, ERROR, DONE }

private const val BASE_URL = "http://dummy.restapiexample.com/api/v1/"

private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()



private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .addCallAdapterFactory(CoroutineCallAdapterFactory())
    .baseUrl(BASE_URL)
    .build()

interface TestApiService {
    @GET("employees")
    fun getPropertiesAsync():
            Deferred<List<TestDataClass>>
}

object TestApi {
    val retrofitTest : TestApiService by lazy { retrofit.create(TestApiService::class.java) }
}
Run Code Online (Sandbox Code Playgroud)

android android-testing android-jetpack

2
推荐指数
1
解决办法
1035
查看次数

导航组件 - 模拟导航控制器图

我正在使用导航组件,并且正在尝试Fragment使用仪器测试来测试我的组件。该片段具有onViewCreated由扩展函数在方法中初始化的自定义工具栏。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    tbBlack.init()
}


fun androidx.appcompat.widget.Toolbar.init(
    menuId: Int? = null
) {
    title = ""
    menuId?.let {
        inflateMenu(it)
    }

    findNavController().let {
        it.graph.let { graph ->
            val configuration = AppBarConfiguration(graph)
            setupWithNavController(it, configuration)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的仪器测试中的场景初始化期间,由于null导航控制器上的图形导致测试崩溃。

导航控制器在测试中被模拟,以及如下图:

@RunWith(AndroidJUnit4::class)
class LoginFragmentTest {
    @Test
    fun testEmptyFields() {
        val mockNavController = mock(NavController::class.java)
        val mockGraph = mock(NavGraph::class.java)
        mockNavController.graph = mockGraph

        val scenario = launchFragmentInContainer(themeResId = R.style.AppTheme) {
            LoginFragment().also { fragment -> …
Run Code Online (Sandbox Code Playgroud)

android android-testing android-espresso android-architecture-navigation

2
推荐指数
1
解决办法
1044
查看次数

使用 Material Design 主题进行片段隔离测试

我有一个单一活动、多功能模块的设置,如下所示:

/app - 包含继承材料设计样式资源的主模块,以及活动 /menu - 仅包含片段和视图模型/其他模块的模块功能...

我试图在 Android 测试中单独运行我的 Fragment,这会导致 Material 组件(在本例中为 CardView)必须将 Material Theme 作为其样式的错误。这确实有道理,但我尝试过。

现在我得到这个错误

android.view.InflateException: Binary XML file line #21 in com.nikolam.menu.test:layout/menu_item: Binary XML file line #21 in com.nikolam.menu.test:layout/menu_item: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: android.view.InflateException: Binary XML file line #21 in com.nikolam.menu.test:layout/menu_item: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.nikolam.menu.ui.menu.adapter.MenuAdapter.onCreateViewHolder(MenuAdapter.kt:27)
at com.nikolam.menu.ui.menu.adapter.MenuAdapter.onCreateViewHolder(MenuAdapter.kt:16)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7266)
at …
Run Code Online (Sandbox Code Playgroud)

android android-fragments android-testing material-design

2
推荐指数
1
解决办法
755
查看次数

强制转换异常 - AndroidJUnitRunner 无法强制转换为 RoboMonitoringInstrumentation

致力于 Android Instrumented Test 来启动和测试此片段

在此输入图像描述

但我正在遇到这个令人讨厌的错误。

java.lang.ClassCastException: androidx.test.runner.AndroidJUnitRunner cannot be cast to org.robolectric.android.fakes.RoboMonitoringInstrumentation
at org.robolectric.android.internal.LocalActivityInvoker.getInstrumentation(LocalActivityInvoker.java:153)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:226)
at androidx.fragment.app.testing.FragmentScenario$Companion.internalLaunch$fragment_testing_release(FragmentScenario.kt:588)
at androidx.fragment.app.testing.FragmentScenario$Companion.launchInContainer(FragmentScenario.kt:559)
at com.example.android.architecture.blueprints.todoapp.taskdetail.TaskDetailFragmentTest.activeTasks_displayedInUi(TaskDetailFragmentTest.kt:35)Tests ran to completion.
Run Code Online (Sandbox Code Playgroud)

这些是我正在使用的依赖项

在此输入图像描述

一直在网上寻找答案,但我没有找到任何有用的发现。

我可能会错过什么?

android android-testing

2
推荐指数
1
解决办法
1077
查看次数