2017-10-17 37 views

回答

0

可以刷卡一次,如果您的顯示TextView的驗證執行刷卡動作。如果不是,再次刷卡。

或者,您可以在您驗證了TextView的方法...如果「NoMatchingViewException」被拋出,你刷卡。

像這樣(沒有測試的代碼):

public static boolean swipeUntilExists(int resourceId) { 
    final ViewInteraction uiElement = onView(withId(resourceId)); 
    boolean isVisible = false; 
    while (!isVisible) { 
     try { 

      // do swipe here 

      uiElement.check(matches(isDisplayed())); 
      isVisible = true; 
     } catch (NoMatchingViewException e) { 
      // do nothing here 
     } 
    } 
    return isVisible; 
} 
0

這可以在一個公正的一個循環,使刷卡完成,然後檢查該元素在try/catch塊是可見直到到達瀏覽量結束或該項目被發現。

如果達到瀏覽量的結束,該項目沒有找到,那麼你可以這樣做Assert.fail(「元素還沒有被發現。」);

試試這個代碼:

boolean found = false; 
int i = 0; 
while (!found && i < NUM_PAGES) { 
    onView(withId(R.id.viewPager)).perform(swipeLeft()); 
    SystemClock.sleep(500); 

    try { 
     if (checkElementVisible()) { 
      found = true; 
     } 
    } catch (Exception e) { 
     // The search continues 
    } 
    i++; 
} 

if (!found) { 
    Assert.fail("The element has not been found."); 
}