2
檢測祝酒詞我正在寫與咖啡一些用戶界面測試,我想斷言敬酒消息。我遇到的問題是我的代碼在棒棒糖上工作,但在棉花糖上失敗,我不明白爲什麼。在棉花糖
我的代碼基本上是打開一個對話框,填寫電子郵件,點擊一個按鈕。這應該提出一個敬酒和它(確認在設備上的視覺檢查),但我的測試未能檢測到吐司。
爲此,我創建了祝酒自定義匹配類:
import android.os.IBinder;
import android.support.test.espresso.Root;
import android.view.WindowManager;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import static com.google.android.exoplayer.util.Assertions.checkArgument;
import static com.google.android.exoplayer.util.Assertions.checkNotNull;
import static org.hamcrest.CoreMatchers.equalTo;
public class ToastMatcher{
public static Matcher<Root> withToastText(String toastText) {
// use preconditions to fail fast when a test is creating an invalid matcher.
checkArgument(!(toastText.equals(null)));
return withToastText(equalTo(toastText));
}
public static Matcher<Root> withToastText(final Matcher<String> matcherText) {
// use preconditions to fail fast when a test is creating an invalid matcher.
checkNotNull(matcherText);
return new TypeSafeMatcher<Root>() {
public void describeTo(Description description) {
description.appendText("is toast");
}
@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) {
// windowToken == appToken means this window isn't contained by any other windows.
// if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
return true;
}
}
return false;
}
};
}
}
至於我的測試案例
@Test
public void success() {
String successMessage = mActivityRule.getActivity().getResources().getString(R.string.info_recover_instructions);
String ok = mActivityRule.getActivity().getResources().getString(android.R.string.ok);
// Click forgot button
onView(withId(R.id.lg_forgot)).perform(click());
// Fill email
onView(withClassName(endsWith("EditText"))).perform(typeText(USERNAME));
// Click OK button
onView(withText(ok))
.check(matches(isEnabled()))
.perform(click());
// Assert Toast
onView(withText(successMessage)).inRoot(ToastMatcher.withToastText(successMessage)).check(matches(isDisplayed()));
}
正如我以前說過,這適用於棒棒糖就好了,但是棉花糖它給我以下錯誤:
android.support.test.espresso.NoMatchingRootException: Matcher 'is toast' did not match any of the following roots: [Root{[email protected], [email protected], has-window-focus=true, layout-params-type=2, layout-params-string=WM.LayoutParams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1820002 fmt=-3 wanim=0x103045c needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1026, height=483, 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=0.0, y=0.0, child-count=1}}, Root{[email protected], [email protected], has-window-focus=false, layout-params-type=2, layout-params-string=WM.LayoutParams{(0,0)(wrapxwrap) gr=#11 sim=#20 ty=2 fl=#1800002 fmt=-3 wanim=0x103045c needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1026, height=598, has-focus=true, has-focusable=true, has-window-focus=false, 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=0.0, y=0.0, child-count=1}}, Root{[email protected], [email protected], has-window-focus=false, layout-params-type=1, layout-params-string=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#81810100 wanim=0x103045b needsMenuKey=2}, decor-view-string=DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=false, 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=0.0, y=0.0, child-count=3}}]
我是否做錯了什麼或應該不要不同的Android棉花糖?
你試過用它的文字趕上烤麪包嗎? – piotrek1543
是的。我試圖用簡單地做onView(withText(successMessage))。檢查(比賽(isDisplayed())),但沒有工作,要麼讓我創建的自定義匹配和它的作品就好了其他Android版本除了MM – PeachMode