Android:java.lang.SecurityException:注入到另一个应用程序需要INJECT_EVENTS权限

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (14)

我是Android Junit测试新手:

我在MainActivityFunctionalTest.java文件中写了一些测试代码

MainActivityFunctionalTest.java:

package com.example.myfirstapp2.test;

public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<Login>{

private static final String TAG = "MainActivityFunctionalTest";
private Login activity;

  public MainActivityFunctionalTest() {
    super(Login.class);
  }


  @Override
  protected void setUp() throws Exception {
     Log.d(TAG,"Set-Up");
     super.setUp();
    setActivityInitialTouchMode(false);
    activity = getActivity();
  }

  public void testStartSecondActivity() throws Exception {
      // add monitor to check for the second activity
        ActivityMonitor monitor =
            getInstrumentation().
              addMonitor(DisplayMessageActivity.class.getName(), null, false);
        //addMonitor(MainActivity.class.getName(), null, false);
     // find button and click it
        Button view = (Button) activity.findViewById(R.id.btnLogin);

        // TouchUtils handles the sync with the main thread internally
        TouchUtils.clickView(this, view);

        // to click on a click, e.g., in a listview
        // listView.getChildAt(0);

        // wait 2 seconds for the start of the activity
        DisplayMessageActivity startedActivity = (DisplayMessageActivity) 

     monitor
            .waitForActivityWithTimeout(5000);
        assertNotNull(startedActivity);

        // search for the textView
        TextView textView = (TextView) startedActivity.findViewById(R.id.Email);

        // check that the TextView is on the screen
        ViewAsserts.assertOnScreen(startedActivity.getWindow().getDecorView(),
            textView);
        // validate the text on the TextView
        assertEquals("Text incorrect", "1http://www.vogella.com", 

         textView.getText().toString());

        // press back and click again
        this.sendKeys(KeyEvent.KEYCODE_BACK);

        TouchUtils.clickView(this, view);

  }


    }

但是,我得到一个错误:java.lang.SecurityException:注入到另一个应用程序需要INJECT_EVENTS权限

at com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)

 TouchUtils.clickView(this, view);
提问于
用户回答回答于

我有同样的问题,并增加了closeSoftKeyboard()方法为我解决它。

onView(withId(R.id.view)).perform(typeText(text_to_be_typed), closeSoftKeyboard());
用户回答回答于

我有同样的问题,我的代码是这样的(对于正常的登录活动):

    onView(withId(R.id.username))
            .perform(new TypeTextAction("test_user"));
    onView(withId(R.id.password))
            .perform(new TypeTextAction("test123"));
    onView(withId(R.id.login)).perform(click());

最后一行崩溃了SecurityException。在最后一次文字输入后发现,键盘保持打开状态,因此下一次点击被认为是在不同的应用程序上。

为了解决这个问题,我只需在打字后关闭键盘。我还必须添加一些睡眠以确保键盘已关闭,否则测试会偶尔中断。所以最终的代码如下所示:

    onView(withId(R.id.username))
            .perform(new TypeTextAction("test_user"));
    onView(withId(R.id.password))
            .perform(new TypeTextAction("test123")).perform(closeSoftKeyboard());
    Thread.sleep(250);
    onView(withId(R.id.login)).perform(click());

扫码关注云+社区