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 | |
| 7 | This is not an official Google product. |
| 8 | |
| 9 | ## Prerequisites |
| 10 | - This tutorial assumes basic familiarity with the Mobly framework, so please |
| 11 | follow the [Mobly tutorial](http://github.com/google/mobly) before doing |
| 12 | this one. |
| 13 | - You should know how to create an Android app and build it with gradle. If |
| 14 | not, follow the |
| 15 | [Android app tutorial](https://developer.android.com/training/basics/firstapp/index.html). |
| 16 | |
| 17 | ## Using Mobly Snippet Lib |
| 18 | |
| 19 | 1. Link against Mobly Snippet Lib in your `build.gradle` file |
| 20 | |
| 21 | ``` |
| 22 | apply plugin: 'com.android.application' |
| 23 | dependencies { |
| 24 | androidTestCompile 'com.google.android.mobly:snippetlib:0.0.1' |
| 25 | } |
| 26 | ``` |
| 27 | |
| 28 | 2. In your `androidTest` source tree, write a Java class implementing `Snippet` |
| 29 | and add methods to trigger the behaviour that you want. Annotate them with |
| 30 | `@Rpc` |
| 31 | |
| 32 | ```java |
| 33 | package com.my.app.test; |
| 34 | |
| 35 | ... |
| 36 | |
| 37 | public class ExampleSnippet implements Snippet { |
| 38 | public ExampleSnippet(Context context) {} |
| 39 | |
| 40 | @Rpc(description='Returns a string containing the given number.') |
| 41 | public String getFoo(Integer input) { |
| 42 | return 'foo ' + input; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void shutdown() {} |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | 3. Add any classes that implement the `Snippet` interface in your |
| 51 | `AndroidManifest.xml` application section as `meta-data` |
| 52 | |
| 53 | ```xml |
| 54 | <manifest xmlns:android="http://schemas.android.com/apk/res/android" |
| 55 | package="com.my.app.test"> |
| 56 | <application> |
| 57 | <meta-data |
| 58 | android:name="mobly-snippets" |
| 59 | android:value="com.my.app.test.MySnippet1, |
| 60 | com.my.app.test.MySnippet2" /> |
| 61 | ... |
| 62 | ``` |
| 63 | |
| 64 | 4. Build your apk and install it on your phone |
| 65 | |
| 66 | 5. In your Mobly python test, connect to your snippet .apk in `setup_class` |
| 67 | |
| 68 | ```python |
| 69 | class HelloWorldTest(base_test.BaseTestClass): |
| 70 | def setup_class(self): |
| 71 | self.ads = self.register_controller(android_device) |
| 72 | self.dut1 = self.ads[0] |
| 73 | self.dut1.load_snippets(name='snippet', package='com.my.app.test') |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | test_runner.main() |
| 77 | ``` |
| 78 | |
| 79 | 6. Invoke your needed functionality within your test |
| 80 | |
| 81 | ```python |
| 82 | def test_get_foo(self): |
| 83 | actual_foo = self.dut1.snippet.getFoo(5) |
| 84 | asserts.assert_equal("foo 5", actual_foo) |
| 85 | ``` |