Showing posts with label android uiautomator. Show all posts
Showing posts with label android uiautomator. Show all posts

Monday, April 27, 2015

Android new testing library update

Android new testing library update

                      
          The new Android Testing Support Library provides an extensive framework for testing Android apps with espresso and Android Junit Runner with Android Studio. Now you can be able to run test cases from Android Studio with all connected devices. This library provides a set of APIs that allow you to quickly build and run test code for your apps, including JUnit 4 and functional user interface (UI) tests. You can run tests created using these APIs from the Android Studio IDE or from the command line. There are 3 major updates 1) Android Junit Runner. 2) Espresso. 3) Uiautomator 2.0


Android Junit Runner: 

         The Android Junit Runner class is a JUnit test runner that lets you run JUnit 3 or JUnit 4-style test classes on Android devices, including those using the Espresso and UI Automator testing frameworks. The test runner handles loading your test package and the app under test to a device, running your tests, and reporting test results. This class replaces the Instrumentation Test Runner class, which only supports JUnit 3 tests.

Uiautomator 2.0:

        The UI Automator testing framework provides a set of APIs to build UI tests that perform interactions on user apps and system apps. The UI Automator APIs allows you to perform operations such as opening the Settings menu or the app launcher in a test device. The UI Automator testing framework is well-suited for writing black box-style automated tests, where the test code does not rely on internal implementation details of the target app.

 Espresso:


                      
          The Espresso testing  framework provides a set of APIs to build UI tests to test user flows within an app. These APIs let you write automated UI tests that are concise and that run reliably. Espresso is well-suited for writing white box-style automated tests, where the test code utilizes implementation code details from the app under test. 


For more information Read More

Saturday, November 24, 2012

Android uiautomater sample test case with assert verification

I have spent a couple days testing out the new UiAutomator API and thinking that it is like a bridge between android instrumentation and monkeyrunner. By using this APIs, you can develop test case with minimum knowledge and less effort. You don’t need to do any pre-setup like package boundaries and configuring manifest file permissions. 

While designing a test case using Robotium, You must need to include some critical steps like extending from ActivityInstrumentationTestCase2 and passing in the name of the target class creating the constructor that takes no arguments.

But here you don’t need to do anything. You can just create a normal Java project with default folder structure and build the test.jar file with project dependency UIAutomater.jar file

Here as the sample UIAutomater Sample Test case.

public void testDemo() throws UiObjectNotFoundException {

// Press home button.... Don’t care about where your device status or previous test case where you opened.
        getUiDevice().pressHome();

 // open the All Apps view
        UiObject allAppsButton = new UiObject(LauncherHelper.ALL_APPS_BUTTON);
        allAppsButton.click();

// clicking the APPS tab
        UiSelector appsTabSelector =
                new UiSelector().className(android.widget.TabWidget.class.getName())
                        .childSelector(new UiSelector().text("Apps"));
        UiObject appsTab = new UiObject(appsTabSelector);
        appsTab.click();

// Clicking the Settings
        UiScrollable allAppsScreen = new UiScrollable(LauncherHelper.LAUNCHER_CONTAINER);
        allAppsScreen.setAsHorizontalList();
        UiObject clockApp =
                allAppsScreen.getChildByText(LauncherHelper.LAUNCHER_ITEM, "Clock");
        clockApp.click();

// Set an alarm to go off in about 2 minutes
        setAlarm(2);

// wait for the alarm alert dialog
        UiObject alarmAlert =
                new UiObject(new UiSelector().packageName("com.google.android.deskclock")
                        .className(TextView.class.getName()).text("Alarm"));
        assertTrue("Timeout while waiting for alarm to go off",
                alarmAlert.waitForExists(2 * 60 * 1000));
        clickByText("Dismiss");
}

private void setAlarm(int minutesFromNow) throws UiObjectNotFoundException {
        UiObject setAlarm = new UiObject(new UiSelector().textStartsWith("Alarm set"));

        if (!setAlarm.exists())
            setAlarm = new UiObject(new UiSelector().textStartsWith("Set alarm"));
        setAlarm.click();
        clickByDescription("Add alarm");
        clickByText("Time");

        UiSelector minuteAreaSelector = new UiSelector().className(
                android.widget.NumberPicker.class.getName()).instance(1);
        UiSelector minuteIncreaseButtonSelector = minuteAreaSelector.childSelector(
                new UiSelector().className(android.widget.Button.class.getName()).instance(1));

        for (int x = 0; x < minutesFromNow; x++)
            new UiObject(minuteIncreaseButtonSelector).click();

        clickByText("Done");
        UiObject doneButton = new UiObject(new UiSelector().text("Done"));
        UiObject okButton = new UiObject(new UiSelector().text("OK"));

        if (doneButton.exists()) {
            doneButton.click();
        } else {
            okButton.click(); // let it fail if neither exists
        }
        clickByText("Done");
        getUiDevice().pressHome();
}

private void clickByDescription(String text) throws UiObjectNotFoundException {
        UiObject obj = new UiObject(new UiSelector().description(text));
        obj.clickAndWaitForNewWindow();
}

private void clickByText(String text) throws UiObjectNotFoundException {
        UiObject obj = new UiObject(new UiSelector().text(text));
        obj.clickAndWaitForNewWindow();
}