Android :java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

前端 未结 9 1442
感动是毒
感动是毒 2020-12-01 11:48

Hi there I am new to Android Junit testing:

I have written some test code in MainActivityFunctionalTest.java file

MainActivityFunctionalTest.java:

         


        
相关标签:
9条回答
  • 2020-12-01 12:22

    It's because your device is locked/any other open dialog box is open /anything preventing the test's ability to click on the button. Eg. if the phone is locked-when the test tries to click on the button it can't because the device is locked.

    I was having troubles on the emulator because it always displayed "launcher has crashed". So whenever it tried to click the button, it couldn't because the alert dialog box was open.

    In short, make sure your screen is unlocked and no message boxes are interfering with the test and it's ability to click on a button.

    0 讨论(0)
  • 2020-12-01 12:22

    I solved using replaceText instead of TypeText action, my code:

    onView(withId(R.id.username_edit_text)).perform(ViewActions.replaceText("user123"))
    
    onView(withId(R.id.password_edit_text)).perform(ViewActions.replaceText("pass123"), closeSoftKeyboard())
    
    0 讨论(0)
  • 2020-12-01 12:23

    When you test many functions in the same activity, it is recommended to put in this code:

    SystemClock.sleep(800);
    

    at the beginning of every function. You can customize it but the idea is to wait a certain number of times before processing.

    0 讨论(0)
  • 2020-12-01 12:33

    I had exactly the same problem and error message when running espresso tests. One of them was always failing when running whole package, however it always passed when I was running it alone. Interesting thing was that the problem happened because I had added following line to one of my Activities in AndroidManifest.xml:

    android:windowSoftInputMode="stateUnchanged|adjustResize"
    

    After removing or changing above line to:

    android:windowSoftInputMode="stateHidden"
    

    mentioned test was passing also when running whole package.

    0 讨论(0)
  • 2020-12-01 12:35

    I was facing this very same problem myself, and here is what I found about this issue.

    1. Adding INJECT_EVENTS permission to your app, makes Android Studio point out that such permission "Is only granted to system apps". Moreover, Google's reference guide for manifest.permissions states that this permission is "Not for use by third-party applications."

      Now, chances are that your app, as mine, is not a system app. So adding this permission is definitely not a good thing to do, and luckily will not be apply on your 3rd party project. At least when developing on Android Studio.

    2. I can see that in your setUp method, you have called setActivityInitialTouchMode(false); As pointed out by Google's best practices for UI testing, when testing UI one must set Touch Mode to true. Otherwise, your test fixture will not be able to interact with UI elements.

    3. Just one more thing. This is an automated test that emulates user actions upon your app. If we interact with the device (real or virtual, doesn't matter), we will most likely make other things gain focus (even inside the app under test) and then there will be a conflict with the touch mode settings that the setUp method had performed.

    Ultimately, that is what was happening to me. I solved my problem simply by not clicking/touching/interacting with the device where the test was being ran.

    0 讨论(0)
  • 2020-12-01 12:37

    for a rooted device, this file helped me a lot. It has:

     Injector.pressBackButton();
     Injector.pressHomeButton();
     Injector.pressPowerButton();
     Injector.showNotificationCenter();
     Injector.swipeLeftRight();
     Injector.swipeRightLeft();
     Injector.touch(x, y);
    
    0 讨论(0)
提交回复
热议问题