Understanding the Robot Pattern for UI tests. (Part 1)

Bego Ros
3 min readNov 17, 2020

Learn the basics of the Robot Pattern for UI tests.

What is the Robot Pattern?

The Robot Pattern is a UI testing pattern that was developed initially by Jake Wharton in Kotlin but it can be adapted to other languages. The main idea behind this pattern is the separation of concerns — the what from the how.

  • The Robot Pattern is beneficial as it aims to separate the tests from the main logic controlling the views. I will go into more details about the robots on the architecture section but essentially, this separation is possible because of robots. The tests represent the what and the robots represent the how.
  • The Robot Pattern is not exclusive to Android. It can easily be implemented for iOS on XCUIT too!
iOS & Android aligning differences

Architecture

Architectural Diagram and its components

Understanding the Components

Robots

  • Robots are basically classes that contain functions that perform actions or assertions on views. The test classes have their own robots following the single responsibility principle.
  • There is a nested Assert Robot in every robot class. The main responsibility of this nested inner class is to make assertions on the screen. This allows for a clean distinction between the actions and the assertions.
  • The BaseRobot is a class that provides all the logic for the Espresso. Its functionality is generic and provides the UI automation functions to be used in the project. All screen specific robots extend from base robot so they can use its functionality. For example, the Base Robot holds the logic for actions such has clicking buttons, asserting views, scrolling, filling text, scrolling functionality etc.

Test Classes

  • The test classes contain the specific tests for their screens. They use their specifically assigned robots to call the functions for actions and to verify the test results.
  • You can add a Base Test class to abstract the launching logic and the most common scenarios.
  • You can optionally add the Gherkin syntax to the test scenarios for clarification on the test steps.

Platforms

  • The Robot Pattern can be implemented on Android UI tests by using Espresso. I will demonstrate how to scale this implementation on my next article using one of my apps as an easy to follow example!
  • Although the Robot Pattern was initially developed for Android, it is not platform specific. I will demonstrate an example of it being implemented on an iOS app using XCUIT on my next article.

In the next articles we will discuss…

  • Robot Pattern Implementation for Android.
  • Robot Pattern Implementation for iOS.

Please feel free to share any questions or your experience when implementing UI tests below. I would love to read about it :)

--

--