大家好,我是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);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:java.lang.SecurityException:注入另一个应用程序需要INJECT_EVENTS权限
at com.example.myfirstapp2.test.MainActivityFunctionalTest.testStartSecondActivity(MainActivityFunctionalTest.java:70)
TouchUtils.clickView(this, view);
Run Code Online (Sandbox Code Playgroud)
请帮忙
m.h*_*ian 45
我有同样的问题,我的代码是这样的(对于正常的登录活动):
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());
Run Code Online (Sandbox Code Playgroud)
最后一行是使用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());
Run Code Online (Sandbox Code Playgroud)
这很好用.
moy*_*een 32
我有同样的问题,并添加closeSoftKeyboard()方法为我解决了它.
onView(withId(R.id.view)).perform(typeText(text_to_be_typed), closeSoftKeyboard());
Run Code Online (Sandbox Code Playgroud)
小智 11
我解决了使用 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())
Run Code Online (Sandbox Code Playgroud)
这是因为您的设备已锁定/任何其他打开的对话框已打开/任何阻止测试单击按钮的能力.例如.如果手机被锁定 - 当测试试图点击按钮时,它不能,因为设备被锁定.
我在模拟器上遇到了麻烦,因为它总是显示"启动器已经崩溃".因此,每当它尝试单击按钮时,它都不能,因为警报对话框已打开.
简而言之.确保您的屏幕已解锁且没有消息框干扰测试,并且可以单击按钮.
我自己也面临同样的问题,这就是我发现的这个问题.
向您的应用添加INJECT_EVENTS权限,使Android Studio指出此类权限"仅授予系统应用".此外,Google的 manifest.permissions 参考指南指出此权限是"不供第三方应用程序使用".
现在,您的应用程序可能不是系统应用程序.因此,添加此权限绝对不是一件好事,幸运的是不适用于您的第三方项目.至少在Android Studio上开发时.
我可以看到你在setUp方法中调用了setActivityInitialTouchMode(false); 正如Google针对UI测试的最佳实践所指出的那样,在测试UI时,必须将Touch Mode设置为true.否则,您的测试夹具将无法与UI元素交互.
还有一件事.这是一个自动化测试,可模拟用户对您应用的操作.如果我们与设备交互(真实或虚拟,无关紧要),我们很可能会让其他事情获得焦点(即使在被测试的应用内),然后与setUp方法的触摸模式设置会发生冲突表演了.
最终,这就是发生在我身上的事情.我只是通过不点击/触摸/与正在运行测试的设备进行交互来解决我的问题.
对于有根的设备,此文件对我有很大帮助。它具有:
Injector.pressBackButton();
Injector.pressHomeButton();
Injector.pressPowerButton();
Injector.showNotificationCenter();
Injector.swipeLeftRight();
Injector.swipeRightLeft();
Injector.touch(x, y);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22125 次 |
最近记录: |