我正在尝试用新的android-test-kit(Espresso)编写一些测试.但我找不到任何关于如何检查对话框是否显示并在其上执行某些操作的信息(如单击正负按钮等).请注意,对话框也可以由a显示WebView,而不是由应用程序自己显示.
任何帮助,将不胜感激.我只需要一个链接,或基本的一些示例代码:
setCancelable(false)在对话框构建器上调用,我们要检查它)谢谢你的建议!
当我在运行一些espresso测试时显示的布局中有ProgressBar时,我会遇到:
Caused by: android.support.test.espresso.AppNotIdleException: Looped for 1670 iterations over 60 SECONDS. The following Idle Conditions failed .
Run Code Online (Sandbox Code Playgroud)
解决这个问题的好方法是什么?找到一些hackish东西,但寻找一个很好的方式
正如标题所说,它失败了一些,有些则失败了.
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.
Expected: is displayed on the screen to the user
Got: "AppCompatTextView{id=2131492981, res-name=snackbar_text, visibility=VISIBLE, width=444, height=71, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=0.0, text=Network Error, input-type=0, ime-target=false, has-links=false}"
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪的第一行表明espresso无法在屏幕上看到Snackbar.但是,第二行指出它实际上是看到了小吃吧与visibility=VISIBLE和text=Network Error它是正确的.
我很困惑,发生了什么事?
这是我的测试代码:
activityRule.launchActivity(new Intent());
onView(withText("Network Error")).check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)
PS:当我运行整套测试服时,它大多失败了; 但有时当我单独运行此测试时它也会失败.有时它会传递绿色,但没有任何模式,似乎是随机的.
我在我的一个活动中遇到匹配toast消息的问题.在其他情况下,它的工作没有任何问题,但在这个特殊的情况下却没有.我不知道任何可能导致此行为的特殊布局更改.
我得到以下异常:
java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong. Selected Root:
Root{application-window-token=android.view.ViewRootImpl$W@40dcb2, window-token=android.view.ViewRootImpl$W@40dcb2, has-window-focus=false, layout-params-type=2, layout-params-string=WM.LayoutParams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1820002 pfl=0x8 fmt=-3 wanim=0x7f0a0088 surfaceInsets=Rect(0, 0 - 0, 0) mwfl=0x10}, decor-view-string=DecorView{id=-1, visibility=GONE, width=1002, height=348, has-focus=false, has-focusable=false, has-window-focus=false, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, …Run Code Online (Sandbox Code Playgroud) 我正在测试一个应用程序Espresso.我有一个问题是可能要等到目前没有吐司出现.我在我的应用程序中有很多不同的吐司,但是在测试时我遇到了问题,因为据我所知,焦点已经转向吐司,我正在获得另一个视图层次结构,我可以在错误日志中看到.
所以我的问题是可以隐藏所有(系统范围内的root访问权限)或只是等到屏幕上有任何toast,或者是否可以手动将焦点设置到活动视图层次结构.
如果对这个问题有任何帮助,我将不胜感激.
谢谢.
PS禁用toast直接在我的应用程序中的某个地方不是一个选项,因为它为应用程序带来了一些额外的逻辑,这只是在测试时需要.
我无法使用浓缩咖啡测试 Toast 消息。有很多与之相关的问题和答案,但我无法解决该问题。
测试代码
class ToastMatcher extends TypeSafeMatcher<Root> {
@Override
public boolean matchesSafely(Root root) {
int type = root.getWindowLayoutParams().get().type;
if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
IBinder windowToken = root.getDecorView().getWindowToken();
IBinder appToken = root.getDecorView().getApplicationWindowToken();
if (windowToken == appToken) {
return true;
//means this window isn't contained by any other windows.
}
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText(String.valueOf(R.string.messsage_login_successful));
}
}
@Test
public void btnLoginClickWithPassingUserNameAndPassword() throws Exception {
onView(withId(R.id.etUsername)).perform(clearText());
onView(withId(R.id.etUsername)).perform(typeText(userName));
onView(withId(R.id.etPassword)).perform(typeText(passWord));
onView(withId(R.id.btnLogin)).perform(click());
// onView(withText(R.string.messsage_login_successful)).inRoot(withDecorView(not(mActivityRule.getActivity().getWindow().getDecorView()))).check(matches(isDisplayed()));
// onView(withText(R.string.messsage_login_successful)).inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));
onView(withText(R.string.messsage_login_successful)).inRoot(new ToastMatcher())
.check(matches(isDisplayed())); …Run Code Online (Sandbox Code Playgroud)