Define Android UI tests with JSON 🤓
This library contains helper classes for UI testing, making screenshots etc.
The Stanwood UI testing framework is hosted on JitPack. Therefore you can simply import it by adding the following text to your apps’s build.gradle
.
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Then add this to you app’s build.gradle
:
dependencies {
androidTestImplementation "com.github.stanwood:ui_testing_android:<add latest version here>"
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
}
Make sure to always clean the project after updating any of these test dependencies, the Android Gradle Plugin doesn’t always seem to automatically pick up these changes when building the test artifacts.
Also in build.gradle
setup the orchestrator as described in the documentation.
A simple test class SUITest needs to be added to the following folder
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class SUITest extends BaseTest {
private static final String JSON_SCHEMA_FILE = "res/raw/test_schema.json";
@Before
public void start() {
SlackTracker slackTracker = new SlackTracker(
"<add slack channel name here>",
"<add slack team id here>",
"<add slack token here>");
//FirebaseSchemaProvider schemaProvider = new FirebaseSchemaProvider("io.stanwood.uitesting.demo", "1.0.0");
ResourceSchemaProvider schemaProvider = new ResourceSchemaProvider(this, JSON_SCHEMA_FILE, BuildConfig.APPLICATION_ID);
super.initTests(schemaProvider, slackTracker);
}
// DO NOT leave this method out: it just does the super call, but adds the @Test annotation!
@Test
public void runTests() {
super.runTests();
}
}
Same class in Kotlin:
const val JSON_SCHEMA_FILE = "res/raw/test_schema.json"
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 18)
class SUITest : BaseTest() {
@Before
fun start() {
ResourceSchemaProvider(this, JSON_SCHEMA_FILE, BuildConfig.APPLICATION_ID).let {
super.initTests(it)
}
}
@Test
public override fun runTests() {
super.runTests()
}
}
Dots (.
) in texts need to be escaped with two backslashes like so: \\.
.
{
"initial_sleep_time": 2000,
"command_sleep_time": 1000,
"launch_timeout": 5000,
"view_timeout": 10000,
"auto_snapshot": false,
"test_cases": [
{
"id": "1",
"title": "Simple button click test",
"description": "Simple button click test",
"enabled": true, // optional, true by default, can be used to quickly disable test cases
"navigation": [
"view['@button'].setText['12345']", // set text on EditText defined by resource ID
"sleep",
"snapshot",
"view['text with a dot\\. Do not forget to escape!'].tap", // tap on View defined by its text
"sleep",
"snapshot"
]
}
]
}
For quick tests the json file can be stored within an app (under /res/raw/test_schema.json).
But in most cases the test schema file should be stored as a resource which can be grabbed via http request (https://console.firebase.google.com/u/0/project/stanwood-ui-testing/database/stanwood-ui-testing/data)
There are two ways how to run tests
Select SUITest class in the left Navigator view, right click it, and select “Run/Debug SUITest”
./run_ui_tests -a app.id.to.test -c full.reference.to.a.test.class -b BuildName [-e avd_name]
-a, —app-id - app id
-c, —test-class - full reference to the class that runs tests
-b, —build - build/flavour name (Release, Debug, FlavorDebug, Qa, etc.)
-e, —avd-name - optional - avd image name that will be started where test will be executed
For more information check out the sample app.