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.
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 |
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 | Corresponding string value |
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.
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.
There are two functional scenarios I want to test through automation:
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
(){
});
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"
;
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);
})
]
}
};
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);
It's time to execute the tests via the Test Runner.
The Results tab loads when execution finishes. All tests passed!
I can additionally publish results to the Telerik Platform for storage and to share with colleagues, managers, and stakeholders.
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.
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.