androidandroid-uiautomatorjunit3

UIautomator Test Case Android


I'm working with UiAutomator, but I unable to extend my class with UIAutomatorTestCase, Also I had added the jar files ie UIAutomator jar files and JUnit3 lib. I'm getting error in this class And also i want to knw how to build the testcases for that application plus How can I run the test cases from my application programmatically? if no then how i can run these test cases.

  package com.example.test;

import junit.framework.TestCase;

public class UiAutomatorTestCase extends TestCase {

    private static final String DISABLE_IME = "disable_ime";
    private static final String DUMMY_IME_PACKAGE = "com.android.testing.dummyime";
    private UiDevice mUiDevice;
    private Bundle mParams;
    private IAutomationSupport mAutomationSupport;
    private boolean mShouldDisableIme = false;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mShouldDisableIme = "true".equals(mParams.getString(DISABLE_IME));
        if (mShouldDisableIme) {
            setDummyIme();
        }
    }

    @Override
    protected void tearDown() throws Exception {
        if (mShouldDisableIme) {
            restoreActiveIme();
        }
        super.tearDown();
    }

    /**
     * Get current instance of {@link UiDevice}. Works similar to calling the static {@link UiDevice#getInstance()} from anywhere in the test classes.
     */
    public UiDevice getUiDevice() {
        return mUiDevice;
    }

    /**
     * Get command line parameters. On the command line when passing <code>-e key value</code> pairs, the {@link Bundle} will have the key value pairs conveniently available to the
     * tests.
     */
    public Bundle getParams() {
        return mParams;
    }

    /**
     * Provides support for running tests to report interim status
     * 
     * @return
     */
    public IAutomationSupport getAutomationSupport() {
        return mAutomationSupport;
    }

    /**
     * package private
     * 
     * @param uiDevice
     */
    void setUiDevice(UiDevice uiDevice) {
        mUiDevice = uiDevice;
    }

    /**
     * package private
     * 
     * @param params
     */
    void setParams(Bundle params) {
        mParams = params;
    }

    void setAutomationSupport(IAutomationSupport automationSupport) {
        mAutomationSupport = automationSupport;
    }

    /**
     * Calls {@link SystemClock#sleep(long)} to sleep
     * 
     * @param ms
     *            is in milliseconds.
     */
    public void sleep(long ms) {
        SystemClock.sleep(ms);
    }

    protected void setDummyIme() throws RemoteException {
        IInputMethodManager im = IInputMethodManager.Stub.asInterface(ServiceManager.getService(Context.INPUT_METHOD_SERVICE));
        List<InputMethodInfo> infos = im.getInputMethodList();
        String id = null;
        for (InputMethodInfo info : infos) {
            if (DUMMY_IME_PACKAGE.equals(info.getComponent().getPackageName())) {
                id = info.getId();
            }
        }
        if (id == null) {
            throw new RuntimeException(String.format("Required testing fixture missing: IME package (%s)", DUMMY_IME_PACKAGE));
        }
        im.setInputMethod(null, id);
    }

    protected void restoreActiveIme() throws RemoteException {
        // TODO: figure out a way to restore active IME
        // Currently retrieving active IME requires querying secure settings provider, which is hard
        // to do without a Context; so the caveat here is that to make the post test device usable,
        // the active IME needs to be manually switched.
    }
}

Following class is to test the calculator. But I dont knw how to use it.. Here's the link

package com.example.test;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class DemoCalTest extends UiAutomatorTestCase {
    public void testingCalculator() throws UiObjectNotFoundException {

        // First we testing the press of the HOME button.

        getUiDevice().pressHome();

        // using the uiautomatorviewer tool we found that the button for the "applications" has

        //the value “Apps” (screen9)

        // so we use this property to create a UiSelector to find the button. 

        UiObject Applications = new UiObject(new UiSelector().description("Apps"));

        // testing the click to bring up the All Apps screen.

        Applications.clickAndWaitForNewWindow();

        // In the All Apps screen, the "Calculator" application is located in 

        // the Apps tab. So, we create a UiSelector to find a tab with the text 

        // label “Apps”.

        UiObject apps = new UiObject(new UiSelector().text("Apps"));

        // and then testing the click to this tab in order to enter the Apps tab.

        apps.click();

        // All the applications are in a scrollable list 

        // so first we need to get a reference to that list

        UiScrollable ListOfapplications = new UiScrollable(new UiSelector().scrollable(true));

        // and then trying to find the application    

        // with the name Calculator

        UiObject Calculator = ListOfapplications.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()), "Calculator");

        Calculator.clickAndWaitForNewWindow();

        // now the Calculator app is open

        // so we can test the press of button "7" using the ID "com.android.calculator2:id/digit7"

        //we found by using uiautomatorviewer

        UiObject seven = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit7"));

        seven.click();

        // now we test the press of button "+"

        UiObject plus = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/plus"));

        plus.click();

        // and then the press of button "1"

        UiObject one = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/digit1"));

        one.click();

        // we test the press of button "="

        UiObject result = new UiObject(new UiSelector().resourceId("com.android.calculator2:id/equal"));

        result.click();

        //and finally we test the press of "Back" button

        getUiDevice().pressBack();

    }
}

I googled alot but could find any explanation about it. Can any one help me out. I am new to it Thanks


Solution

  • This is how you should be extending your class:

    public class <yourClassName> extends UiAutomatorTestCase {
    

    We also need to import uiautomator.jar which usually resides in ~/sdk/platforms/android-xx/

    You can kick start from Link1 and Link2