在AndroidJunit测试项目中运行UiAutomatorTestcase

时间:2013-07-19 18:15:30

标签: java android uiautomator android-uiautomator

我实际上是在AndroidJunit Test项目中实现一个简单的测试套件,该项目使用以下类

  • UIObject类
  • UiSelector
  • UiAutomatorTestcase

点击并打开Android设备上的Messaging应用程序,并在Eclipse中将其作为AndroidJunit Test运行。

运行代码时,我得到以下异常

  

java.lang.RuntimeException:Stub!

我不明白我哪里出错了。请告诉我们是否可以使用AndroidJuint测试项目运行UiAutomatorTestcase测试套件。

以下是示例代码和故障跟踪

public class UiautomatorTest extends
        ActivityInstrumentationTestCase2<MyMainActivity> {

    UiAutomatorTestCase mUiAutomatorTestCase;

    public UiautomatorTest() {
        super(MyMainActivity.class);`enter code here`
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void setUp() throws Exception {
        // TODO Auto-generated method stub
        super.setUp();
        mUiAutomatorTestCase = new UiAutomatorTestCase();
    }

    public void testToOpenMessageApp() throws UiObjectNotFoundException {
        UiObject message = new UiObject(
                new UiSelector().description("Messaging"));
        message.clickAndWaitForNewWindow();

        UiObject composeMessage = new UiObject(
                new UiSelector().description("compose"));
        composeMessage.clickAndWaitForNewWindow();

        UiObject typeMessage = new UiObject(
                new UiSelector().description("Type Message"));
        typeMessage.clickAndWaitForNewWindow();
        mUiAutomatorTestCase.getUiDevice().pressHome();

    }

}

stacktrace

java.lang.RuntimeException: Stub!
at mypackagename.UiAutomatorTestCase.<init>(UiAutomatorTestCase.java:5)
at mypackagename..setUp(UiautomatorTest.java:25)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)

4 个答案:

答案 0 :(得分:2)

您需要在Android设备上运行测试。为了实现这一点,代码需要dexed才能运行,等等。阅读更多here,了解如何构建运行在android上的测试。

像这样运行测试

adb shell uiautomator runtest <JARS> -c <CLASSES> [options]

来自official documentation

  

要在目标设备上运行测试用例,可以使用adb shell   命令调用uiautomator工具。

答案 1 :(得分:2)

Android 4.3提供了一种与Instrumentation http://developer.android.com/reference/android/app/Instrumentation.html#getUiAutomation()中的UIAutomation进行互动的方式,该主题在http://developer.android.com/about/versions/android-4.3.html#Testing

中介绍

这似乎是UI Test Automation的另一种风格。我想更多地了解4.3的UIAutomation与UIAutomator之间的互动http://developer.android.com/tools/help/uiautomator/index.html

答案 2 :(得分:0)

作为标准,UI Automator测试被编译并构建到桌面计算机上的jar文件中,然后通过使用adb将jar文件推送到设备将它们部署到设备。然后使用adb shell uiautomator ...在Android运行时在设备上运行它们。感谢https://stackoverflow.com/users/363437/vidstige更正我之前发布的不准确的帖子。

可能帮助您运行测试的最快方法是将代码作为问题的一部分发布。

2013年7月22日更新 我还没有看到有人试图直接在设备上运行测试用例,而不是按照Google(Android)文档的方法构建测试用例。 可能可能,但您可能需要努力发现您的方法是否可行或可行。 UIAutomator与Android InstrumentationTestCase框架截然不同。相反,它与GUI交互,并具有与各种应用程序交互的优势,包括Android设备上内置的设置UI等内容。

我建议您从Android开发网站http://developer.android.com/tools/testing/testing_ui.html中提供的文档化教程开始,看看是否可以实现这一点。您将需要使用运行Android 4.2的设备(或者更新版本的Android发布后阅读此版本的设备)。一旦你完成了教程的工作,我希望为你想要编写自动化测试的应用程序编写UIAutomator测试是一个很好的起点。

欢迎您发布其他更新。

答案 3 :(得分:0)

您可以使用.sh

之类的脚本
 ant build

 $youtSdkPath/platform-tools/adb  shell     rm /data/local/tmp/ProjectName.jar
 $youtSdkPath/platform-tools/adb  push  $pathProject/bin/ProjectName.jar /data/local/tmp/
 $youtSdkPath/platform-tools/adb  shell     uiautomator runtest ProjectName.jar -c ClassToTest

bash theScript.sh