In this presentation by a developer at Square: “Software Quality/Automation Testing”, he discussed how they take advantage of Robots to make it easier to update tests whenever UI elements change, as well as a few additional benefits. This is a very useful abstraction technique for when you’re writing large numbers of tests in Espresso. So let’s look at an example of using a Screen Robot to test a simple login flow.
Before Using a Screen Robot
So the above code is how you would typically make use of Espresso, where you have multiple calls to onView() and reference the resource ids directly in each test. Two main drawbacks of this approach is that 1) if you need to change the ids in the future you have to update multiple tests, and 2) it’s not very clear from first glance what you’re attempting to accomplish in a given test due to the Espresso calls. So now let’s look at the same example, this time using Screen Robots.
Notice how the test method is more succinct and more easily conveys what we are trying to do, namely login with a given username and password and then make sure that it was a success. This includes the code for our LoginScreenRobot. This allows us to have a thin abstraction layer over the Espresso calls by extending our base ScreenRobot class. Hope you found this short testing tip helpful!