问题描述
我试图运行Ui测试,每次我运行应用程序时,它都会在控制台中显示以下错误并关闭正在运行的应用程序.我有uiautomator.jar,android.jar&导入了JUnit4库.我正在使用Eclipse.我在这里想念什么?
I am trying to run a Ui test, and everytime I run the applicatoin it gives the following error in the console and closes the app that is running. I have the uiautomator.jar, android.jar & JUnit4 libraries imported. Im using Eclipse. What am I missing here?
TestClass
package android.support.v7.appcompat.test;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class DMTest extends UiAutomatorTestCase
{
public void testDemo() throws UiObjectNotFoundException
{
// setText
new UiObject(new UiSelector().description("edittext_brute")).setText("bazinga");
// clickButton
//new UiObject(new UiSelector().description("button_done")).click();
UiObject btDone = new UiObject(new UiSelector().description("button_done"));
btDone.click();
}
}
清单
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.support.v7.appcompat.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.sony.reel" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
即使testDemo()构造函数中没有代码,它也会给出相同的异常
Edit 1: It gives the same exception even if there is no code within the testDemo() constructor
Logcat
04-04 07:25:48.539: E/AndroidRuntime(12026): FATAL EXCEPTION: main
04-04 07:25:48.539: E/AndroidRuntime(12026): Process: com.example.sony.reel, PID: 12026
04-04 07:25:48.539: E/AndroidRuntime(12026): java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{android.support.v7.appcompat.test/android.test.InstrumentationTestRunner}: java.lang.RuntimeException: Could not find test class. Class: android.support.v7.appcompat.test.DMTest
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4599)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.app.ActivityThread.access$1500(ActivityThread.java:148)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.os.Handler.dispatchMessage(Handler.java:102)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.os.Looper.loop(Looper.java:135)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.app.ActivityThread.main(ActivityThread.java:5312)
04-04 07:25:48.539: E/AndroidRuntime(12026): at java.lang.reflect.Method.invoke(Native Method)
04-04 07:25:48.539: E/AndroidRuntime(12026): at java.lang.reflect.Method.invoke(Method.java:372)
04-04 07:25:48.539: E/AndroidRuntime(12026): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
04-04 07:25:48.539: E/AndroidRuntime(12026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
04-04 07:25:48.539: E/AndroidRuntime(12026): Caused by: java.lang.RuntimeException: Could not find test class. Class: android.support.v7.appcompat.test.DMTest
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.test.AndroidTestRunner.runFailed(AndroidTestRunner.java:255)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.test.AndroidTestRunner.loadTestClass(AndroidTestRunner.java:89)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:50)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:78)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:444)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:425)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:371)
04-04 07:25:48.539: E/AndroidRuntime(12026): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4596)
04-04 07:25:48.539: E/AndroidRuntime(12026): ... 9 more
推荐答案
Android 建议使用Studio进行uiautomator测试项目,并使用gradle进行构建.
Android recommends to use studio for uiautomator test projects and gradle for building.
在您的情况下,请尝试在测试类定义的开头添加@RunWith(AndroidJUnit4.class)
注释.
In your case try adding @RunWith(AndroidJUnit4.class)
annotation at the beginning of your test class definition.
如果将其移至Studio,您的代码示例肯定会起作用.或使用ant
进行编译并使用已编译的jar文件运行.
Your code sample will definitely work if you move this to studio. Or compile with ant
and run with compiled jar file.
这篇关于Android:UiTesting时出现运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!