5

Android SDK : 22 (L) UiAutomator Version : 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

On a Button Click I am trying to Access Instrumentation from an Android Application using

Boolean start = false;
start = startInstrumentation(new ComponentName("com.automation.vzw.sanity.test", "android.test.InstrumentationTestRunner"), null, null);
  System.out.println("value of start is " +start);

Value of the start shows "true" which indicates it was launched

Problem is : once the Instrumentation starts and setUp() is called , when the below gets executed , there is a Crash

public class ApplicationTest extends InstrumentationTestCase {

 public void setUp() {
              UiDevice testDevice = UiDevice.getInstance(getInstrumentation());
             }

 public void testCase1(){

        System.out.println("In testcase1");

}

}

Crash Details :

/TestRunner(11209): started: testCase1(com.automation.vzw.sanity.ApplicationTest)

I/TestRunner(11209): failed: testCase1(com.automation.vzw.sanity.ApplicationTest)

I/TestRunner(11209): ----- begin exception -----

I/TestRunner(11209): 

I/TestRunner(11209):   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.UiAutomation.setOnAccessibilityEventListener(android.app.UiAutomation$OnAccessibilityEventListener)' on a null object reference

I/TestRunner(11209):    at android.support.test.uiautomator.UiAutomatorBridge.setOnAccessibilityEventListener(UiAutomatorBridge.java:78)

I/TestRunner(11209):    at android.support.test.uiautomator.QueryController.<init>(QueryController.java:58)

I/TestRunner(11209):    at android.support.test.uiautomator.UiAutomatorBridge.<init>(UiAutomatorBridge.java:66)

I/TestRunner(11209):    at android.support.test.uiautomator.InstrumentationUiAutomatorBridge.<init>(InstrumentationUiAutomatorBridge.java:35)

I/TestRunner(11209):    at android.support.test.uiautomator.UiDevice.<init>(UiDevice.java:103)

I/TestRunner(11209):    at android.support.test.uiautomator.UiDevice.getInstance(UiDevice.java:263)

I/TestRunner(11209):    at com.automation.vzw.sanity.ApplicationTest.setUp(ApplicationTest.java:103)

I/TestRunner(11209):    at junit.framework.TestCase.runBare(TestCase.java:132)

I/TestRunner(11209):    at junit.framework.TestResult$1.protect(TestResult.java:115)

I/TestRunner(11209):    at junit.framework.TestResult.runProtected(TestResult.java:133)

I/TestRunner(11209):    at junit.framework.TestResult.run(TestResult.java:118)

I/TestRunner(11209):    at junit.framework.TestCase.run(TestCase.java:124)

I/TestRunner(11209):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)

I/TestRunner(11209):    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)

I/TestRunner(11209):    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)

I/TestRunner(11209):    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1944)

I/TestRunner(11209): ----- end exception -----

I/TestRunner(11209): finished: testCase1(com.automation.vzw.sanity.ApplicationTest)

I/ActivityManager(30686): Force stopping com.automation.vzw.sanity appid=10175 user=0: finished inst

I/ActivityManager(30686): Killing 11209:com.automation.vzw.sanity/u0a175 (adj 0): stop com.automation.vzw.sanity

I/ServiceManager(31912): Waiting for service SurfaceFlinger...

W/ActivityManager(30686): Spurious death for ProcessRecord{2a8ec647 11209:com.automation.vzw.sanity/u0a175}, curProc for 11209: null

Note: when executed from command line there is no problem works well (am instrument -w com.automation.vzw.sanity.test/android.test.InstrumentationTestRunner)

Please help to overcome this

Thanks

Gautham
  • 47
  • 1
  • 6

2 Answers2

0

That's not how you get the instrumentation. Use

Instrumentation instr = InstrumentationRegistry.getInstrumentation();

to access instrumentation.

Then you can obtain an UiDevice object:

UiDevice device = UiDevice.getInstance(instr);
Inês
  • 882
  • 3
  • 13
  • 28
  • which one? The first or the second command? Do you have developer mode activated on your phone? – Inês Sep 16 '16 at 18:18
  • the second. yes, developer mode is on – likejudo Sep 16 '16 at 22:14
  • Make sure you are: 1) implementing your code in the test project folder (androidTest); 2) implementing it as junit 4 tests 3) you're accessing the instrumentation registry, etc in the setUp() method – Inês Sep 22 '16 at 09:56
  • 1
    I tired the above steps but still getting null pointer.Were you able to fix this problem? – Ajit Pratap Singh Sep 29 '16 at 06:42
0

while running test with adb commands. add a -w flag.

adb shell am instrument -w com.example.app.test/android.support.test.runner.AndroidJUnitRunner
Venkataramanan
  • 149
  • 1
  • 15