Friday, January 22, 2016

Introduction to Selendroid

Have you ever developed an Android application and publish it to Google Play? What will you do if you get a user review like -
When you publish an app to Google play, it must be well tested to avoid the potential bugs. There's a ton of test scenarios that should be executed before publishing an app. To save the testing effort, you need a testing tool. One of the best testing tool for Android app is Selendroid.

What is the Selendroid?

Selendroid is a test automation framework for multi type of mobile application: native and hybrid android app and mobile web.

You can write the tests using the Selenium 2 client APIs. Because Selendroid still reuses of the existing Selenium infrastructure for the web
Selendroid is a powerful testing tool. It can be used on emulators and real devices

Why do we need Selendroid?

Selendroid is a great testing tool. But you may still doubt its usefulness.
This section will introduce the important feautures of the Selendroid to answer the question why you need Selendroid.
  • You can test the application under test using Selendroid without any modification of the app. You just need the binary file (APK) installed on the computer. In order to install the binary file on the device, the test app and mobile app must be signed with same sign key
  • Selendroid test app can interact with multiple devices or simulators simultaneously. It's a great advantage of Selendroid. So you can test your app with various android devices to check compatibility.
  • Selendroid can simulate human-user actions on an app, such as touch, swipe, drag and drop on devices
  • You can change the hardware devices (Plug and unplug) during the testing without restarting or stopping the test. Selendroid recognizes the new devices automatically
  • Corresponding to the Android API version up, Selendroid also supports the new Android API (From API 10 to API 19)
  • Selendroid also has some built in inspector tool to help you identify the UI element of application under test. For example the ID of button, text field, text view…

Selendroid architecture

Selendroid is based on the Android instrumentation framework. Selendroid tests are written base on the Selenium Web driver client API, so it support full integration with current Selenium frameworks.
The folllowing figure describe the architecture of Selendroid
Selendroid contains 4 major components:
  • Web Driver Client – The Java client library based on Selenium. This library should be installed on the computer (which is used to develop the test cases)
  • Selendroid-Server – The server which runs be in the app under test on Android device or simulator. This is the main components of Selendroid architecture
  • Android Driver-App - A built in Android driver, Web View app to test the mobile web.
  • Selendroid-Standalone – This component is used to install the Selendroid server and the application under test (AUT)

Getting started with Selendroid

You already known the importance of the Selendroid. Now let's get our hands dirty with Selendroid.
There're 3 steps should be done before first test with Selendroid

Setting up Selendroid environment

Selendroid can work on Window, Linux and Mac OS. In this tutorial, we will setup Selendroid in Window OS.
Before using Selendroid, you need install following package first
  • Java SDK (minimum 1.6)
    You must accept the license agreement and download the java installer (Choose x64 or x86 base on your OS)
    Download and install the Java SDK as normal software
  • Latest version of Android SDK
  • Your computer must have atleast one Android Virtual Device (AVD), or a real Android device plugged into the PC.
  • Selendroid Standalone with dependencies, Selendroid Client and Selenium Client
  • Eclipse software
  • Set up the JAVA_HOME and ANDROID_HOME
    Step 1) On Window, right click Computer -> Properties -> Advance System Setting
    Step 2) System Properties window display, select tab Advanced -> Environment Variables
     
    Step 3) The Environment window display, click New -> Enter a variable ANDROID_HOME as following
     
    The variable value is the path to android-sdks which you already installed.
    Find the system variable Path -> Edit -> Add the following line after the current line
    Similar to ANDROID_HOME, add new variable JAVA_HOME with value as below
    The value is the Path to your Java JDK installation
    Step 4) Restart your PC -> Done

How to launch Selendroid

Step 1) Getting an application under test
You can use existing Selendroid test app to check that how Selendroid works (Link to sample application under test)
Once download is complete, copy this APK and the above Selendroid Standalone jar file to a folder with name "Guru99"
Step 2) Launch the Selendroid
Open the terminal on Windows & navigate to the folder Guru99 created in step 1.
Run the following command
The output will display as following

After running this command, Selendroid-standalone http server starts! The default port number of this server is 4444. All hardware device as well as Android Virtual Device will be scanned and recognized automatically. Selendroid will identify the Android target version and device screen size.
To check the Android target version as well as the device information, you can launch following URL on browser:

Selendroid basic command

This section introduce you some basic Selendroid-Standalone command line. You may use them to setup the Selendroid testing environment
  1. Setting port of Selendroid
    The default port of Selendroid is 4444. But you can change to other port by adding parameter to the command to launch Selendroid
    Parameter: -port [port number]
    For example:
    In above command, 5555 is the new port.
    So the URL to check the Android target version is changed to: http://localhost:5555/wd/hub/status
  1. Specify the location of the application under test (Binary APK file). Selendroid often required the absolute path for this file
    Parameter: -app [file path]
    For example:
    In above command, the Selendroid automatically find the binary file base on the "C:\Guru99App.apk" to get the information of the application under test.
    Check the URL http://localhost:4444/wd/hub/status, you will see this information
  2. Change the port the Selendroid uses to communicate with instrumentation server. Selendroid uses the port 8080 as the default
    Parameter: -selendroidServerPort [port number]
    Example
    The port now is changed to 9000
  3. Change the timeout to start emulators. The unit is milliseconds.
    Parameter: -timeoutEmulatorStart
    By default Selendroid will wait 300,000 milliseconds until the emulator starts. You can change to new timeout (200,000 ms) by command
    After this time our expired, if the emulator cannot start, the Selendroid will throw the exception error (Error occurred while looking for devices/emulators.) then stop running
  4. When you start the Selendroid command on terminal, you will see a log printed out on the screen. You can change the type of log you see by adding following parameter
    Parameter: -logLevel [type of log]
    The log level values are: ERROR, WARNING, INFO, DEBUG and VERBOSE. Default: ERROR.
    For example, set Selendroid to print the WARNING log only, you can use this command
    The Selendroid only print the WARNING log

Start your first test with Selendroid

This section is a step by step guide to create your first test script using Selendroid
Suppose we have an Android application under test name Guru99App. The application include a text field and a button name "Show Text".
We need execute following test case using Selendroid

Test Cases 
Condition 
Expected output: 
  1. Launch the application
  2. Enter a text "Guru99 Test" to the text field
  3. Press "Show Text" button
The binary of the application under test is available
Device is connected to PC 
The text "Text Show here" is change to the text
which user enter in text field

Step 1) Create a Java project in Eclipse
Step 2) Add selenium and Selendroid jar file in eclipse environments
Right click Guru99Test Project -> Build Path -> Add External Archives

Navigate to the folder which stored the jar files
There're 3 jar files should be added
  • selendroid-client-0.10.0.jar : Selendroid java client library
  • selendroid-standalone-0.11.0-with-dependencies :Selendroid standalone server library
  • selenium-java-2.40.0.jar : Selenium Web Driver library
Select all -> Choose Open to add jar file to the project


Step 3) after adding the above library, those libraries will be added to the Reference Libraries of the test project. Tester can use the APIs of those library to develop the test program
Create package "com.guru.test"and add java file "Guru99Test.java" like below
Right Click Guru99Test -> New -> Package
Type com.guru.test to the Name field on New Java Package dialog à Finish

The Eclipse will create a list folders and sub folders like this in the source code structure

Step 4) Install TestNG for Eclipse

In Eclipse, Help -> Install New Software, in the Install Dialog, click Add and enter the following
Press OK -> Next to install the TestNG
Step 5) Copy the Guru99App.apk to the folder of Test App

Step 6) Get the ID of application under test.
Suppose we have an APK file name Guru99App.apk. Follow the step which is described in previous section, execute the command on terminal
Open following link in browser
The information of device is displayed, copy the appId value "com.guru99app:1.0"
Step 7) Open the file Guru99Test.java (in sample code) and change as following
To create a new test session with Selendroid, you have to provide the app id in the format: com.guru99app:1.0. This app Id can is identified in step 6. If you don't set the app Id matching the Android device, the test session will throw error and will not start.
After initializing the found device, Selendroid creates a customized selendroid-server and installs the Selendroid server on it
Selendroid also installs the application under test and starts the selendroid-server on device
After initzalizing the test session succesfully, the test command is started executing on device. (Such as enter text, press button…). If the test app ends the test session, the emulator will stop automatically
Step 8) Start the new test session
Launch the Selendroid server using following command on terminal like step 6
After the Selendroid Server started, open the sample test project Guru99test on Eclipse, set a breakpoint at line 77 on file Guru99Test.java by double click to the line 77 -> A dot will display like below
Start a test session by right Click to Guru99Test project -> Debug As -> TestNg Test.
A test session will start like below
Step 9) Get the Id of GUI element of application under test
After test session start successfully, open browser, navigate to the URL http://localhost:4444/inspector
You will see the Application Under Test is launched like below
Use mouse hover to each UI element of AUT (Button, TextField, Text Label), the ID of each element will hightlight on the right pane
After this step, you can get the ID of each above UI element
  • Button Show Text ID : "btnShow"
  • Text Field ID: "edtText"
  • Label Text ID: "txtView"
Those IDs will be used in next step
Step 10) Enter the test program like below
A test program using Selendroid includes 3 sections
Setup Test:
Following is the code for setup test, it will set up the condition for a test session.In case of an error, the Selendroid will throw the exception and the test app will stop.
The code include the commments to explain each the statement.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.guru.test;
         
 import io.selendroid.SelendroidCapabilities;
        import io.selendroid.SelendroidConfiguration;
        import io.selendroid.SelendroidDriver;
        import io.selendroid.SelendroidLauncher;
        import io.selendroid.device.DeviceTargetPlatform;
         
        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.testng.Assert;
        import org.testng.annotations.AfterSuite;
        import org.testng.annotations.BeforeSuite;
        import org.testng.annotations.Test;
         
 /**
         * @author Guru99 Test App using Selendroid
             * Application under test: Guru99App
         *
         */
        public
                class Guru99Test {
                 
                     //Declare web driver variable
            private WebDriver driver;
                        
    
            /**
             * Setup the environment before testing
             * @throws Exception
                 */
            @BeforeSuite
            public
                    void setUp() throws Exception {
                             
        //Start selendroid-standalone during test
                SelendroidConfiguration config = new SelendroidConfiguration();
                     
              // Add the selendroid-test-app to the standalone server
                config.addSupportedApp("Guru99App.apk");
                     
                  
                //start the standalone server
                SelendroidLauncher selendroidServer = new SelendroidLauncher(config);
                    selendroidServer.launchSelendroid();
                 
               // Create the selendroid capabilities
                SelendroidCapabilities capa = new SelendroidCapabilities();
                     
         
               // Specify to use selendroid's test app
                capa.setAut("com.guru99app:1.0");
                    
               // Specify to use the Android device API 19
                capa.setPlatformVersion(DeviceTargetPlatform.ANDROID19);
                     
                // Don't request simulator, use real device
                capa.setEmulator(false);
                     
          
                //capa.wait(10000000);
                 
                // Create instance of Selendroid Driver
                driver = new SelendroidDriver(capa);
                            
            }
        
Execute Test
Below is the code to execute a test. The code includes the commments
Here are the test steps again
  1. Enter the text "Hello Guru""
  2. Click Show Text Button
  3. Wait a while
  4. Verify that the app display the text as user enter to text field (Ex. Display the text "Hello Guru")
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 /**
             * Start execute the test case
             * 01. Enter the text "Selendroid" to the textfield
             * 02. Press OK button
             * @throws Exception
                 */
            @Test
            public
                    void selendroidTest() throws Exception {
                                     
                // Print the log
                System.out.print("Start executing test");
                             
                // Find the input text field on screen
        // The id of this text field was get from step 9
                WebElement inputField = driver.findElement(By.id("edtText"));
                             
                // Verify that the text field enabled so user can enter text
                Assert.assertEquals("true", inputField.getAttribute("enabled"));
                             
                // Enter a text to text field
                inputField.sendKeys("Hello Guru");        
             
         // click Show Text button
        // The id of this button was get from step 9
                WebElement button = driver.findElement(By.id("btnShow"));
                            button.click();
                 
                // Delay time to take effect
                Thread.sleep(5000);
                 
                //Find the label "Text Show Here" on screen
        // The id of this label was get from step 9
                WebElement txtView = driver.findElement(By.id("txtView"));
                             
                //Get the text display on screen
                String expected = txtView.getText();
                 
        // Verify that the text which user enter on text field is same as text display on screen
                Assert.assertEquals(expected, inputField.getText());        
         
     }

Finish Test
Following code will complete the test by stopping the Selendroid driver.
?
1
2
3
4
5
6
7
8
9
 /**
             * Stop the Selendroid driver
             *
             */
            @AfterSuite
            public
                    void tearDown() {
                            driver.quit();
                }
You can see the detail in sample code included in this article.

Step 10) Connect Android device to the PC via USB cable. Points to observe -
  • Please make sure the device has no screen lock configured.
  • Devices must be plugged in via USB to the computer that the selendroid-standalone component is running on.
  • The device should install at least Android Target Version API 10
Step 11) Run the Test App: Right click Guru99test -> Run as -> TestNG test

Step 10) The Script start executed as following
Step 12) After test finishes execution, TestNG auto generates the test report as following
Good Job, you are done the test now.
Summary

Summary

  • Selendroid is a very powerful tool for testing Android native app, hybrid app as well as the web app.
  • It can be used on real devices as well as the simulator.
  • It also allows you to run tests in parallel by running test on multi devices.
  • The entire Selendroid suite is comprised of four components:
    • Web Driver client,
    • Selendroid-Server,
    • Android Driver App
    • Selendroid-stand alone
  • To use Selendroid you need Java JDK, Android SDK and Eclipse installed.

Your First iOS test program using UIAutomation framework

To guarantee the quality of your iOS application, you should follow the Test-Driven development  processes shown in below figure
Test-Driven Development (TDD) is a testing model which is applied to iOS application testing. In this model, tester has to follow 4 phases below:
  • Design: Figure out what you want to test, design your test cases
  • Test: Run all tests and see if test cases fail
  • Implement: Revise your code, fix bugs which cause the test to fail
  • Test again: if Fail,if you roll back to the design. If all test cases Pass, the code meets the entire tested requirement.

Setting up test environment

In order to create iOS Test Program, you need a Mac Book.Your Mac has already installed:
  • OS X. Operating system for Mac PC
  • Xcode IDE, a development tool for iOS
  • Automated Testing framework (UIAutomation, OCUnit..)
  • iOS SDK 4 (or higher)

Create iOS test program using UIAutomation framework.

  1. Launch Instruments
Open XCode ->Open Developer Tool ->Instrument
  1. Add Automation Instrument
In Instruments window, select Automation Instrument
To create a test script, you either record a test scenario or you program it manually.
  1. An instrument is launching, and Stop recording immediately. If you want to start the record, press Red button
  1. In the Scripts window, click Add > Create to create a new script
  1. Choose the target
You're in the Trace window, use the Choose Target pull down to navigate to the debugging version of you App.
In this case, I will use Apple's sample SimpleDrillDown simple app as an application under test. It has GUI below.
  1. Start record your script
Record your script by hitting the record button at the top or bottom of the tool.
Now you can do some UI action on your application under test, and your script is recorded.
  1. See your script
To see your script, hit the Trace Log/Editor Log drop down and switch to script log view.
You will see your recorded script

  1. Play your script
Press play button, the script runs, and you can stop it after logs appear.

Create iOS test program using OCUnit framework
  1. Start Xcode IDE, Add Unit Test Bundle target
  1. Write the name of the new Unit test Bundle as figure above, then click Finish
  2. Make Unit Test active target
  1. Add group for test classes
  1. Add Unit test class
  1. Now start your implement
OCUnit uses Objective-C language to create test program. Developer must have knowledge about this language.

Source code examples

This article includes some Source Code examples. They help you to understand the tutorial more clearly and quickly.
UIAutomationSample Test script for UIAutomation demo.

Getting started with iOS testing

Why iOS testing ?

First released on June 29, 2007, iOS is the name of Apple’s platform for mobile applications. Unlike Android, Apple does not license iOS for installation on non-Apple hardware. iOS and iOS applications are only installed on Apple devices. Your app must be compatible with 4 types of devices and iOS versions.
This is the common question when developer create an iOS application

It doesn't matter how much time you invest in design and implementation, mistakes are inevitable, and bugs will appear. There are some common bugs on iOS application. As shown in below figure.
  1. Application crashing
     One of the most frustrating problems, when using Apple's devices, is that an application may crash frequently during execution. Many times the app crashes because there's some bugs or memory leak in apps.
  1. Application incompatibilities
     Your iOS application may run perfectly on current iOS version, but if iOS is upgraded it may not work due to incompatibility issues.
  1. Security vulnerability
     Security vulnerability in iOS allow the hacker to attack your iOS devices, steal your private information. Until now, severe iPhone security vulnerabilities are discovered in different iOS versions.
  1. Memory leaks
     Memory leaks are blocks of allocated memory that the program no longer uses. Memory leaks cause your iOS application to crash. They are bugs and should always be fixed.

iOS testing MindMap

As shown in the above figure, iOS testing MindMap shows all the items which tester should consider when conduct testing on iOS.

iOS Testing Strategy

The below figure introduces some common types of iOS testing strategy.

Automated testing

Automated testing is the most advantages of iOS testing. It enables you to detect the bug and performance issues quickly. The benefits of automated testing as shown below:
  • Automated testing can run on multiple devices, saving your time
  • Automated testing can target SDKs. You can run test on different SDK versions
  • Automated testing increase your productivity of testing, save your cost of software development
  • There's many open source Testing frameworks support automated testing on iOS

Unit testing with OCUnit

When the original iOS SDK was released, it lacked unit testing capabilities. So Apple has brought back theOCUnit unit test solution in iOS SDK version 2.2.
OCUnit is a testing framework for C- Objective in the Mac OS. The biggest advantages of OCUnit framework are the tight integration into XCode development environment as shown below
Some of benefits of OCUnit are shown in below figure.

UI Testing with UIAutomation


UI Automation is a JavaScript library provided by Apple Inc, which can be used to perform automated test on real devices and on iOS Simulator. This framework is added to iOS SDK4.0. Using UI Automation, you can automate testing the application not only on simulator but also real device.
UIAutomation bring you these benefits:
  • Reduce effort on manual testing
  • Use less your memory to execute all your tests
  • Simplify your UI testing procedure ( just push one or three buttons and run full your test suites)
UIAutomation instrument works from scripts, which are written in JavaScript. It simulates user events on target iOS application.
UIAutomation Cons vs. Pros
 
Pros
Cons
1.
Good support for gesture and rotation
It's not open source, less support from the developer
2.
Can run UIAutomation tests on the device, not only simulator.
Can’t integrate with other tools extremely well
3.
Developed by JavaScript, it is a popular programming language.
 
The above figure represents some common classes in UIAutomation framework.
  • The UIAElement class is the super class for all user interface elements in the context of the Automation
  • The UIATarget class represents the high-level user interface elements of the system under test
  • The UIALogger class provides test and error information on retrieval functionality
  • The UIAActivityViewclass allows access to, and control of, activity views within your app.
  • The UIAActionSheet class allows access to, and control of, action sheets within your app.
  • User Event Action
    • UISlider class
    • UIAButton class
    • UIAKey class
    • UIAKeyboard class

Other Automated testing frameworks

  • MonkeyTalk: A tool for automated testing of iOS, Android, HTML5 and Adobe applications. It is an integrated environment for managing and running test suites
  • Frank: Automated Acceptance Test framework for iPhone and iPad
  • KIF : is an iOS integration test framework. It allows for easy automation of iOS apps by leveraging the accessibility attributes that the OS makes available for those with visual disabilities.

Manual testing

Exploratory Testing

It is a testing without a formal test plan. Exploratory Testing is low cost testing method, but it can miss the potential bugs in your iOS application.
Exploratory Testing Cons vs. Pros
 
Pros
Cons
1.
Less preparation is needed, early detect serious bugs
Requires high skill of tester
2.
Don't need test plan speed up the bug detection
Test coverage is low. It does not guarantee that all your requirements are tested
3.
Most bugs are discovered early by some sort of exploratory testing
Lack of testing documentation

User Testing

User testing is a type of manual testing on iOS. The purpose of this testing is to create a better apps, not onlybug-free apps. Below figure shows 4 types of User Testing

Concept testing:

Evaluate user response to an application idea before release to the market. Procedures of concept testing on iOS are described as below

Usability testing

Usability testing is a test how easy to use your iOS application. In iOS testing, usability test could be recordedto remember or to share with others.
There is some tools support usability testing on iOS.
Magitest, a simple iOS usability testing for sites and apps.
Delight.io, this tool can capture real user interaction on your iOS apps

Beta testing

Beta testing is the integration testing with real data to get final feedback from users. To distribute your apps for beta testing, you have to follow steps below.
-Pre-condition: If you are beta testing a final candidate for a release, be sure to validate the app before distributing it to testers.
-Find tester via service: you collect device IDs from testers and add them to Member Center
-Create ad-hoc distribution: Ad Hoc distribution allows the tester to run your app on their device without need Xcode. This step includes 2 sub-steps
  • Create distribution certificates
  • Create Ad hoc provisioning profiles
-Solicit feedback from tester: Tester conduct testing and send bug reports to you. After your app is released, you can get the reports from iTunes connect.

A/B testing

A/B testing is one of the most powerful ways to evaluate the effectiveness of your iOS app. It usesrandomized experiments with two devices, A and B.
A/B testing includes 3 main steps
  • Configure a test: Prepared 2 versions of your iOS app (A & B) and test metric
  • Test: Test 2 iOS apps versions above on devices simultaneously.
  • Analyze: Measure and select better version to release
Following tools support A/B testing on iOS.
  • Arise : A/B testing for both iOS and Android. It can be integrated into your iOS app and make the testing process more quickly.
Best practices for A/B testing
  • Define the target of your test. Any test is useless without a target.
  • Watch end users use your app the first time
  • Run one test only per update. It saves your time when conduct testing
  • Monitor your test carefully. You can learn experiences from your test by monitoring it.

iOS testing Best practise

Here are some tips you should know when organizing the testing of your iOS application
  1. Test the application on a real device to get real about performance
  2. Improve your testing methods, because traditional testing methods are no longer sufficient to coverage all tests on iOS testing
  3. Using console log to test iOS application. This is an iOS feature includes information from every application on the device.
  4. Document application bugs using built-in screen short command. It helps developer understand how the bugs occur.
  5. Crash reporting is useful tools when test your application. They can detect crashes and log detail so you can investigate the bugs easily.

MYTHS about iOS testing

This section examines a few popular myths and realities of iOS testing
Testing application on iOS and Android are the same thing
iOS and Android are two platforms were developed by Apple Inc and Google. They are totally different. Etc test environments, test frameworks, programming languages.
Test application on iOS Simulator is enough
iOS Simulator is not strong enough for testing an app. Because iOS Simulator has some limitations:
  • Hardware limitations (Camera, Microphone input, Sensor)
  • Your app's UI may appear to run both faster and smoother than on a device
  • API Limitations
  • Some frameworks unsupported (Media Player, Store Kit, Message UI..)
Everyone will download my apps on app store because it has many features
The more features your application has, the more bugs you could get. No user will download your application if it still has many defects.

Your First Test with Android Testing Framework

In this article, you will start with your first Android Testing. To guarantee quality of your Android application, you should follow the procedure below
  1. Design test specification
  2. Develop test program
  3. Execute test case on target device
  4. Collect test result
Android application testing procedure

STEP 1) Design test specification

  • This is the first step to test your application.In this step you Define target to be test. In your Android application, there's many targets need to be tested such as UI, Activity, components, services. Clearly defining the target in your application will help achieve wide test coverage.
  • Plan the test types should be conducted (Unit test, Functional test, System test).
  • Design test cases for maximum coverage but minimize number of test cases. The more code is tested more are chances of early bug detection.

STEP 2) Write TEST program

This section guides you how to write an Android test program using Android JUnit Test and Robotium. Assume that you have already developed an Android program name HelloAndroid. This program has some functions described below:
  • Display a text "Hello world!" on screen.
  • Display a message HelloAndroid when user press "Start" button
HelloAndroid Application

System Requirements

  • Android platform comes with pre-integrated JUnit 3.0 framework.
  • In order to create Android Test Project from Eclipse, your computer must have installed:
    • Latest version Android Platform (currently Android 4.2.2)
    • Eclipse ADT plug-in (latest version is ADT  22.0.4)
You can download Eclipse IDE with built-in ADT (Android Developer Tools). It includes the essential Android SDK components and a version of the Eclipse IDE .
For the Robotium testing framework, you need to down Robotium library from Robotium webpage.

Create Android Test Project

  • Click File -> New -> Other
  • Choose: Android -> Android Test Project as per below figure -> Choose Next
Create new Android test project
Write name of your test project. As naming convention, your test project should be name "HelloAndroidTest"
Add test project name base on naming convention
Choose target application under test. In this case, this is HelloAndroid click Finish
Choose target application under test

Create Test Suites

Base on your test specification, you started to create test suites for your test program. You can choose various Testing framework. In this tutorial, I choose standard Android testing frameworkActivityInstrumentationTestCase2.  You have to add Robotium library file to a libs directory in your project folder in case you want to test with Robotium framework. (You create lib folder in your project folder).
A test case defines the fixture to run multiple tests. To define a test case, you must follow the program structure below:
  • Implement a subclass of TestCase.
  • Define instance variables that store the state of the fixture
  • Initialize the fixture state by overriding setUp()
  • Clean-up after a test by overriding tearDown().
Test program's structure

Adding Test Cases

  • In the same package with TestSuite, we create TestCase classes
  • To test certain activity i.e. HelloAndroid, create a test case extentActivityInstrumentationTestCase2
  • In this class, tester can obtain testing activity through getActivity() method .
  • You can  freely create test for a testing activity by create method with name "test + original Method Name"
  • In test method, tester can use Android JUnit function to compare the actual value and expected value. These methods are shown in below.
Example methods of Robotium and Android Testing framework
These test suites above verified that Application GUI must display a text "Hello World!", and contains a button name "Start".

STEP 3) Run Test

After you finish writing your test program, run the test using the steps below
  • Connect Android device to your PC (or start Emulator in case you don't have real device).
  • In your IDE, right click àRun asàAndroid Unit Test
Running test program
Besides running test on IDE, you can run test on command line. In this test program, test package iscom.example.helloandroid.test . In Linux  terminal, you can use following command to run all test in this package:
$ adb shell am instrument -w -e package com.example.helloandroid.test

STEP 4) Get test result

After test executes, you get test results .
In this test program, 4 test methods are executed.  In this case, all test cases are passed.
Test result output in case all test cases passed
In case test case fails, the output is display and show you which test cases failed
Test result output in case all test cases failed

Source code examples

This articles include some Source Code examples which help you to understand the tutorial more clearly and quickly catch up the technical knowledge

Evil Twin attack

Evil Twin Attack is attack is frequently carried upon wireless access points with malicious intentions. This attack happens when...