简体   繁体   中英

UIAutomator - Error - Invoking virtual method

I'm running a simple test on UIautomator. I have a class with 2 methods (one for setting up the ambient, and another to perform UI interactions on device.

And I have another class that instantializes the first class and call their methods. However, I'm seeing this error when I run the code:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.test.uiautomator.UiObject2.click()' on a null object reference
   at com.example.rcorrea.AutoCSP.testCSP(AutoCSP.java:147)
   at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
   at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
   at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
   at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
   at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
   at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1873)

My code:

Main class (AutoCSP_new):

package com.example.rcorrea;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.test.InstrumentationTestCase;
import java.util.*;
import static java.lang.System.out;


/**
 * Created by rcorrea on 10/1/2015.
 */
public class AutoCSP_new extends InstrumentationTestCase {


    private UiDevice mDevice;
    private AutoCSP_Generica autocsp;
    private Vector numeros;

    public void test() throws Exception

    {
        numeros = new Vector (5);

        numeros.addElement(new String ("0191234"));
        numeros.addElement(new String ("0191234"));
        numeros.addElement(new String ("0191234"));
        numeros.addElement(new String ("0191234"));


        //numeros[0] = "0191234";
        //numeros[1] = "0191234567";
       // numeros[2] = "01912345678";
        //numeros[3] = "019123456789";
        autocsp = new AutoCSP_Generica (numeros);

        autocsp.setUp();
        autocsp.testCSP();
    }
}

Class that does stuff:

package com.example.rcorrea;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.test.InstrumentationTestCase;
import java.util.*;

import static android.support.test.uiautomator.UiDevice.*;
import static java.lang.System.out;


import java.sql.Time;

/**
 * Created by rcorrea on 10/1/2015.
 */
public class AutoCSP_Generica extends InstrumentationTestCase {


    private UiDevice mDevice;
    //private String[] numeros = new String[10];
    private Vector numeros;

    public AutoCSP_Generica(Vector num)
    {
        this.numeros = num;
    }


    public void setUp() throws Exception {
        super.setUp();

        mDevice = UiDevice.getInstance(getInstrumentation());

        mDevice.pressHome();

        mDevice.wait(Until.hasObject(By.text("Telefone")), 10000);

        UiObject2 appsButton = mDevice.findObject(By.text("Telefone"));
        if (mDevice.findObject(By.text("Telefone")) == null){

            mDevice.pressHome();

        }
        appsButton.click();


        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/more_menu")), 3000);
        UiObject2 moreButton = mDevice.findObject(By.res("com.android.contacts:id/more_menu"));
        moreButton.click();

        mDevice.wait(Until.hasObject(By.text("Configurações")), 3000);
        mDevice.waitForIdle(10000);
        UiObject2 buttonSettings = mDevice.findObject(By.text("Configurações"));
        buttonSettings.click();

        //assertEquals com.android.systemui:id/mobile.enabled = true;

        mDevice.wait(Until.hasObject(By.text("Chamadas")), 3000);
        UiObject2 buttonCall = mDevice.findObject(By.text("Chamadas"));
        buttonCall.click();



        mDevice.wait(Until.hasObject(By.text("Mais configurações")), 5000);
        UiObject2 buttonMore = mDevice.findObject(By.text("Mais configurações"));
        buttonMore.click();

        while (mDevice.hasObject(By.textContains("Lendo")) == true){

            mDevice.waitForIdle(5000);
        }

        mDevice.wait(Until.hasObject(By.text("Números FDN")), 5000);
        UiObject2 buttonFDN = mDevice.findObject(By.text("Números FDN"));
        buttonFDN.click();

        mDevice.waitForIdle(6000);

        mDevice.wait(Until.hasObject(By.text("Ativar FDN")), 5000);
        UiObject2 buttonOn = mDevice.findObject(By.text("Ativar FDN"));
        buttonOn.click();

        mDevice.waitForIdle(5000);


        mDevice.wait(Until.hasObject(By.clazz("android.widget.EditText")), 5000);
        UiObject2 buttonInsert = mDevice.findObject(By.clazz("android.widget.EditText"));
        buttonInsert.setText("1234");


        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.text("OK")), 5000);
        UiObject2 buttonOk = mDevice.findObject(By.text("OK"));
        buttonOk.click();

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.text("Lista FDN")), 5000);
        UiObject2 buttonList = mDevice.findObject(By.text("Lista FDN"));
        buttonList.click();

        mDevice.waitForIdle(5000);

        //Adicionando o Contato na FDN, 8 Digitos
        mDevice.wait(Until.hasObject(By.text("Adicionar")), 5000);
        UiObject2 buttonAdd = mDevice.findObject(By.text("Adicionar"));
        buttonAdd.click();

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.res("com.android.phone:id/fdn_number")), 7000);
        UiObject2 buttonNum = mDevice.findObject(By.res("com.android.phone:id/fdn_number"));
        buttonNum.setText("01912345678");

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.res("com.android.phone:id/fdn_pin2")), 7000);
        UiObject2 buttonPIN = mDevice.findObject(By.res("com.android.phone:id/fdn_pin2"));
        buttonPIN.setText("1234");

        mDevice.waitForIdle(5000);
        mDevice.wait(Until.hasObject(By.text("Salvar")), 7000);
        UiObject2 buttonSave = mDevice.findObject(By.text("Salvar"));
        buttonSave.click();

        mDevice.waitForIdle(5000);

        //Adicionando com 9 Digitos.
        mDevice.wait(Until.hasObject(By.text("Adicionar")), 5000);
        buttonAdd = mDevice.findObject(By.text("Adicionar"));
        buttonAdd.click();

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.res("com.android.phone:id/fdn_number")), 7000);
        buttonNum = mDevice.findObject(By.res("com.android.phone:id/fdn_number"));
        buttonNum.setText("019123456789");

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.res("com.android.phone:id/fdn_pin2")), 7000);
        buttonPIN = mDevice.findObject(By.res("com.android.phone:id/fdn_pin2"));
        buttonPIN.setText("1234");

        mDevice.waitForIdle(5000);
        mDevice.wait(Until.hasObject(By.text("Salvar")), 7000);
        buttonSave = mDevice.findObject(By.text("Salvar"));
        buttonSave.click();

        mDevice.waitForIdle(5000);

        //Adicionando com 8 Digitos e CSP diferente
        mDevice.wait(Until.hasObject(By.text("Adicionar")), 5000);
        buttonAdd = mDevice.findObject(By.text("Adicionar"));
        buttonAdd.click();

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.res("com.android.phone:id/fdn_number")), 7000);
        buttonNum = mDevice.findObject(By.res("com.android.phone:id/fdn_number"));
        buttonNum.setText("0181912345678");

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.res("com.android.phone:id/fdn_pin2")), 7000);
        buttonPIN = mDevice.findObject(By.res("com.android.phone:id/fdn_pin2"));
        buttonPIN.setText("1234");

        mDevice.waitForIdle(5000);
        mDevice.wait(Until.hasObject(By.text("Salvar")), 7000);
        buttonSave = mDevice.findObject(By.text("Salvar"));
        buttonSave.click();

        mDevice.waitForIdle(5000);

        //Adicionando com CSP diferente e 9 Digitos
        mDevice.wait(Until.hasObject(By.text("Adicionar")), 5000);
        buttonAdd = mDevice.findObject(By.text("Adicionar"));
        buttonAdd.click();

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.res("com.android.phone:id/fdn_number")), 7000);
        buttonNum = mDevice.findObject(By.res("com.android.phone:id/fdn_number"));
        buttonNum.setText("01819123456789");

        mDevice.waitForIdle(5000);

        mDevice.wait(Until.hasObject(By.res("com.android.phone:id/fdn_pin2")), 7000);
        buttonPIN = mDevice.findObject(By.res("com.android.phone:id/fdn_pin2"));
        buttonPIN.setText("1234");

        mDevice.waitForIdle(5000);
        mDevice.wait(Until.hasObject(By.text("Salvar")), 7000);
        buttonSave = mDevice.findObject(By.text("Salvar"));
        buttonSave.click();

        mDevice.waitForIdle(5000);
        mDevice.pressHome();


    }


    public void testCSP() throws Exception {
        //FirstTest 11 Numbers.


        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/digits")), 3000);
        UiObject2 buttonNumber = mDevice.findObject((By.res("com.android.contacts:id/digits")));
        buttonNumber.click();

        buttonNumber.setText(numeros.get(0).toString());

        mDevice.wait(Until.hasObject(By.desc("Chamar")), 10000);
        UiObject2 buttonDial = mDevice.findObject(By.desc("Chamar"));
        buttonDial.click();

        mDevice.wait(Until.hasObject(By.text("Encerrar")), 50000);
        UiObject2 buttonEnd = mDevice.findObject(By.text("Encerrar"));
        buttonEnd.click();

        mDevice.waitForIdle(10000);
        mDevice.pressBack();

        //Second Test 7 Numbers.
        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/digits")), 3000);
        buttonNumber = mDevice.findObject((By.res("com.android.contacts:id/digits")));
        buttonNumber.click();

        buttonNumber.setText(numeros.get(1).toString());

        mDevice.wait(Until.hasObject(By.desc("Chamar")), 10000);
        buttonDial = mDevice.findObject(By.desc("Chamar"));
        buttonDial.click();

        mDevice.wait(Until.hasObject(By.text("Encerrar")), 50000);
        buttonEnd = mDevice.findObject(By.text("Encerrar"));
        buttonEnd.click();

        mDevice.waitForIdle(10000);
        mDevice.pressBack();

        //Third Test 12 Numbers
        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/digits")), 3000);
        buttonNumber = mDevice.findObject((By.res("com.android.contacts:id/digits")));
        buttonNumber.click();

        buttonNumber.setText(numeros.get(2).toString());

        mDevice.wait(Until.hasObject(By.desc("Chamar")), 10000);
        buttonDial = mDevice.findObject(By.desc("Chamar"));
        buttonDial.click();

        mDevice.wait(Until.hasObject(By.text("Encerrar")), 50000);
        buttonEnd = mDevice.findObject(By.text("Encerrar"));
        buttonEnd.click();

        mDevice.waitForIdle(10000);
        mDevice.pressBack();

        //Fourth Test 10 Numbers
        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/digits")), 3000);
        buttonNumber = mDevice.findObject((By.res("com.android.contacts:id/digits")));
        buttonNumber.click();

        buttonNumber.setText(numeros.get(3).toString());

        mDevice.wait(Until.hasObject(By.desc("Chamar")), 10000);
        buttonDial = mDevice.findObject(By.desc("Chamar"));
        buttonDial.click();

        mDevice.wait(Until.hasObject(By.text("Encerrar")), 50000);
        buttonEnd = mDevice.findObject(By.text("Encerrar"));
        buttonEnd.click();

        mDevice.waitForIdle(10000);
        mDevice.pressBack();

        //Starting Videocall protocols.

        //FirstTest 11 Numbers.
        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/digits")), 3000);
        buttonNumber = mDevice.findObject((By.res("com.android.contacts:id/digits")));
        buttonNumber.click();

        buttonNumber.setText(numeros.get(0).toString());

        mDevice.wait(Until.hasObject(By.desc("Videochamada")), 10000);
        buttonDial = mDevice.findObject(By.desc("Videochamada"));
        buttonDial.click();

        mDevice.wait(Until.hasObject(By.text("Encerrar")), 50000);
        buttonEnd = mDevice.findObject(By.text("Encerrar"));
        mDevice.waitForIdle(10000);
        buttonEnd.click();

        mDevice.waitForIdle(10000);
        mDevice.pressBack();

        //Second Test 7 Numbers.
        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/digits")), 3000);
        buttonNumber = mDevice.findObject((By.res("com.android.contacts:id/digits")));
        buttonNumber.click();

        buttonNumber.setText(numeros.get(1).toString());

        mDevice.wait(Until.hasObject(By.desc("Videochamada")), 10000);
        buttonDial = mDevice.findObject(By.desc("Videochamada"));
        buttonDial.click();

        mDevice.wait(Until.hasObject(By.text("Encerrar")), 50000);
        buttonEnd = mDevice.findObject(By.text("Encerrar"));
        mDevice.waitForIdle(10000);
        buttonEnd.click();

        mDevice.waitForIdle(10000);
        mDevice.pressBack();

        //Third Test 12 Numbers
        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/digits")), 3000);
        buttonNumber = mDevice.findObject((By.res("com.android.contacts:id/digits")));
        buttonNumber.click();

        buttonNumber.setText(numeros.get(2).toString());

        mDevice.wait(Until.hasObject(By.desc("Videochamada")), 10000);
        buttonDial = mDevice.findObject(By.desc("Videochamada"));
        buttonDial.click();

        mDevice.wait(Until.hasObject(By.text("Encerrar")), 50000);
        buttonEnd = mDevice.findObject(By.text("Encerrar"));
        mDevice.waitForIdle(10000);
        buttonEnd.click();

        mDevice.waitForIdle(10000);
        mDevice.pressBack();

        //Fourth Test 10 Numbers
        mDevice.wait(Until.hasObject(By.res("com.android.contacts:id/digits")), 3000);
        buttonNumber = mDevice.findObject((By.res("com.android.contacts:id/digits")));
        buttonNumber.click();

        buttonNumber.setText(numeros.get(3).toString());

        mDevice.wait(Until.hasObject(By.desc("Videochamada")), 10000);
        buttonDial = mDevice.findObject(By.desc("Videochamada"));
        buttonDial.click();

        mDevice.wait(Until.hasObject(By.text("Encerrar")), 50000);
        buttonEnd = mDevice.findObject(By.text("Encerrar"));
        buttonEnd.click();

        mDevice.waitForIdle(10000);
        mDevice.pressBack();


    }
}

Could you please help me out? Everything was running fine when I coded in a single dumb class, but when I separed them to reuse code in the future, I got this error....

One of the calls to mDevice.findObject(..) is returning null, meaning the object was not found. Then, when you try to call click() on the null object, you get an NPE.

You should double check around line 147 to see which specific call is failing. There is likely a problem with the selector that you're using, or that the test is on the wrong screen when it tries to look up the UI element.

I got this error when running from the command line with commands like these:

./gradlew connectedDebugAndroidTest

./gradlew connectedAndroidTest

I think I got these errors because I had forgotten to specify AndroidJUnitRunner as the default instrumentation runner in my project . In an appropriate AndroidManifest.xml, define an instrumentation with android:name set to AndroidJUnitRunner and the appropriate android:targetPackage set. For example, here is my app/src/debug/AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackage.android.sdk" >

    <instrumentation
        android:name="androidx.test.runner.AndroidJUnitRunner"
        android:targetPackage="com.mypackage.android.sdk"/>

</manifest>

I got this error on tests involving screens which require a permission. I wrote and called the following method to deal with the situation where it asked for permission and the situation where the permission had already been granted:

/**
 * Taps Allow on permission granting modal
 *
 * @see <a href="https://medium.com/exploring-android/handling-android-runtime-permissions-in-ui-tests-981f9dc11a4e">medium.com article</a>
 *
 * @param uiDevice
 * @throws UiObjectNotFoundException
 */
private void allowPermissionsIfNeeded(UiDevice uiDevice) throws UiObjectNotFoundException {
    if (Build.VERSION.SDK_INT >= 23) {
        UiObject allowPermissions = uiDevice.findObject(new UiSelector().text("ALLOW"));
        if (allowPermissions.exists()) {
            allowPermissions.click();
        }
    }
}

Thanks to Joe Birch for this solution which I adapted to deal with my screens which show "ALLOW" instead of "Allow" as the text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM