Automated coded UI testing become very popular in the past few years. Its allow developers to create tests which are executed directly on UI level and simulate user actions. At the same time HTML5 become a standard for creating universal modern applications which can be hosts in a native browser controls.
New model of creating applications brings a new challenges in the testing fields that's why in this post I want to present my solution (it takes me almost two days to get this working!) which is first step in the process of creation an end-to-end test automation for mobile applications. My solution is prototype of a .NET console application which can be use to control Android emulator and simulate user standard operation like installing app, typing and rotating. This prototype can be use as 'emulator manager' which controls device emulator on which tests are performed - for example by using Selenium.
New model of creating applications brings a new challenges in the testing fields that's why in this post I want to present my solution (it takes me almost two days to get this working!) which is first step in the process of creation an end-to-end test automation for mobile applications. My solution is prototype of a .NET console application which can be use to control Android emulator and simulate user standard operation like installing app, typing and rotating. This prototype can be use as 'emulator manager' which controls device emulator on which tests are performed - for example by using Selenium.
As initial requirements to run the project:
1) Install the latest Java build :( (sorry .NET geeks but it`s required)
2) Install Android SDK
Now we have all environment in place so we can create our emulator device. To do that we need to use Android Virtual Devices Manager tool which is located in the '..\\sdk\tools\' path inside Android SDK folder. To run this tool we need start cmd, navigate to the SDK tools folder and type 'android avd'. Now we can create a new emulator by specifying all setting we need - in the picture below I presented settings for my AVD called 'nexus'. To make sure that all configuration if correct we should run out AVD by calling emulator.exe from '..\\sdk\tools\' for example 'emulator -avd nexus' - this cause our emulator start running and first run might take some time so be patient.
Picture 1. Configuration of a new device emulator. |
Now its time to back to the C# code a present solution which can manage Android emulator through .NET code. Basically solution based on the simple console application which is calling externals console applications:
- emulator.exe - console application which allow to start device emulator by device name with specific parameters.
- adb.exe - console application which allow communication with device emulator; allow to run shell mode directly on the emulator.
Picture 2. Application architecture. |
There is two very important parts inside this solution. First one is the IEmulator interface which expose all functions which are currently supported by the program. The idea is that this solution can be extended to all main three mobile platforms (iOS, Android, Windows) and the IEmulator interface will remain common an contract for all of them so just implementation of each emulator will be different (now only AndroidEmulator class is implemented). IEmulator interface implementation is as follow:
Code Snippet
- /// <summary>
- /// Expose all emulator tasks.
- /// </summary>
- public interface IEmulator
- {
- /// <summary>
- /// Starts the emulator.
- /// </summary>
- /// <returns>Value which determines emulator is ready for use.</returns>
- bool Initialize();
- /// <summary>
- /// Enable shell mode on the emulator.
- /// </summary>
- void StartShell();
- /// <summary>
- /// Checks the emulator is running.
- /// </summary>
- bool IsRunning();
- /// <summary>
- /// Install app on the emulator.
- /// </summary>
- /// <param name="packagePath">Path to the apk package to intsall.</param>
- /// <returns>True if application installed correctly.</returns>
- bool InstallApp(string packagePath = null);
- /// <summary>
- /// Uninstall specyfic package from emulator.
- /// </summary>
- /// <param name="packageName">Specyfic package name to uninstall.</param>
- void UninstallApp(string packageName = null);
- /// <summary>
- /// Run monkey on package.
- /// </summary>
- /// <param name="activitiesCount">Action count.</param>
- void RunMonkey(int activitiesCount = 500);
- /// <summary>
- /// Simulating keypress events on emulator.
- /// </summary>
- /// <param name="codes">Hardware codes array.</param>
- void TypeAsHardwareInput(int[] codes);
- /// <summary>
- /// Change orientation of the emulator.
- /// </summary>
- void ChangeOrientation();
- }
As you can see here, not all options are implemented but the most important functions are on place so let`s have a quick look on its:
- Initialize: function which starts device emulator by calling emulator.exe as separate process and pass AVD name from the configuration file.
- StartShell: using 'adb shell' command from adb.exe tool in separated process to start shell mode on the currently running AVD. Process which hosts shell will remain active until application close.
- IsRunning: function which call 'adb devices' command in separated process from adb.exe tool to check if AVD is currently running.
- InstallApp: install APK file from specific path in local PC by calling 'adb wait-for-device install <apk full-path>' command from adb.exe as separated process. Additional wait-for-device command ensure that emulator is ready to install app.
- UninstalApp: uninstall app from shell level by using package name (ex. com.android.tools.sdkcontroller stored in AndroidInstalledAppName App.config)
- RunMonkey: from shell mode run monkey random actions on the installed package by executing N (activitiesCount) random activities.
- TypeAsHardwareInput: allow to use simulate hardware keys to input any supported input. Under the hood its calling shell command 'input keyevent <key_code>' with key code specified for the platform. Key-codes list is available here.
Code Snippet
- [DllImport("user32.dll")]
- static extern bool SetForegroundWindow(IntPtr hWnd);
- /// <summary>
- /// Change orientation of the emulator.
- /// </summary>
- public void ChangeOrientation()
- {
- var emulatorProcess = Process.GetProcessesByName("emulator-arm");
- if (emulatorProcess.Count() == 0)
- {
- throw new InvalidOperationException("Unable to find emulator process.");
- }
- BringToFront(emulatorProcess[0]);
- WindowsInput.InputSimulator.SimulateModifiedKeyStroke(WindowsInput.VirtualKeyCode.CONTROL, WindowsInput.VirtualKeyCode.F11);
- }
- /// <summary>
- /// Set focus on the process.
- /// </summary>
- /// <param name="pTemp">Process to be focused.</param>
- private void BringToFront(Process pTemp)
- {
- SetForegroundWindow(pTemp.MainWindowHandle);
- }
Now when all functions are on place we can call them in logical order.
Code Snippet
- class Program
- {
- static void Main(string[] args)
- {
- var emulator = EmulatorFactory.GetEmulator(EmulatorType.Android);
- emulator.Initialize();
- emulator.InstallApp();
- emulator.StartShell();
- emulator.ChangeOrientation();
- emulator.ChangeOrientation();
- emulator.RunMonkey();
- emulator.UnistallApp();
- }
- }
I can`t guarantee but I hope that soon I will add next emulator - keep your fingers crossed!
Whole source code of the project is available here.
Thank you
More info: