简体   繁体   English

UIAutomator-错误-调用虚拟方法

[英]UIAutomator - Error - Invoking virtual method

I'm running a simple test on UIautomator. 我正在UIautomator上运行一个简单的测试。 I have a class with 2 methods (one for setting up the ambient, and another to perform UI interactions on device. 我有一个包含2种方法的类(一种用于设置环境,另一种用于在设备上执行UI交互。

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): 主类(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. 对mDevice.findObject(..)的调用之一返回null,这意味着未找到该对象。 Then, when you try to call click() on the null object, you get an NPE. 然后,当您尝试在null对象上调用click()时,会得到一个NPE。

You should double check around line 147 to see which specific call is failing. 您应该仔细检查第147行,以查看哪个特定呼叫失败。 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. 您使用的选择器可能存在问题,或者在尝试查找UI元素时测试显示在错误的屏幕上。

I got this error when running from the command line with commands like these: 使用以下命令从命令行运行时出现此错误:

./gradlew connectedDebugAndroidTest ./gradlew connectedDebugAndroidTest

./gradlew connectedAndroidTest ./gradlew connectedAndroidTest

I think I got these errors because I had forgotten to specify AndroidJUnitRunner as the default instrumentation runner in my project . 我认为我遇到了这些错误,因为我忘记了将AndroidJUnitRunner指定为项目中的默认检测运行器 In an appropriate AndroidManifest.xml, define an instrumentation with android:name set to AndroidJUnitRunner and the appropriate android:targetPackage set. 在适当的AndroidManifest.xml中,定义android:name设置为AndroidJUnitRunner且适当的android:targetPackage设置的工具。 For example, here is my app/src/debug/AndroidManifest.xml : 例如,这是我的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. 感谢Joe Birch提供的此解决方案 ,我适应了我的屏幕显示“ ALLOW”而不是“ Allow”的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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