从启动器运行android测试项目

时间:2014-03-11 18:39:47

标签: android automation black-box-testing

我有一些要求,我应该随时在我的设备上运行测试项目,而无需将其连接到系统并运行eclipse。

我向intent-filter添加了一些AndroidManifest.xml以使测试项目在启动器上可见,但是,它从未成功运行且无法找到目标活动类

同时,如果我将设备连接到系统,并从eclipse执行Run As Android Junit,它会成功运行,没有任何异常。那么这里有什么问题?我错过了一些概念吗?

以下是我的测试应用程序的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.org.search.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="18"/>

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.third.party.packageName" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />

        <activity android:name="com.org.search.test.MyTestClass">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>    
    </application>

</manifest>

这是异常日志

java.lang.ExceptionInInitializerError
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1130)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2183)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
    at android.app.ActivityThread.access$600(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:5225)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: 
    com.third.party.packageName.SearchActivity
    at com.expedia.search.test.ExpediaHotelsScraper.<clinit>(ExpediaHotelsScraper.java:35)
    ... 15 more

Caused by: java.lang.ClassNotFoundException: com.third.party.packageName.SearchActivity
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:204)
    at java.lang.Class.forName(Class.java:169)
    at com.org.search.test.MyTestClass.<clinit>(MyTestClass.java:32)
    ... 15 more

Caused by: java.lang.NoClassDefFoundError: com/third/party/packageName/activity/SearchActivity
    ... 19 more

Caused by: java.lang.ClassNotFoundException: Didn't find class 
    "com.third.party.packageName.SearchActivity" on path: 
    DexPathList[[zip file "/system/framework/android.test.runner.jar", 
    zip file "/data/app/com.org.search.test-1.apk"],
    nativeLibraryDirectories=[/data/app-lib/com.org.search.test-1, 
    /vendor/lib, /system/lib]]

测试类代码MyTestClass.java

public class MyTestClass extends ActivityInstrumentationTestCase2 {


    private static final String LAUNCH_ACTIVITY_NAME = 
        "com.third.party.packageName.activity.SearchActivity";

    private Solo solo;

    private static Class<?> splashActivityClass;
    static {
        try {
            splashActivityClass = Class.forName(LAUNCH_ACTIVITY_NAME);
        }
        catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public MyTestClass() throws ClassNotFoundException {
        super(splashActivityClass);
    }

    @Override
    protected void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testAllCountries() {
            //my test goes here
    }

我是Android自动化的新手。如果我尝试不可行的东西,请帮助我理解这个概念。

1 个答案:

答案 0 :(得分:0)

您应阅读Android Testing: running tests from code,这有助于您实现目标。