This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll write our first UI test using Espresso!
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
It's about time we finally got
our feet wet with Espresso.
0:00
Inside our editTextUpdatesTextView method,
0:03
we've already taken care of
the given string variable.
0:06
So all that's left from the arrange
step is to get the given string
0:09
into the EditText.
0:13
On a new line,
let's type onView() with the parentheses.
0:15
And then use Alt+Enter
to import the method.
0:21
Next, inside the parentheses,
let's add withId.
0:25
Then in parentheses R.id.editText,
which is the id of our edit text.
0:30
And let's use Alt+Enter to
import with id as well.
0:38
Then at the end,
let's add .perform typeText,
0:45
pass in our given string and
0:52
then use Alt + Enter to import typeText,
and there we go.
0:56
Now when we run this test,
1:05
it will type the text of our
given string into our edit text.
1:07
Or put another way,
on the view with the id of
1:12
R.id.editText we'll perform an action
of typing the given string.
1:16
Now before we move on to the act section,
1:24
let's take a minute to talk
about Espresso syntax.
1:26
This is the basic structure for performing
an action on a view using Espresso.
1:30
It starts with a call to onView where
we provide a view matcher, like withId.
1:35
To locate the view on the screen, then
once we've used a view matcher to locate
1:40
the view, we then call the perform method
and pass in a view action like typeText.
1:46
And if instead of performing an action we
wanted to assert something about the view,
1:53
we would use the check method instead.
1:57
And then, instead of using a view action,
2:01
we would use a view assertion
like does not exist.
2:04
As you can imagine, there are more
view matchers, view actions and
2:08
view assertions than just those three.
2:12
But since it's hard to remember them all,
2:15
Espresso provides an excellent cheat
sheet to make this easier for us.
2:17
There's a link in
the teacher's notes below.
2:21
Now for the Act section, we need to tell
our edit text that we're finished editing.
2:25
Let's copy this line.
2:29
And paste it into the Act section.
2:34
Then instead of using the typeText
view action, let's use.
2:36
PressImeActionButton.
2:45
Ime stands for input method editor and
2:50
it represents a user control for
entering text, like our editText.
2:53
So this method is basically
just pressEditTextActionButton.
2:59
All right now for the assert step.
3:06
Asserting with Espresso is done by using
the check method on the selected view.
3:09
Just like how we use the perform
method to perform an action,
3:14
we use the check method
to make an assertion.
3:18
Let's first select our text
view by typing onView and
3:21
passing in a withId view
matcher with a parameter
3:27
of R.id.textView,
then let's call the check method.
3:32
And for our view assertion,
let's pass in matches and
3:38
use Alt+Enter to import it from Espresso.
3:44
Make sure you're picking
the one from Espresso.
3:52
Finally we need to provide
a view matcher as the parameter.
3:56
Let's type, withText and
pass in our givenString.
3:59
And Alt + Enter to import with text.
4:10
That should do it.
4:13
We're checking that the view
with the id of text view
4:15
matches the view with
the text of given string.
4:19
All right, I think it's about
time we run our first UI test.
4:25
But before we hit the run button,
4:29
there's a little bit of setup we need
to do on our device or emulator.
4:31
If you'll be testing on a virtual device
like me, and it's not currently up and
4:35
running, open up the AVD Manager and
4:39
hit the play button to start
your favorite virtual device.
4:43
Once you've got your device, we just need
to go into the developer settings and
4:48
turn off animations.
4:52
We don't really want to wait for
animations when we're running our tests.
4:54
Also, when we're running a UI test,
the clicks and
4:58
other actions happen pretty quickly.
5:01
If an animation takes a little longer for
any reason, the test runner could
5:04
send the click before it's supposed to,
and we'd fail our test.
5:08
So it's best to just turn off animations.
5:12
Let's go to Settings, Developer options,
5:16
which if it's not available here,
5:22
you need to click on
the build number seven times,
5:26
then back in developer options
scroll down to the drawing section,
5:30
and then let's change each of these from
animation scale 1x to animation off.
5:37
Nice.
5:51
We're finally ready to
run our first UI test.
5:52
Lett's right click on our class and
choose Run, MainActivityUI test.
5:55
Then let's pick the connective device and
make sure we check the use same
6:04
selection box, then let's hit OK and
switch our focus to the device.
6:09
Did you see it?
6:23
Quick, right?
6:25
It typed in the text,
hit the action button, and
6:26
then ran our assertion,
all in under three seconds.
6:29
So if we take out the Act section,
it should fail right?
6:34
Let's find out.
6:38
Yup.
6:51
It still types the text but
it doesn't submit it so
6:52
out TextView is never updated.
6:55
Now let's bring back the Act section.
6:59
And run it one more time
to get back to green
7:03
One test down, two tests remain.
7:15
In the next video, we'll see how to
use Espresso to test our spinner.
7:17
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up