gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:42:22 26 4
gpt4 key购买 nike

您好,我是 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)到另一个应用程序需要 INJECT_EVENTS 权限

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

 TouchUtils.clickView(this, view);

请帮忙

最佳答案

我遇到了同样的问题,我的代码是这样的(对于正常的登录 Activity ):

    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 而崩溃。结果在最后一次输入文本后,键盘保持打开状态,因此下一次点击被认为是在不同的应用程序上。

要解决此问题,我只需在键入后关闭键盘即可。我还必须添加一些 sleep 以确保键盘已关闭,否则测试会不时中断。所以最终的代码是这样的:

    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());

这工作得很好。

关于Android :java. lang.SecurityException:注入(inject)另一个应用程序需要INJECT_EVENTS权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22163424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com