0

I have some requirements where I should run the test project anytime on my device, without connecting it to system and running through eclipse.

I added some intent-filter to my AndroidManifest.xml to make the test project visible on the launcher, however, it never runs successfully and is not able to find the target activity class

Meanwhile, If I connect the device to system, and from eclipse perform Run As Android Junit , it runs successfully without any exception. So what is wrong here? Am i missing some concept ?

Below is the AndroidManifest.xml of my test application

<?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>

And here is the exception log

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]]

Also the test class code 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
    }

I am new to android automation. Please help me understand the concept if am trying something infeasible.

JJD
  • 50,076
  • 60
  • 203
  • 339
roger_that
  • 9,493
  • 18
  • 66
  • 102

1 Answers1

0

You should read Android Testing: running tests from code which would help you in achieving your objective.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I followed the blog but not actually able to figure out where to put that code? How will it find the package? Can you please give an example for that? – roger_that Mar 12 '14 at 06:24
  • `getPackageName()` should return the package name – Diego Torres Milano Mar 12 '14 at 22:07
  • Are you sure? The requirement is , there is a third party app - A. To test this, i created a test project - B which has a target package of project A. Now, I create another project - C which has a button. In the `onClickListener` of the button, I need to invoke project A. So how do I use your code for this? That is why I asked for an example. Pardon me as m new to android. – roger_that Mar 13 '14 at 14:56
  • You cannot test a third party application in this way because instrumentation requires both application (app+tests) signed with same certificate. – Diego Torres Milano Mar 13 '14 at 15:10
  • Third party means that I din't have the source code. Although, I have resigned the apk I got from devs and am able to test this using android Junit. All I now want is to invoke the tests on the go. – roger_that Mar 13 '14 at 15:12
  • any help ? How to invoke the method you've shared in your blog? Will it be the part of test project? or another project C as per my above comment? – roger_that Mar 16 '14 at 12:52
  • I think it should be part of C, and as in the sample code packageName is a String you can specify whatever package you want (even a list for the user to select). – Diego Torres Milano Mar 17 '14 at 00:09
  • No luck. I get a toast msg saying it `Cannot find instrumentation for "com.my.test.pkg"` – roger_that Mar 18 '14 at 15:03
  • Try `adb shell pm list instrumentation` to check instrumentation is installed – Diego Torres Milano Mar 18 '14 at 18:28
  • Yes. The instrumentation is there. – roger_that Mar 19 '14 at 05:14