# How To Do App Testing In Ubuntu Docker Using Command Line?
###### tags: `Guide`
**From your terminal, you can use `docker pull ubuntu:latest` or `docker run -it ubuntu:latest` to create a new docker image.**
> Before we get started, just to notify you that the following steps are based on Ubuntu 20.04.2 LTS version.
### **Step 1 - Update System and Install Basic Command Tool**
The Ubuntu image lacks lots of basic commands, so you have to install them manually, depending on your needs.
```
apt update && apt upgrade -y \
&& apt install wget vim sudo curl unzip net-tools -y \
&& apt install libglu1 libpulse-dev libasound2 libc6 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxi6 libxtst6 libnss3 -y
```
During the installation, choose Timezone Asia (6) and Taipei (73) when installation progress is about 56%.
### **Step 2 - Create User and Programming Packages**
After you have `sudo` command, you can now create an user to do your work. Here, I named a user as "test".
```
adduser test \
&& vim /etc/sudoers
```
In `sudoers` file, you gotta specify the user to be granted **sudo permission**. Append your users in privilege specification. In my case, it would be like below.
> User privilege specification
root ALL=(ALL:ALL) ALL
test ALL=(ALL:ALL) ALL
Now change the user to the one you just created by using `su test`.
### **Step 3 - Install Requirements**
Logging in as root may lead to some problem when installing. Just remember you should use other user with `sudo` permission.
#### 0. Prerequisite
```
sudo apt install python3 python3-pip -y
pip3 install robotframework appium-python-client robotframework-appiumlibrary
```
Install the packages you'll need for automated test suites.
#### 1. Here, we're installing java jdk for Android Studio
```
cd ~
sudo apt install openjdk-11-jdk -y
```
#### 2. Then install linuxbrew for installing nodejs (Using `sudo apt install` will block some operation of nodejs, causing problems when running Appium)
You can visit **[Linuxbrew](https://brew.sh/)** for more information.
```
sudo apt install build-essential file git -y
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
Add the following path to the end of `.bashrc`.
```
vim .bashrc
```
> export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
> export BREW_HOME="/home/linuxbrew/.linuxbrew/bin"
> export ROBOT_HOME="/home/test/.local/bin"
> export PATH="\$PATH:\$JAVA_HOME:\$ROBOT_HOME:\$BREW_HOME"
```
source .bashrc
brew update
brew install node
```
#### 3. Use **`npm`** to download Appium
```
npm install -g appium
npm install -g appium-doctor
npm install -g wd
```
#### 4. Verify requirements are installed
Input `jave -version`, `brew -v`, `npm -v`, `node -v`, `appium-doctor` to check if they're correctly installed. Try `source .bashrc` or confirm your variable environment whenever you receive messages like "command not found"
> You can use `appium-doctor --android` or `appium-doctor --ios` to specify platforms.
### **Step 4 - Install Android SDK Command Line Tool**
To install Android Studio command line tool, you can visit [Android Studio](https://developer.android.com/studio#downloads) for more detail.
```
wget https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip
unzip commandlinetools-linux-6858069_latest.zip
rm commandlinetools-linux-6858069_latest.zip
```
After unzipping Android Studio, you gotta do something in advance, because it seems Google doesn't wanna fix it.
```
cd ~
mkdir tools/
mv cmdline-tools/* tools/
mv tools/ cmdline-tools/
mkdir sdk/
mv cmdline-tools/ sdk/
```
The reason why we have to do so is because Android Studio we downloaded from web is named "cmdline-tools", however, the files under `cmdline-tools/` should be under `cmdline-tools/tools/`.
After replace the folder and files, add variable path to `.bashrc`.
```
vim .bashrc
```
> export ANDROID_HOME="/home/test/sdk"
> export ANDROID_SDK_ROOT="\$ANDROID_HOME"
> export ANDROID_AVD_ROOT="/home/test/.android/avd"
> export ANDROID_EMULATOR_HOME="/home/test/sdk/emulator"
> export PATH="\$PATH:\$ANDROID_HOME:\$ANDROID_SDK_ROOT:\$ANDROID_AVD_ROOT:\$ANDROID_EMULATOR_HOME"
> export PATH="\$PATH:\$ANDROID_SDK_ROOT/cmdline-tools/tools/bin:\$ANDROID_SDK_ROOT/platform-tools"
```
source .bashrc
```
Input the following command to see if it works well. If not, please check your `export path` again.
```
sdkmanager --update
sdkmanager --list
(or specifically sdkmanager --list | grep system-images)
```
Type `sdkmanager --help` for more information.
### **Step 5 - Install Required Tools And Images To Start Emulator**
```
sdkmanager "emulator" "platform-tools" "platforms;android-25" "system-images;android-25;google_apis;armeabi-v7a"
```
Since you're gonna run emulator on Ubuntu OS, you have to choose the `system-images` that ends like `armeabi-v7a...`, the ones with intel or x64 are not compatible with Ubuntu.
You can decide whether to install `sdkmanager "build-tools;25.0.0"`. You might get `android could NOT be found in /Path/` from `appium-doctor`, but it doesn't really affect your headless avd tests.
##### After the installation, I suggest you can use `emulator` command to make sure the variable environment is all set. If you receive `emulator: command not found`, please restart your system or log out user.
Then go create an avd device.
```
avdmanager create avd -n demoTest -d "pixel" -k "system-images;android-25;google_apis;armeabi-v7a" -g "google_apis" -b "armeabi-v7a"
```
##### I named my device as "demoTest"
> You will get "Do you wish to create a custom hardware profile?" from the terminal,
> I suppose "no" should be enough for you.
After, use avd to run emulator and adb device.
```
emulator @demoTest -no-window -no-audio -no-snapshot-load -verbose &
adb kill-server && adb devices
appium &
adb shell service list
```
Then input `ps nx | grep emulator` to check if it's running. If it works fine, you can type in `appium &` and remember the port number, modify your `test.robot` file and try.
(You can use `netstat -lp` to check services working status too.)
### **Step 6 - Run Your First Test Case**
Now use the following template as your first test case to check the environment and server, try debugging yourself according to the error message.
Once you're able to run the template, congratulations, you're ready to build a docker image!
```
*** Settings ***
Documentation Simple example using AppiumLibrary
Library AppiumLibrary
*** Variables ***
${ANDROID_AUTOMATION_NAME} UIAutomator2
${ANDROID_APP} ${absolute path to .apk file}
${ANDROID_PLATFORM_NAME} Android
${ANDROID_PLATFORM_VERSION} ${emulator android version, e.g. 7.1.1}
${DEVICE_NAME} ${your avd name}
${APP_PACKAGE} ${com.yourapp.mobile}
${APP_ACTIVITY} ${com.yourapp.android.MainActivity}
*** Test Cases ***
Should send keys to search box and then check the value
Open Test Application
*** Keywords ***
Open Test Application
Open Application http://localhost:4723/wd/hub
... platformName=${ANDROID_PLATFORM_NAME} platformVersion=${ANDROID_PLATFORM_VERSION}
... deviceName=${DEVICE_NAME} automationName=${ANDROID_AUTOMATION_NAME}
... app=${ANDROID_APP} appPackage=${APP_PACKAGE} appActivity=${APP_ACTIVITY}
```
Note: If you are unable to use command `robot` or `rebot`, please go find the robot path and append it to `.bashrc` file.