|  | ||
|---|---|---|
| .. | ||
| src/main | ||
| README.md | ||
| build.gradle | ||
		
			
				
				README.md
			
		
		
			
			
		
	
	Standalone Snippet App Example
This tutorial shows you how to create a standalone Mobly snippet app. To create a snippet app that controls (instruments) another app under test, please see Example 2.
Tutorial
- 
Use Android Studio to create a new app project. 
- 
Link against Mobly Snippet Lib in your build.gradlefiledependencies { implementation 'com.google.android.mobly:mobly-snippet-lib:1.3.1' }
- 
Write a Java class implementing Snippetand add methods to trigger the behaviour that you want. Annotate them with@Rpcpackage com.my.app; ... public class ExampleSnippet implements Snippet { @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 Snippetinterface in yourAndroidManifest.xmlapplication section asmeta-data<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.app"> <application> <meta-data android:name="mobly-snippets" android:value="com.my.app.test.MySnippet1, com.my.app.test.MySnippet2" /> ...
- 
Add an instrumentationtag to yourAndroidManifest.xmlso that the framework can launch your server through aninstrumentcommand.<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.app"> <application>...</application> <instrumentation android:name="com.google.android.mobly.snippet.ServerRunner" android:targetPackage="com.my.app" /> </manifest>
- 
Build your apk and install it on your phone 
- 
In your Mobly python test, connect to your snippet .apk in setup_classclass HelloWorldTest(base_test.BaseTestClass): def setup_class(self): self.ads = self.register_controller(android_device) self.dut1 = self.ads[0] self.dut1.load_snippet(name='snippet', package='com.my.app.test')
- 
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)
Running the example code
This folder contains a fully working example of a standalone snippet apk.
- 
Compile the example ./gradlew examples:ex1_standalone_app:assembleDebug
- 
Install the apk on your phone adb install -r ./examples/ex1_standalone_app/build/outputs/apk/debug/ex1_standalone_app-debug.apk
- 
Use snippet_shellfrom mobly to triggergetFoo():snippet_shell.py com.google.android.mobly.snippet.example1 >>> print(s.help()) Known methods: getBar(String) returns String // Returns the given string with the prefix "bar" getFoo(Integer) returns String // Returns the given integer with the prefix "foo" >>> s.getFoo(5) u'foo 5'