How do I see by ID in my Espresso test?
I don’t understand how to get View from screen in my Espresso test using findViewById
.
I
will use espresso autotest to go to a screen in my application, and then I want to check if the screen is correct. To do this, I need to get the toolbar from the screen and check some of his properties.
First, I tried to do this using the ViewInteractions
object, but I could convert it to a View, but I couldn’t access the object properties.
I
want to get View directly using findViewById
, but I know how to use it properly.
public void goToMyOrders (){
BottomTabBar bottomTabBar = new BottomTabBar();
bottomTabBar.profileButton.perform(click());
myOrderButton.perform(click());
ViewInteraction toolbarView = onView(allOf(withId(R.id.custom_font_toolbar)));
toolbarView.getContext();
toolbarView.check(matches(withText("Мои заказы")));
}
Solution
No, I think you’re a bit wrong about the idea of testing. First of all, the Toolbar does not extend/inherit the TextView, so it cannot have text, but you can place a
TextView
inside the Toolbar
Create a new sample application and put this layout into your activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=". MainActivity">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintTop_toTopOf="parent"
android:background="@color/colorAccent"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/toolbar"
app:layout_constraintBottom_toBottomOf="@id/toolbar"
app:layout_constraintLeft_toLeftOf="@id/toolbar"
app:layout_constraintRight_toRightOf="@id/toolbar"
android:text="SomeTextBeforeChange"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="Change text after click"/>
In your MainActivity
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
final TextView textView = findViewById(R.id.textview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("Changed text");
}
});
}
As you can see, this might be a similar View
to yours – Toolbar
, TextView, and Button
that change the text of the TextView
.
Let’s now use Espresso
for automated testing
Create a new class in your androidTest subfolder and name it e.g.
MainActivityTest
Define an
ActivityTestRule
read the information in Android docs
You may also need thoseGradle
dependencies:androidTestImplementation 'com.android.support.test:runner:1.0.2'<br/>
androidTestImplementation 'com.android.support.test:rules:1.0.2'<br/>
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'Rules:
@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>
(MainActivity.class);Then define a public method with a return type of void, annotated as
@Test
.
In this method you can define yourTextView
callEspresso.onView(ViewMatchers.withId(your.textview.id));
To check if it contains the specified text, you need to call ‘textview.check(ViewAssertions.matches(ViewMatchers.withText(“expected text”))));
I
won’t explain exactly what these methods do, as their names speak for themselves, but if you’re having some trouble understanding them, you can find plenty of resources on StackOverflow
. For my code above, I wrote the following simple test, so you can analyze it if you want:
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
@Test
public void checkIfButtonChangesText(){
TextView
ViewInteraction textView = Espresso.onView(ViewMatchers.withId(R.id.textview));
Button
ViewInteraction button = Espresso.onView(ViewMatchers.withId(R.id.button));
check if TextView contains text: "SomeTextBeforeChange"
textView.check(ViewAssertions.matches(ViewMatchers.withText("SomeTextBeforeChange")));
analogically with Button, text: "Change text after click"
button.check(ViewAssertions.matches(ViewMatchers.withText("Change text after click")));
perform button click:
button.perform(ViewActions.click());
Check if text has been changed to "Changed text"
textView.check(ViewAssertions.matches(ViewMatchers.withText("Changed text")));
}
}