commit | f417eaa29c72692ede7b5c211a0bf7fc90d47dd9 | [log] [tgz] |
---|---|---|
author | Alexander Dorokhine <adorokhine@google.com> | Mon Dec 05 17:05:29 2016 -0800 |
committer | Alexander Dorokhine <adorokhine@google.com> | Mon Dec 05 17:05:29 2016 -0800 |
tree | 703381bc657c72d1be5be271c3115140667b812e | |
parent | 171092d2b296115510b5bc0b3cf224a4b05fc638 [diff] |
Launch snippets as an instrumentation rather than a service. This allows us to create snippets that control other apps.
Mobly Snippet Lib is a library for triggering device-side code from host-side Mobly tests. This tutorial teaches you how to use the snippet lib to trigger custom device-side actions.
This is not an official Google product.
Link against Mobly Snippet Lib in your build.gradle
file
apply plugin: 'com.android.application' dependencies { androidTestCompile 'com.google.android.mobly:snippetlib:0.0.1' }
In your androidTest
source tree, write a Java class implementing Snippet
and add methods to trigger the behaviour that you want. Annotate them with @Rpc
package com.my.app.test; ... public class ExampleSnippet implements Snippet { public ExampleSnippet(Context context) {} @Rpc(description='Returns a string containing the given number.') public String getFoo(Integer input) { return 'foo ' + input; } @Override public void shutdown() {} }
Add any classes that implement the Snippet
interface in your AndroidManifest.xml
application section as meta-data
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.app.test"> <application> <meta-data android:name="mobly-snippets" android:value="com.my.app.test.MySnippet1, com.my.app.test.MySnippet2" /> ...
Build your apk and install it on your phone
In your Mobly python test, connect to your snippet .apk in setup_class
class HelloWorldTest(base_test.BaseTestClass): def setup_class(self): self.ads = self.register_controller(android_device) self.dut1 = self.ads[0] self.dut1.load_snippets(name='snippet', package='com.my.app.test') if __name__ == '__main__': test_runner.main()
Invoke your needed functionality within your test
def test_get_foo(self): actual_foo = self.dut1.snippet.getFoo(5) asserts.assert_equal("foo 5", actual_foo)