Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 1 | # Getting Started with Snippets for Mobly |
| 2 | |
| 3 | Mobly Snippet Lib is a library for triggering device-side code from host-side |
| 4 | [Mobly](http://github.com/google/mobly) tests. This tutorial teaches you how to |
| 5 | use the snippet lib to trigger custom device-side actions. |
| 6 | |
Alexander Dorokhine | a113c04 | 2016-12-05 15:07:55 -0800 | [diff] [blame] | 7 | Note: Mobly and the snippet lib are not official Google products. |
| 8 | |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 9 | |
| 10 | ## Prerequisites |
Alexander Dorokhine | a113c04 | 2016-12-05 15:07:55 -0800 | [diff] [blame] | 11 | |
| 12 | - These examples and tutorials assume basic familiarity with the Mobly |
| 13 | framework, so please follow the |
| 14 | [Mobly tutorial](http://github.com/google/mobly) before doing this one. |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 15 | - You should know how to create an Android app and build it with gradle. If |
| 16 | not, follow the |
| 17 | [Android app tutorial](https://developer.android.com/training/basics/firstapp/index.html). |
| 18 | |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 19 | |
Alexander Dorokhine | a113c04 | 2016-12-05 15:07:55 -0800 | [diff] [blame] | 20 | ## Overview |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 21 | |
Alexander Dorokhine | a113c04 | 2016-12-05 15:07:55 -0800 | [diff] [blame] | 22 | The Mobly Snippet Lib allows you to write Java methods that run on Android |
| 23 | devices, and trigger the methods from inside a Mobly test case. The Java methods |
| 24 | invoked this way are called `snippets`. |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 25 | |
Alexander Dorokhine | a113c04 | 2016-12-05 15:07:55 -0800 | [diff] [blame] | 26 | The `snippet` code can either be written in its own standalone apk, or as a |
| 27 | [product flavor](https://developer.android.com/studio/build/build-variants.html#product-flavors) |
| 28 | of an existing apk. This allows you to write snippets that instrument or |
| 29 | automate another app. |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 30 | |
adorokhine | 03478b8 | 2017-01-27 15:24:49 -0800 | [diff] [blame] | 31 | |
| 32 | ## Under The Hood |
| 33 | |
| 34 | A snippet is launched by an `am instrument` call. Snippets use a custom |
| 35 | `InstrumentationTestRunner` derived from `AndroidJUnitRunner`. This allows |
| 36 | for snippets that interact with a main app's classes, such as Espresso snippets, |
| 37 | and allows you to get either the test app's or the main app's context from |
| 38 | `InstrumentationRegistry`. |
| 39 | |
| 40 | Once started, the special runner starts a web server which listens for requests |
Kenny Root | 1706b21 | 2018-09-07 04:43:08 +0900 | [diff] [blame] | 41 | to trigger snippets. The server's handler locates the corresponding methods by |
| 42 | reflection, runs them, and returns results over the TCP socket. All common |
adorokhine | 03478b8 | 2017-01-27 15:24:49 -0800 | [diff] [blame] | 43 | built-in variable types are supported as arguments. |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 44 | |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 45 | |
Alexander Dorokhine | a113c04 | 2016-12-05 15:07:55 -0800 | [diff] [blame] | 46 | ## Usage |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 47 | |
Alexander Dorokhine | 0d91300 | 2016-12-08 11:15:59 -0800 | [diff] [blame] | 48 | The [examples/](examples/) folder contains examples of how to use the |
Alexander Dorokhine | a113c04 | 2016-12-05 15:07:55 -0800 | [diff] [blame] | 49 | mobly snippet lib along with detailed tutorials. |
Alexander Dorokhine | b1b8b50 | 2016-11-10 13:52:59 -0800 | [diff] [blame] | 50 | |
adorokhine | 787e6a7 | 2017-01-10 18:19:50 -0800 | [diff] [blame] | 51 | * [ex1_standalone_app](examples/ex1_standalone_app): Basic example of a |
| 52 | snippet which is compiled into its own standalone apk. |
| 53 | * [ex2_espresso](examples/ex2_espresso): Example of a snippet which |
David T.H. Kao | a6f4252 | 2017-08-15 21:52:46 -0700 | [diff] [blame] | 54 | instruments a primary app to drive its UI using |
Alexander Dorokhine | a113c04 | 2016-12-05 15:07:55 -0800 | [diff] [blame] | 55 | [Espresso](https://google.github.io/android-testing-support-library/docs/espresso/). |
David T.H. Kao | a6f4252 | 2017-08-15 21:52:46 -0700 | [diff] [blame] | 56 | * [ex3_async_event](examples/ex3_async_event): Example of how to use the |
Kenny Root | 1706b21 | 2018-09-07 04:43:08 +0900 | [diff] [blame] | 57 | @AsyncRpc annotation to handle asynchronous callbacks. |
David T.H. Kao | a6f4252 | 2017-08-15 21:52:46 -0700 | [diff] [blame] | 58 | * [ex4_uiautomator](examples/ex4_uiautomator): Example of how to create |
| 59 | snippets that automate the UI actions using UIAutomator. Unlike Espresso |
| 60 | UIAutomator works even without access to app source code. |
| 61 | * [ex5_schedule_rpc](examples/ex5_schedule_rpc): Example of how to use the |
| 62 | 'scheduleRpc' RPC to execute another RPC at a later time, potentially after |
| 63 | device disconnection. |
Ang Li | c2d947e | 2018-01-12 11:54:03 -0800 | [diff] [blame] | 64 | * [ex6_complex_type_conversion](examples/ex6_complex_type_conversion): Example of how to pass a |
| 65 | non-primitive type to the Rpc methods and return non-primitive type from Rpc methods by |
| 66 | supplying a type converter. |