Telerik blogs

Last time we automated a native iOS application with Telerik Mobile Testing. Now it's time for Android. Once again I will make it testable, develop a test plan, write and execute tests, and publish results to the Telerik Platform.

The App-Under-Test

This case study uses a simple application called My First App, created from this Android Developers tutorial. A message is entered and sent on the main screen, and that message is displayed on the second screen.

Main screen
Message entered
Second screen
Main screen Message entered Second screen

 

Prepare App for Automation

There are three Android widgets I plan to interact with: Button, EditText, and TextView. I plan to identify the Button based on its text property, so no additional steps are needed there. I can identify each TextView by index, and the EditText has a unique hint property.

EditText hint property
strings.xml
EditText hint property Corresponding string value

 

Add Testing Extension

I created a duplicate testing flavor and added the Mobile Testing extension based on these instructions. Next I deployed the new testing build variant to a Samsung Galaxy S III.

Download Companion App

I need the Mobile Testing companion app to test a native Android app. I downloaded it from Google Play on the device. Follow these instructions to install it to the emulator.

Create a Test Plan

There are two functional scenarios I want to test through automation:

  1. Enter a message, send it, and verify the second screen displays it.
  2. Send a message, go back from the second screen, and verify the main screen displays.

Write Tests

Create JavaScript File

I started by creating a new JavaScript file. Code may be written to that file using any IDE or text editor. I prefer Sublime Text.

Start with the main spec method:

spec(function(){
  
});

 

Query Repository

Next comes the Query Repository. I defined the queries for all the elements here and assigned each to a variable.

var queries = {
 
    messageTextInput: {'class': 'android.widget.EditText', 'properties': { 'hint': 'Enter a message'} },
    sendBtn: { 'class': 'android.widget.Button', 'properties': { 'text': 'Send'} },
    messageTextView: {'class': 'android.widget.TextView', 'index': 2 },
    titleTextView: {'class': 'android.widget.TextView', 'index': 0 }
 
};

 

Here I defined a few static variables used later in the test steps.

var message = "Hello world";
var title = "My First App";

 

Step Repository

The bulk of the code comes next via the Step Repository. Each step is labeled with a friendly name, followed by platform-specific operations. This example only uses Android operations.

These steps perform the actions and verifications against the application's elements, which are referenced via the query repository. You'll see things like launching the app, tapping buttons, setting text, and verifying text views.

var stepRepository = {
    "Launch app": {
        'android': [
            android.launch('com.telerik.myfirstapp')
        ]
    },
 
    "Enter message" : {
        'android': [
            android.setText(queries.messageTextInput, message)
        ]
    },
 
    "Tap Send": {
        'android': [
            android.tap(queries.sendBtn)
        ]
    },
 
    "Tap Back": {
        'android': [
            android.pressBackButton()
        ]
    },
 
    "Verify message": {
        'android': [
            android.getPropertyValue(queries.messageTextView, 'text', function (contents) {
                assert(contents).equals(message);
            })
        ]
    },
 
    "Verify on main screen": {
        'android': [
            android.getPropertyValue(queries.titleTextView, 'text', function (contents) {
                assert(contents).equals(title);
            })
        ]
    }
 
};

 

Test Suite

Finally comes the Test Suite. A suite is a group of tests, labeled with a friendly name, that calls the step repository. Each test is also labeled and contains steps, referenced by their friendly names from the step repository.

Based on the test plan I created earlier, I simply mix and match steps to create each test.

describe("Messages", function(){
 
    test("Input, send, and verify message", function(){
        step("Launch app");
        step("Enter message");
        step("Tap Send");
        step("Verify message");
    });
 
    test("Send message and return to main screen", function(){
        step("Launch app");
        step("Enter message");
        step("Tap Send");
        step("Tap Back");
        step("Verify on main screen");
    });
 
}, stepRepository);

 

Execute Tests

It's time to execute the tests via the Test Runner.

  1. Click Settings and change the Spec Folder path to where your test resides.
  2. Click Save and notice the suite and tests are displayed by their friendly names.
  3. Launch the Mobile Testing app on the device and connect it.
  4. Click Refresh Agents.
  5. Click Run Tests.

Tests

Publish Results

The Results tab loads when execution finishes. All tests passed!

Results

I can additionally publish results to the Telerik Platform for storage and to share with colleagues, managers, and stakeholders.

Conclusion

That's it! The handy query and step repository method makes it easy to add new queries and tests when I add new elements and features to my application.


About the Author

Anthony Rinaldi

Anthony Rinaldi combines his experience in test automation and quality assurance with his passion for mobile devices and platforms. He is an avid CrossFitter, soccer player, and music fan.

Related Posts

Comments

Comments are disabled in preview mode.