Hello, espresso! Recipe 1 Find adjacent elements 🎭

Gaurav Singh
3 min readAug 11, 2022

Learning outcome

In this post, we will understand how to find an element using its adjacent elements property and hasSibling matcher

Overview

In the last part of our series on espresso Hello, espresso! Part 5 Automating WebViews 🕸️🌎, We understood how to automate web views with espresso. Please go ahead and have a read in case you missed it.

By now, You should have a solid idea on how to automate android UI tests with espresso and be somewhat familiar with its core API

In the next few posts we’ll explore at how to accomplish common scenarios in android apps or recipes

Let’s go.

How to find an element with its sibling

Most modern mobile apps are designed in a modular fashion with reusable components wherein the same component is duplicated in a list like control.

In some cases there will be multiple elements with similar ID and finding them using R.id.x would not work since espresso does not know which specific element we are trying to look up.

In such cases, we can find elements using some unique property of its adjacent (or sibling) element.

Let’s take a look at an example to understand how this works in action

Understanding the App under test

For this test we’ll use APIDemos app which has been forked from the popular Appium project (which itself was forked from a Google repo).

This app showcases lot of Android components as examples and as such is quite a decent playground app.

Say we want to automate below scenario:

NOTE: If you are manually exploring the app using Android emulator within android studio you can arrive at this screen by tapping on Accessibility > Accessibility Node Querying

If you try to inspect a unique id for this checkbox, you’ll find there are multiple such checkboxes within @android:id/list and we can't use that attribute

However each checkbox has a TextView control that has a unique text and we can use this property to identify the element

Let’s write our test

We launch our activity using:

Let’s use the property that the sibling element has a unique text to find the desired element

We start by saying:

  • We want to find a view that matches all the the properties by wrapping the conditions in allOf
  • Next we say we want to find an element with id withId(R.id.tasklist_finished) (i.e. our checkbox)
  • However, we only want the element that has a sibling with specific text by specifying hasSibling(withText(taskText)) that useshasSibling View matcher

Finally once we have found the element, we want to click on it and check that the state changes to checked by specifying:

Below is the complete test:

Resources 📘

Before you go

Thanks for spending your time reading this post. 🙏

💡 If you found this insightful or helpful 👍🏼, then please take a few minutes and share it with your friends and colleagues or on your social media accounts. Every share helps this grow and “sharing is caring” 🫂 isn’t it?

Before you go 🙌🏼. Did you know?

I go deep into Test Automation and Software Testing in both these platforms as well and you may find subscribing valuable to your learning journey

Have Questions 🤔 or Feedback 😉?

Please let me know in the comments (I promise I read them all ✌🏼) or you can ping me over Twitter or LinkedIn at @automationhacks

Until next time, Happy Testing 🕵🏻 and Learning! 🌱

Originally published at https://automationhacks.io on August 11, 2022.

--

--

Gaurav Singh

Software Engineer passionate about Software Testing, Test Automation {Tooling, Frameworks, Infrastructure}, and scaling teams.