# Product 2 - Android Automation
## Goal
Automate the smoke test process in iOS and Android devices
## Happy Test
1/ Start the test
- Given the apk name ABC.apk
- When press on start the test
- Then the game can launch on Android device automatically
2/
## Tech Options
> Advance solutions can be checked here: https://medium.com/better-programming/6-ways-to-do-automated-ui-testing-in-parallel-with-selenium-132e47403c4f
### Option 1: [Appium](http://appium.io/docs/en/about-appium/getting-started/#running-your-first-test)
Props:
- Standard Mobile app automation
- Can apply for IOS
- Support screenshot, ....
Cons:
- Learning curve, hard to setup
> If your game is developed with Unity, you can use **AltUnity Tester** with Appium to get the objects from Unity to write custom scripts.
### Option 2: ADB
using ADB input for send touch
https://developer.android.com/studio/command-line/adb
https://stackoverflow.com/questions/4386449/send-touch-events-to-a-device-via-adb
Nodejs To call adb
https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options
```
Start activities
adb shell am start -n com.example.demo/com.example.test.MainActivity
```
Props:
- Easy to setup
Cons:
- Only work for android
> [Dante] Not go with this option as it's only applicable for Android
>
## POC(Proof of concept)
#### Option 1: Use Appium only
- [x] 1.1 - Try follow getting start
http://appium.io/docs/en/about-appium/getting-started/#running-your-first-test
ETA: 2 hour - Done
- [x] 1.2 - Try to test the apk from LittleRabbitGames with Appium
ETA: 2 hour - Done
### What was done
Step 1: Setup Appium
- Make sure you have NPM installed
- Install Appium: `npm install -g appium`
- Verifying the Installation:
> To verify that all of Appium's dependencies are met you can use appium-doctor. Install it with npm install -g appium-doctor, then run the appium-doctor command, supplying the --ios or --android flags to verify that all of the dependencies are set up correctly.
> Note that if you are using MacOS and need to set up JAVA_HOME & ANDROID_HOME, the environemnt setup is based on if you use .bash or .zsh
> Example setup for .zsh
> 1.Open The Terminal
> 2.Type below command: `nano ~/.zshrc`
>then one window will open add the path as per ur system directory
```
export ANDROID_HOME=/Users/<yourusername>/Library/Android/sdk
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=${JAVA_HOME}/bin:$PATH
export PATH=${PATH}:${ANDROID_HOME}/tools
export PATH=${PATH}:${ANDROID_HOME}/platforms-tools
export PATH=${PATH}:${ANDROID_HOME}/build-tools/27.0.1
export PATH=${PATH}:${JAVA_HOME}
```
> 3.Run `source ./.zshrc` to update environment without restart your Mac
> The appium-doctor should be run in the same Terminal window where you setup path to zsh
- Download the example apk and follow the steps [here](http://appium.io/docs/en/about-appium/getting-started/?lang=en) to check if Appium can run correctly in your Mac.
Step 2: Source that modified to used with an apk from Little Rabbit Games which was made in Unity
> You will need to launch appium in terminal first to start server.
> Below is the client code index.js
```
const wdio = require("webdriverio");
const assert = require("assert");
const { Console } = require("console");
const fs = require('fs');
const opts = {
path: '/wd/hub',
port: 4723,
capabilities: {
platformName: "Android",
platformVersion: "9",
deviceName: "Samsung Galaxy S9",
app: "/Users/dante/Appium/EpicHoop_Ama_v110_200812.apk",
appPackage: "com.amanotes.pamaepichoopgp",
appActivity: "com.google.firebase.MessagingUnityPlayerActivity",
automationName: "UiAutomator2",
}
};
async function main() {
const client = await wdio.remote(opts);
try {
await client.touchAction({
action: 'wait',
ms: 8000
});
let screenshot = await client.takeScreenshot();
fs.writeFileSync('./screenshot.png', Buffer.from(screenshot.toString(),'base64'));
//tap on Play a song
await client.touchAction({
action: 'tap',
x: 930,
y: 1300
});
//wait for loaing
await client.touchAction({
action: 'wait',
ms: 4000
});
//tap to open cheat
for(let i = 0; i< 6; i++){
await client.touchAction({
action: 'tap',
x: 200,
y: 80
});
}
//wait for cheat open
await client.touchAction({
action: 'wait',
ms: 1000
});
//tap on auto play
await client.touchAction({
action: 'tap',
x: 530,
y: 300
});
//wait to close cheat
await client.touchAction({
action: 'wait',
ms: 500
});
//tap to close cheat
await client.touchAction({
action: 'tap',
x: 920,
y: 100
});
}
catch (err) {
console.log(err);
}
// await client.deleteSession();
}
main();
//end
```
Result:
- Launch game automatically
- Wait till loading screen
- Capture a screenshot in main menu and save to root folder
- Tap on play button
- Wait for load action phase
- Tap 5 times to open cheat
- Tap on auto play cheat option
- Wait to close cheat
- Tap to close cheat menu
iOS case:
#### Option 2: Use AltUnity Tester only
#### Option 3: Use combination Appium + AltUnity Tester
> Option 4: Try write nodejs, call adb open activities + send touch event
> [Dante] Not go with this option as it's only applicable for Android
## Production
Go with option 1
Step 2:
``` javascript
// Module1.js
// function X
// function Y
```
###### tags: `Dante` `Learning Project`