2017-04-13 62 views
0

我有TabLayoutViewPager,第3頁有一個SwitchCompat我想執行一個click()上,但它什麼都沒做。Espresso:單擊()上SwitchCompat不工作

@Test 
public void checkSummaryPageUpdates_worksCorrect() { 

    onView(withId(R.id.view_pager)).perform(swipeLeft()); 
    onView(withId(R.id.view_pager)).perform(swipeLeft()); 

    onView(withId(R.id.gs_switch)).check(matches(isDisplayed())); 
    onView(withId(R.id.gs_switch)).perform(click()); 
    onView(withId(R.id.gs_switch)).check(matches(isChecked())); // AssertionFailed 

} 

這是錯誤

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with checkbox state: is ' doesn't match the selected view. Expected: with checkbox state: is true

的SwitchCompat似乎有以下特性:

Got: "SwitchCompat{id=2131689643, res-name=gs_switch, visibility=VISIBLE, width=996, height=100, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=GLÜCKSSPIRALE +5,00 €, input-type=0, ime-target=false, has-links=false, is-checked=false}"

錯誤說,開關沒有被選中,但這個問題似乎發生之前,導致開關只是保持未選中,perform(click())似乎什麼都不做。任何人都可以幫忙

回答

1

我發佈了一個備用解決方案,它涉及到創建一個自定義ViewAction來設置開關狀態(或任何可檢查的事情),而不是使用點擊操作。這可以確保您的操作與狀態無關。

https://stackoverflow.com/a/39650813/1947601

所以,不管它是以前籤與否,你保證,這將是切換到你所期望的狀態。

+0

是的,謝謝你我只是用這個,它工作得很好。但是,爲什麼click()不起作用?它不應該只與perform.click()一起工作? – dan