DevQAExpert Solution Pvt. Ltd.

A Sample Automation Script for Calculator App

Introduction:

An effective solution for automating UI tests for Android applications is the Espresso Testing Framework. It enables developers to create effective and trustworthy tests to guarantee the performance and quality of their Android applications. In this article, we’ll show you how to set up the Espresso Testing Framework and run a sample automation script for a calculator app. Let’s start now!

 

Part 1: Setting Up Espresso Testing Framework:

Step 1: Install Android Studio

To begin, you need to install Android Studio, Google’s official IDE for Android development. Android Studio comes bundled with all the necessary tools and SDKs required for Android app testing with Espresso.

Step 2: Create an Android Project

After installing Android Studio, create a new Android project or open an existing one you wish to test. Ensure that your project is built and ready for testing before proceeding.

Step 3: Add Espresso Dependencies

In the project’s build.gradle file, add the necessary Espresso dependencies to enable testing. Add the following lines inside the dependencies block:

 

androidTestImplementation ‘androidx.test.espresso:espresso-core:3.4.0’
androidTestImplementation
‘androidx.test.espresso:espresso-contrib:3.4.0’
androidTestImplementation
‘androidx.test.espresso:espresso-intents:3.4.0’

 

Step 4: Create a Test Class

Create a new package for your Espresso test classes. Inside the package, create a new Java class and extend it from androidx.test.espresso.Espresso or androidx.test.espresso.intent.Intents.

Step 5: Launch the App for Testing

To test the Calculator app, make sure it is installed on the device or emulator. Espresso requires the app to be launched before the tests begin. You can use the @Before annotation 

to set up the necessary conditions before each test.

 

Part 2: Execution of a Sample Automation Script for Calculator App:

For this demonstration, let’s create a simple automation script to test the basic arithmetic operations of the Calculator app. We will verify addition, subtraction, multiplication, and division functionalities.

Step 1: Identify UI Elements

The first step in writing our test script is to identify the UI elements we want to interact with. For example, the Calculator app might have buttons for digits (0-9), operators (+, -, *, /), and an output display to show the results.

Step 2: Write Test Cases

Next, let’s write the test cases using Espresso’s API to interact with the UI elements and verify the expected outcomes. Here’s a sample test script for the Calculator app:

 

import androidx.test.espresso.Espresso;
import androidx.test.espresso.intent.Intents;
import androidx.test.espresso.intent.rule.IntentsTestRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class CalculatorAppTest {

    @Rule
    public IntentsTestRule<CalculatorActivity> activityRule = new IntentsTestRule<>(CalculatorActivity.class);

    @Before
    public void setUp() {
        // Set up any preconditions here (if required)
    }

    @Test
    public void testAddition() {
        // Perform addition
        Espresso.onView(withId(R.id.button_1)).perform(click());
        Espresso.onView(withId(R.id.button_plus)).perform(click());
        Espresso.onView(withId(R.id.button_2)).perform(click());
        Espresso.onView(withId(R.id.button_equals)).perform(click());

        // Verify the result
        Espresso.onView(withId(R.id.result_display)).check(matches(withText(“3”)));
    }

    @Test
    public void testSubtraction() {
        // Perform subtraction
        Espresso.onView(withId(R.id.button_5)).perform(click());
        Espresso.onView(withId(R.id.button_minus)).perform(click());
        Espresso.onView(withId(R.id.button_2)).perform(click());
        Espresso.onView(withId(R.id.button_equals)).perform(click());

        // Verify the result
        Espresso.onView(withId(R.id.result_display)).check(matches(withText(“3”)));
    }

    @Test
    public void testMultiplication() {
        // Perform multiplication
        Espresso.onView(withId(R.id.button_3)).perform(click());
        Espresso.onView(withId(R.id.button_multiply)).perform(click());
        Espresso.onView(withId(R.id.button_4)).perform(click());
        Espresso.onView(withId(R.id.button_equals)).perform(click());

        // Verify the result
        Espresso.onView(withId(R.id.result_display)).check(matches(withText(“12”)));
    }

    @Test
    public void testDivision() {
        // Perform division
        Espresso.onView(withId(R.id.button_8)).perform(click());
        Espresso.onView(withId(R.id.button_divide)).perform(click());
        Espresso.onView(withId(R.id.button_2)).perform(click());
        Espresso.onView(withId(R.id.button_equals)).perform(click());

        // Verify the result
        Espresso.onView(withId(R.id.result_display)).check(matches(withText(“4”)));
    }
}

 

Step 3: Run the Tests

Now that we have the test script ready, you can run the tests using Android Studio. Open the “Run” menu and select “Run ‘CalculatorAppTest’.” Espresso will execute the tests on the Calculator app, and you will see the test results in the Android Studio’s test console.

 

Conclusion:

The Espresso Testing Framework provides developers with a robust and efficient way to automate UI tests for Android applications. Developers can guarantee the dependability and performance of their apps by following the setup procedure and creating insightful test scripts. Using the Calculator app as an example, we set up the Espresso framework, created a test class, and ran a sample automation script in this blog. You may improve the functionality and user experience of your Android apps and give your customers top-notch goods by integrating Espresso into your testing process.