使用Espresso进行Android测试时,建议禁用与动画相关的三个选项。假设通过这样做我们将阻止主线程绘制动画,线程将在我们期望他时变为空闲并且测试不会停在以下行:
progressBar.setVisibility(VISIBLE);
但邪恶的事实是,它不起作用!
我在设备的开发者选项中将window animation scale
,transition animation scale
和animator duration scale
设置为off
。然后我运行我的测试:
@RunWith(AndroidJUnit4.class)
public class ExchangeRatesActivityTest {
@Rule
public ActivityTestRule<ExchangeRatesActivity> mActivityRule = new ActivityTestRule<>(
ExchangeRatesActivity.class);
@Test
public void test() {
onView(withId(R.id.amountEditText))
.perform(typeText("100"));
onView(withId(R.id.progressBar))
.check(matches(not(isDisplayed())));
onView(withId(R.id.convertButton))
.perform(click());
}
}
在最后.perform(click())
测试暂停一段时间后,我得到了这个:
android.support.test.espresso.PerformException: Error performing 'single click - At Coordinates: 719, 735 and precision: 16, 16' on view 'with id: com.sample.app:id/convertButton'.
更多事实: