# Automation Test via Appium - Interaction between test and app
###### tags: `Test` `Appium` `Python` `Interaction`
經討論後,好奇 appium 有什麼辨法可以傳檔案或指令給 app?
最初查到的資料是
* [Testing mobile app and passing test data between test automation and test app](https://github.com/appium/appium/issues/9672)
* [Launching apps with options](https://github.com/appium/appium/issues/9659)
* [How to access args on mobile that were passed in processArguments](https://discuss.appium.io/t/how-to-access-args-on-mobile-that-were-passed-in-processarguments/19750)
得知可以在啟動 app 時順便傳參數給 app,但一直不知怎樣使 app 得到傳入的資訊。
自己測試這樣設定 Capabilities
```
# customized parameter
param = {'DONGLE_IP_ADDRESS': '122.146.250.176'}
self.desired_capabilities['processArguments'] = param
```
會看見下面的訊息
```
The following capabilities were provided, but are not recognized by Appium: processArguments
```
之後一直 Google Search, 卻沒想要的結果
* [Starting your iOS app with Process Arguments in Appium](https://medium.com/@mjm_/starting-your-ios-app-with-process-arguments-in-appium-2fe137826cb6)
* [Launch Android app with arguments](https://github.com/baldurk/renderdoc/issues/987)
* [Set parameters to launch app](http://hy1984427.github.io/appium/write_script_for_android_app/set_parameters_to_launch_app.html)
然後我原本想說:是不是設定 ``processArguments``,
我就可以在 Android ``Activity`` 從 intent 中取得我要的資訊
於是就好奇 [Appium Desired Capabilities](https://appium.readthedocs.io/en/latest/en/writing-running-appium/caps/) 文件
是不是有已經訂好的 key 值,可以讓我達成這個目的,
後來看見有一個 key 值 ``optionalIntentArguments`` 並帶 ``--es <extra_key> <extra_string> ``
就可以讓 Android ``Activity`` 從 intent 中取得我要的資訊
於是注意到 [Launch Android app with arguments](https://github.com/baldurk/renderdoc/issues/987) 也提到此事,
按照它上面的寫法,自己這樣測試
```
bundle_args = ' --es DONGLE_IP_ADDRESS 122.146.250.176'
self.desired_capabilities['optionalIntentArguments'] = bundle_args
```
```
private fun getDongleIpAddressFromAppium(theIntent: Intent?) {
if (null != theIntent) {
val ipAddpress = theIntent.getStringExtra("DONGLE_IP_ADDRESS")
if (null != ipAddpress) {
Toast.makeText(this@MainActivity, "Get IP address: $ipAddpress", Toast.LENGTH_LONG).show()
}
else {
Toast.makeText(this@MainActivity, "Get No IP address!!", Toast.LENGTH_LONG).show()
}
}
else {
Toast.makeText(this@MainActivity, "No DONGLE_IP_ADDRESS Intent!!", Toast.LENGTH_LONG).show()
}
}
```
看見 appium 在啟動 app 時的 ``adb`` 指令中確實有帶入
`` --es DONGLE_IP_ADDRESS 122.146.250.176``
```
[ADB] Running '/home/elite_lin/Android/Sdk/platform-tools/adb -P 5037 -s ce031713210f50e20c shell am start -W -n tw.com.goglobal.projects.smartinspectionsystems/.MainActivity -S --es DONGLE_IP_ADDRESS 122.146.250.176'
```
而當 App 啟動時,我也有看見 Toast 有顯示收到的 Ip address ``122.146.250.176``
---
# [Testing mobile app and passing test data between test automation and test app](https://github.com/appium/appium/issues/9672)
### The problem
I have mobile test app and Appium test automation.
I want to pass data between the test automation and test app
for test validation how can I do it?
---
#### mykola-mokhnach commented on 21 Nov 2017
You can pass __command line arguments__ and __environment variables__
to the application under test. Also,
in Android it is possible to push a file to the device.
See [#9659](https://github.com/appium/appium/issues/9659), for example.
#### asoneji commented on 21 Nov 2017
We are putting the data on storage and accessing from it
but we need to some unique id so we can track different test run/test cases.
https://github.com/libimobiledevice -
What about integrating something like this to pushFile to iOS device?
Pushing in the media folder or other folder where iOS allows sending files.
If we use processArguments on capability
how do we access that data from the iOS app and Android App?
Thanks for the help!
#### mykola-mokhnach commented on 21 Nov 2017
We are already using libimobiledevice there. Regarding the access to the file system - we can consider support of https://github.com/libimobiledevice/ifuse, however it might not be always stable, especially after Apple releases a new iOS version. You could create a feature request though.
---
# [Launching apps with options](https://github.com/appium/appium/issues/9659)
### The problem
I have an .ipa file that I’m trying to launch via the xcuitest driver.
I want to __launch the app with certain arguments passed upon launch__.
I have an __environmental variable__ which
is present in the xcode project called ``GE_VERBOSE 1``
When this environmental variable is activated
when running the app in xcode,
it disables an overlay screen that pops up when the app launches.
How can I do the same with appium?
How can I pass this argument on launch __using the inspector session__ or
__command line__ (xcuitest)?
I __tried using the ``processArguments = GE_VERBOSE 1``
in the desired capabilities but it didn’t like the format I gave it__.
The goal is to have a way to launch the app
for automated testing by successfully bypassing
the obstructive overlay screen with launches when the app is installed freshly.
#### imurchie commented on 18 Nov 2017
Process arguments (the processArguments capability) get sent in as an object, with the properties args and/or env. args is an array of arguments to be passed to the app. env is an object with key-value pairs to set the environment.
In the abstract it will look something like
{"args": ["a", "b", "c"], "env": { "a": "b", "c": "d" }}
You should try:
{"env": {"GE_VERBOSE": 1}}
####
final List<String> processArgs = new ArrayList<>(Arrays.asList(
"--debug-log-network"
, "-com.apple.CoreData.ConcurrencyDebug", "1"
));
final Map<String, Object> processEnv = new HashMap<>();
processEnv.put("key", "value");
final JSONObject argsValue = new JSONObject();
argsValue.put("args", processArgs);
argsValue.put("env", processEnv);
capabilities.setCapability("processArguments", argsValue.toString());
---
## [Push File](http://appium.io/docs/en/commands/device/files/push-file/)
Place a file onto the device in a particular place
### Usage
```
dest_path = '/data/local/tmp/test_push_file.txt'
data = bytes('This is the contents of the file to push to the device.', 'utf-8')
self.driver.push_file(dest_path, base64.b64encode(data).decode('utf-8'))
```
---
## [Send Keys](http://appium.io/docs/en/commands/element/actions/send-keys/)
Send a sequence of key strokes to an element
### Usage
```
self.driver.find_element_by_accessibility_id('SomeAccessibilityID').send_keys('Hello world!')
```
### Description
Any UTF-8 character may be specified, however,
if the server does not support native key events,
it should simulate key strokes for a standard US keyboard layout.
The Unicode Private Use Area code points, 0xE000-0xF8FF,
are used to represent pressable, non-text keys (see table below).
(See Unicode document for information on Unicode characters)
---
## [Execute Mobile Command](http://appium.io/docs/en/commands/mobile-command/)
Execute a native mobile command
### Usage
```
self.driver.execute_script("mobile: scroll", {'direction': 'down'})
```
### Description
Execute a variety of native,
mobile commands that aren't associated with a specific endpoint
Syntax is execute("mobile: <commandName>", <JSON serializable argument>)
(see Execute Script for more details on syntax).
List of available commands:
---
# Other links
[How to Automate Appium Java Tests In Parallel using TestNG](https://bitbar.com/blog/how-to-automate-appium-java-tests-in-parallel-using-testng/)
[Appium Tip #2: Using Python to Upload and Configure Your Apps and Tests](https://bitbar.com/blog/appium-tip-2-using-python-to-upload-and-configure-your-apps-and-tests/)
https://github.com/bitbar/testdroid-samples/tree/master/appium/sample-scripts/python