项目作者: stanwood

项目描述 :
Define Android UI tests with JSON 🤓
高级语言: Java
项目地址: git://github.com/stanwood/ui_testing_android.git
创建时间: 2018-06-13T12:25:17Z
项目社区:https://github.com/stanwood/ui_testing_android

开源协议:MIT License

下载


Release

Stanwood UI testing framework

This library contains helper classes for UI testing, making screenshots etc.

Table of contents

Import

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.

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url "https://jitpack.io" }
  5. }
  6. }

Then add this to you app’s build.gradle:

  1. dependencies {
  2. androidTestImplementation "com.github.stanwood:ui_testing_android:<add latest version here>"
  3. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
  4. androidTestImplementation 'com.android.support.test:runner:1.0.2'
  5. androidTestImplementation 'com.android.support.test:rules:1.0.2'
  6. androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
  7. androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
  8. }

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.

Add test class

A simple test class SUITest needs to be added to the following folder
TestClassFolder

  1. @RunWith(AndroidJUnit4.class)
  2. @SdkSuppress(minSdkVersion = 18)
  3. public class SUITest extends BaseTest {
  4. private static final String JSON_SCHEMA_FILE = "res/raw/test_schema.json";
  5. @Before
  6. public void start() {
  7. SlackTracker slackTracker = new SlackTracker(
  8. "<add slack channel name here>",
  9. "<add slack team id here>",
  10. "<add slack token here>");
  11. //FirebaseSchemaProvider schemaProvider = new FirebaseSchemaProvider("io.stanwood.uitesting.demo", "1.0.0");
  12. ResourceSchemaProvider schemaProvider = new ResourceSchemaProvider(this, JSON_SCHEMA_FILE, BuildConfig.APPLICATION_ID);
  13. super.initTests(schemaProvider, slackTracker);
  14. }
  15. // DO NOT leave this method out: it just does the super call, but adds the @Test annotation!
  16. @Test
  17. public void runTests() {
  18. super.runTests();
  19. }
  20. }

Same class in Kotlin:

  1. const val JSON_SCHEMA_FILE = "res/raw/test_schema.json"
  2. @RunWith(AndroidJUnit4::class)
  3. @SdkSuppress(minSdkVersion = 18)
  4. class SUITest : BaseTest() {
  5. @Before
  6. fun start() {
  7. ResourceSchemaProvider(this, JSON_SCHEMA_FILE, BuildConfig.APPLICATION_ID).let {
  8. super.initTests(it)
  9. }
  10. }
  11. @Test
  12. public override fun runTests() {
  13. super.runTests()
  14. }
  15. }

Resource ids

  1. Start the app within emulator or on a device(needs to be connected to the computer)
  2. Start uiautomatorviewer (usually at /Users/username/Library/Android/sdk/tools/bin)
    ./uiautomatorviewer
  3. Pick the desired device (click at an upper ‘phone’ icon)
    UiAutomatorViewer
  4. UIAutomatorViewer has made a screenshot of current screen. Now you can explore all the visible views and get their resource ids, which are needed in order to be able to look for them within tests
    UiAutomatorViewer
    On the left side you can see a screenshot of the current view.
    On the right side you see all the views used within current activity.
    In this particular case the resource id of the selected view is: countryTitle
    This id is being used within test if we want to do some action on that view, or maybe just test if the view is being displayed
    There might be the case that a view does not have resource id defined yet, especially when/if databinding is being used. In that case the developer either needs to define a resource id for that view in the XML layout or you search for the View by the text on it.

Supported actions

Actions

Dots (.) in texts need to be escaped with two backslashes like so: \\..

Json example

  1. {
  2. "initial_sleep_time": 2000,
  3. "command_sleep_time": 1000,
  4. "launch_timeout": 5000,
  5. "view_timeout": 10000,
  6. "auto_snapshot": false,
  7. "test_cases": [
  8. {
  9. "id": "1",
  10. "title": "Simple button click test",
  11. "description": "Simple button click test",
  12. "enabled": true, // optional, true by default, can be used to quickly disable test cases
  13. "navigation": [
  14. "view['@button'].setText['12345']", // set text on EditText defined by resource ID
  15. "sleep",
  16. "snapshot",
  17. "view['text with a dot\\. Do not forget to escape!'].tap", // tap on View defined by its text
  18. "sleep",
  19. "snapshot"
  20. ]
  21. }
  22. ]
  23. }

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)

Run tests

There are two ways how to run tests

Within the Android Studio IDE

Select SUITest class in the left Navigator view, right click it, and select “Run/Debug SUITest”

With a helper script

Script

./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.

Roadmap