嗨,我是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权限
在com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)
TouchUtils.clickView(this, view);
请帮帮忙
发布于 2015-11-03 09:14:03
我也有同样的问题,我的代码是这样的(对于正常的登录活动):
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());
这一切运行得很好。
发布于 2016-11-14 23:32:05
我也遇到了同样的问题,添加closeSoftKeyboard()方法为我解决了这个问题。
onView(withId(R.id.view)).perform(typeText(text_to_be_typed), closeSoftKeyboard());
发布于 2019-09-20 23:24:53
我使用replaceText而不是TypeText操作解决了这个问题,我的代码是:
onView(withId(R.id.username_edit_text)).perform(ViewActions.replaceText("user123"))
onView(withId(R.id.password_edit_text)).perform(ViewActions.replaceText("pass123"), closeSoftKeyboard())
https://stackoverflow.com/questions/22163424
复制相似问题