Heyy, I am writing tests with Espresso for my android app and I am having 2 cases, where Espresso won't find the views. Here, are the xmls( so you can see the hierarchy and the test classes ):
<?xml version="1.0" encoding="utf-8"?>
<myapp.ui.SafeAreaConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/login_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_color_3_oranges_linear_270"
tools:context=".fragments.onBoarding.EmailSignInFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/logo_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/welcome_figure"
android:layout_width="172dp"
android:layout_height="180dp"
android:background="@drawable/ic_logo_app_transparent"
android:layoutDirection="ltr"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:fontFamily="@font/arial"
android:lineSpacingExtra="40dp"
android:text="@string/app_name"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/welcome_figure" />
<TextView
android:id="@+id/powered_by"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="42dp"
android:layout_marginTop="15dp"
android:fontFamily="@font/arial"
android:text="@string/powered_by"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="11sp"
app:layout_constraintEnd_toStartOf="@+id/logo"
app:layout_constraintStart_toStartOf="@+id/app_name"
app:layout_constraintTop_toBottomOf="@+id/app_name" />
<ImageView
android:id="@+id/logo"
android:layout_width="31dp"
android:layout_height="31dp"
android:layout_marginStart="3dp"
android:layout_marginTop="9dp"
android:layout_marginEnd="37dp"
android:background="@drawable/logo_30x30"
android:layoutDirection="ltr"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="@+id/app_name"
app:layout_constraintStart_toEndOf="@+id/powered_by"
app:layout_constraintTop_toBottomOf="@+id/app_name"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
<myapp.ui.translatable.TranslatableTextView
android:id="@+id/sign_in_main_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="80dp"
android:fontFamily="@font/ceragr_medium"
android:lineSpacingExtra="5dp"
android:text="@string/email_tip"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/logo_banner" />
<myapp.ui.translatable.TranslatableTextView
android:id="@+id/email_input_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:fontFamily="@font/arial"
android:text="@string/email"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="@id/sign_in_main_text"
app:layout_constraintStart_toStartOf="@id/sign_in_main_text"
app:layout_constraintTop_toBottomOf="@id/sign_in_main_text" />
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/email_input_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="@color/light_blue_main"
android:fontFamily="@font/arial"
android:hint="[email protected]"
android:inputType="textEmailAddress"
android:textColor="@color/white"
android:textColorHint="@color/light_grey_secondary"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="@id/sign_in_main_text"
app:layout_constraintStart_toStartOf="@id/sign_in_main_text"
app:layout_constraintTop_toBottomOf="@id/email_input_label"
tools:ignore="HardcodedText" />
</myapp.ui.SafeAreaConstraintLayout>
```
and then the test:
```
@LargeTest
@HiltAndroidTest
@ExperimentalCoroutinesApi
class FragmentWithArgsTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
private lateinit var navController: TestNavHostController
@Before
fun setUp() {
// Populate @Inject fields in test class
hiltRule.inject()
// Create a TestNavHostController
navController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)
}
@Test
fun latestValidateEmail() { //did not pass
Thread.sleep(7000)
val args = Bundle().apply {
putString("signInLink", null)
}
// Create a graphical welcomeFragmentScenario for the EmailSignInFragment
launchFragmentInHiltContainerFirst<EmailSignInFragment>(args) {
// Set the graph on the TestNavHostController
navController.setGraph(R.navigation.onboarding_nav_graph)
// Make the NavController available via the findNavController() APIs
Navigation.setViewNavController(this.requireView(), navController)
}
val editText = Espresso.onView(
Matchers.allOf(
withId(R.id.email_input_text),
withHint("[email protected]"),
withInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS),
withParent(
Matchers.allOf(
withId(R.id.login_layout),
withParent(withId(R.id.nav_host_fragment))
)
),
isDescendantOfA(withId(R.id.login_layout)),
isDisplayed()
)
)
editText
.check(matches(isDisplayed()))
.perform(
ViewActions.typeText("[email protected]"),
ViewActions.closeSoftKeyboard()
)
.check(matches(withInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS)))
}
}
and then the other xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_orange_main">
<include
android:id="@+id/background_for_welcome"
layout="@layout/background_for_welcome"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.flexbox.FlexboxLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:alignItems="center"
app:flexDirection="column"
app:justifyContent="space_between"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/logo_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/welcome_figure"
android:layout_width="172dp"
android:layout_height="170dp"
android:background="@drawable/ic_logo_app_transparent"
android:layoutDirection="ltr"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:fontFamily="@font/arial"
android:lineSpacingExtra="40dp"
android:text="@string/app_name"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/welcome_figure" />
<TextView
android:id="@+id/powered_by"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="42dp"
android:layout_marginTop="15dp"
android:fontFamily="@font/arial"
android:text="@string/powered_by"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="11sp"
app:layout_constraintEnd_toStartOf="@+id/logo"
app:layout_constraintStart_toStartOf="@+id/app_name"
app:layout_constraintTop_toBottomOf="@+id/app_name" />
<ImageView
android:id="@+id/logo"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="3dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="37dp"
android:background="@drawable/eagme_logo_20x20"
android:layoutDirection="ltr"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="@+id/app_name"
app:layout_constraintStart_toEndOf="@+id/powered_by"
app:layout_constraintTop_toBottomOf="@+id/app_name"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.flexbox.FlexboxLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:alignItems="center"
app:flexDirection="column"
app:justifyContent="flex_start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logo_banner">
<TextView
android:id="@+id/connect_with_text"
android:layout_width="136dp"
android:layout_height="28dp"
android:layout_marginBottom="20dp"
android:fontFamily="@font/arial"
android:text="@string/connect_with"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="21sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/signin_google_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<myapp.ui.SigninThumbnailButton
android:id="@+id/signin_google_button"
android:layout_width="230dp"
android:layout_height="50dp"
app:signin_button_background="@drawable/signin_google_background"
app:signin_button_icon="@drawable/google_icon"
app:signin_button_text="@string/google"
app:signin_button_text_color="@color/not_so_black" />
<myapp.ui.SigninThumbnailButton
android:id="@+id/signin_facebook_button"
android:layout_width="230dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
app:signin_button_background="@drawable/signin_facebook_background"
app:signin_button_icon="@drawable/facebook_icon"
app:signin_button_text="@string/facebook"
app:signin_button_text_color="@color/white" />
<myapp.ui.SigninThumbnailButton
android:id="@+id/signin_email_button"
android:layout_width="230dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
app:signin_button_background="@drawable/signin_email_background"
app:signin_button_icon="@drawable/envelope_icon"
app:signin_button_text="@string/email"
app:signin_button_text_color="@color/white" />
<TextView
android:id="@+id/btn_signin_skip"
style="@style/ButtonSignInText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:fontFamily="@font/arial"
android:text="@string/skip"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="17sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/signin_email_button" />
</com.google.android.flexbox.FlexboxLayout>
<TextView
android:id="@+id/footer_text"
style="@style/TextFooter"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginBottom="39dp"
android:text="@string/legal_agreement"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</com.google.android.flexbox.FlexboxLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
and then the test class:
@LargeTest
@HiltAndroidTest
@ExperimentalCoroutinesApi
class SignInFragmentTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
private lateinit var navController: TestNavHostController
private lateinit var emailSignInButton: ViewInteraction
private lateinit var googleSignInButton: ViewInteraction
private lateinit var facebookSignInButton: ViewInteraction
private lateinit var skipButton: ViewInteraction
@Before
fun setUp() {
// Populate @Inject fields in test class
hiltRule.inject()
// Create a TestNavHostController
navController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)
val args = Bundle().apply {
Pair<String, Any>("mode", AuthenticationManager.AuthenticationModes.SIGNIN)
}
// Create a graphical welcomeFragmentScenario for the WelcomeFragment
launchFragmentInHiltContainerFirst<SignInFragment>(args) {
// Set the graph on the TestNavHostController
navController.setGraph(R.navigation.onboarding_nav_graph)
// Make the NavController available via the findNavController() APIs
Navigation.setViewNavController(this.requireView(), navController)
}
// Intents.init()
}
// @After
// fun tearDown() {
// Intents.release()
// }
@Test
fun testSignInSkip() { //passed
Thread.sleep(7000)
skipButton = Espresso.onView(
Matchers.allOf(
ViewMatchers.withId(R.id.connect_button),
ViewMatchers.withText(R.string.connect),
isDisplayed()
)
)
skipButton.check(matches(isDisplayed()))
// Verify that performing a click changes the view to home page
skipButton
.perform(click())
//check if HomeActivity/ HomeFragment
}
@Test
fun testGoogleSignIn() { //did not pass
Thread.sleep(7000)
googleSignInButton =
Espresso.onView(
Matchers.allOf(
ViewMatchers.withId(R.id.signin_google_button),
ViewMatchers.withText(R.string.google),
isDisplayed()
)
)
googleSignInButton.check(matches(isDisplayed()))
googleSignInButton.perform(click())
}
@Test
fun testFacebookSignIn() { //did not pass
Thread.sleep(7000)
facebookSignInButton = Espresso.onView(
Matchers.allOf(
ViewMatchers.withId(R.id.signin_facebook_button),
ViewMatchers.withParent(
ViewMatchers.withParent(
IsInstanceOf.instanceOf(
ViewGroup::class.java
)
)
),
isDisplayed()
)
)
facebookSignInButton.check(matches(isDisplayed()))
facebookSignInButton.perform(click())
}
@Test
fun testEmailSignIn() { //did not pass
Thread.sleep(7000)
emailSignInButton = onView(
Matchers.allOf(
ViewMatchers.withId(R.id.signin_email_button),
childAtPosition(
childAtPosition(
ViewMatchers.withClassName(Matchers.`is`("com.google.android.flexbox.FlexboxLayout")),
1
),
3
),
isDisplayed()
)
)
// Espresso.onView(
// Matchers.allOf(
// ViewMatchers.withId(R.id.signin_email_button),
// ViewMatchers.withParent(ViewMatchers.withParent(IsInstanceOf.instanceOf(ViewGroup::class.java))),
// isDisplayed()
// )
// )
// emailSignInButton.check(matches(isDisplayed()))
// Verify that performing a click changes the NavController’s state
emailSignInButton
.perform(click())
//from SignInFragment To EmailSignInFragment
MatcherAssert.assertThat(
navController.currentDestination?.id,
Matchers.equalTo(R.id.emailSignInFragment)
)
}
}
ALso an example of the stacktrace for the test : LatestValidateEmail , I am getting is :
androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (view.getId() is <2131296487/com.crowdpolicy.onext.igme:id/email_input_text> and an instance of android.widget.TextView and view.getHint() matching: is "[email protected]" and an instance of android.widget.EditText and editText.getInputType() is <32> and view.getParent() view.getId() is <2131296572> and is descendant of a view matching view.getId() is <2131296572> and (view has effective visibility <VISIBLE> and view.getGlobalVisibleRect() to return non-empty rectangle))
View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=720, height=1280, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=WM.LayoutParams{(0,0)(fillxfill) sim=#20 ty=1 fl=#85810100 fmt=-2 wanim=0x103045b needsMenuKey=2 clr=0x1}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@bc31964, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->ViewStub{id=16909220, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@b2677cd, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+-->FrameLayout{id=-1, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@46e8b93, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+--->FitWindowsLinearLayout{id=2131296312, res-name=action_bar_root, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@e73cace, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+---->ViewStubCompat{id=2131296327, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@86c4def, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+---->ContentFrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@285c5da, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+----->FrameLayout{id=-1, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@db360e8, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+------>FragmentContainerView{id=2131296636, res-name=nav_host_fragment, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@d8a65a6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+------->FragmentContainerView{id=2131296636, res-name=nav_host_fragment, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@1d1a394, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+-------->ConstraintLayout{id=-1, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@965d12c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=7}
|
+--------->ConstraintLayout{id=2131296358, res-name=background_for_welcome, visibility=VISIBLE, width=720, height=1188, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@44b598a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
I used Espresso recorder test to get the views, but even with this, my tests are not successful ! Any idea how to fix it ? Any help is appreciated, because I have spent a lot of time on this with no result !