Espresso Test Support¶
This page describes how to incorporate the Literal TestRule in Espresso1. The TestRule makes it very simple to use Literal in conjunction with your Espresso tests so that app maps of your user flows are automatically created, for any build.
Integration¶
Important note
Prior to adding the TestRule, ensure the SDK has been fully set up by confirming that session recording produces a board for your app.
It’s recommended to use the TestRule for tests which encompass a user flow of screens. It’s less useful for simple tests on a single screen.
In the app-level build.gradle
or build.kts
file, add a reference to the test support library:
dependencies {
debugImplementation "ai.literal:literator:1.9.2"
androidTestImplementation "ai.literal:literator-test-support:1.9.2"
}
dependencies {
debugImplementation("ai.literal:literator:1.9.2")
androidTestImplementation("ai.literal:literator-test-support:1.9.2")
}
TestRule¶
Literal's TestRule follows the same convention as any other test rule such as the common ActivityTestRule
or ActivityTestScenario
.
Simply add a public class member of type LiteratorTestRule
. With the TestRule enabled, Literal will produce an app board with all screens that were seen during the test. A simple example is provided below:
import ai.literal.test.client.LiteratorTestRule
@RunWith(AndroidJUnit4::class)
class ExampleTest : BaseTest() {
@Rule
@JvmField
var literatorRecordingRule = LiteratorTestRule()
@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(MainActivity::class.java)
@Test
fun open_settings() {
onView(ViewMatchers.withContentDescription("Open navigation drawer")).perform(click())
onView(ViewMatchers.withText("Settings")).perform(click())
}
}
import ai.literal.test.client.LiteratorTestRule;
@RunWith(AndroidJUnit4.class)
public class ExampleTestJava extends BaseTest {
@Rule
public LiteratorTestRule literatorTestRule = new LiteratorTestRule();
@Rule
public ActivityTestRule activityTestRule = new ActivityTestRule(MainActivity.class);
@Test
public void open_settings() {
onView(ViewMatchers.withContentDescription("Open navigation drawer")).perform(click());
onView(ViewMatchers.withText("Settings")).perform(click());
}
}
Note: For large test suites, it's advantageous to add the test rule to a base class which all test classes inherit from.
Logging¶
Logs for a test run can be seen by filtering on the Literator
tag. After running a test with Literator successfully setup, you should see output similar to below:
D Literator initialized.
D Attempting to establish new session.
D Session successfully started. You should see a new notification to start a recording.
D Session enqueued for processing. Results for this session will appear on: <board_url>
D Test 'open_settings' passed. Literal captured test: true
-
Espresso provides developers the ability to create automated UI tests via code written in Java or Kotlin. Interactions may also be recorded to generate the underlying test code, see Espresso's test recorder feature. ↩