UIautomator测试案例Android

时间:2014-07-10 05:51:27

标签: android uiautomator junit3 android-uiautomator

我正在使用UiAutomator,但我无法使用UIAutomatorTestCase扩展我的课程, 我还添加了jar文件,即UIAutomator jar文件和JUnit3 lib。 我在这堂课上遇到错误 而且我想知道如何为该应用程序构建测试用例以及如何从我的应用程序运行测试用例 编程?如果没有,那么我如何运行这些测试用例。

  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.
    }
}

下课是测试计算器。 但我不知道如何使用它.. 这是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();

    }
}

我用Google搜索了很多,但可以找到任何解释。  谁能帮我吗。 我是新手 感谢

2 个答案:

答案 0 :(得分:3)

这就是你应该如何扩展你的课程:

public class <yourClassName> extends UiAutomatorTestCase {

我们还需要导入通常位于uiautomator.jar

~/sdk/platforms/android-xx/

您可以从Link1Link2

开始

答案 1 :(得分:0)

由于Gradle是作为新的构建工具引入的,因此您不必包含任何jar。只需添加

androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

作为build.gradle文件的依赖项。

例如:

dependencies {
    androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
        exclude module: 'support-annotations'
    }

    androidTestCompile 'com.android.support:support-annotations:24.2.1'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'

}