我正在测试一个不寻常的情况.我正在使用Espresso来编写我的测试.我知道Espresso和InstrumentationTestCase不是为了做到这一点.
我有一个我在其中一个课程中创建的监听器,它会通知我某个值的变化.我在我的测试套件中使用了监听器.
当我从侦听器获取值时,我需要声明值已更改为此类.
我的问题是测试将在我收到监听器的值之前结束.
private void sendSpeedChanges() {
setStaticSpeed(new Random().nextInt(10) + 2);
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
consoleActivity.onSpeedChanged(getStaticSpeed(), false);
}
});
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
private void createSpeedDelegate() {
EspressoMachineValues.setOnSpeedChangeListener(new EspressoMachineValues.OnSpeedChangeListener() {
@Override
public void onSpeedChanged(double speed) {
//assert speed is correct.
assertTrue(getStaticSpeed() == speed);
}
});
}
Run Code Online (Sandbox Code Playgroud)
这些是我正在使用的两种方法.这createSpeedDelegate()是最开始的电话.然后我打电话sendSpeedChanges.我需要做X次.
sendSpeedChanges()在检查了价值之前我无法打电话onSpeedChange()Thread.sleep();因为监听器在主线程上.我试过添加一个getInstrumentation().wait(2000);也getInstrumentation().waitForIdleSync();很明显,没有工作.
在一个完美的世界里,我会这样做:
for (int i …Run Code Online (Sandbox Code Playgroud) 我需要匹配由红色矩形突出显示的视图.我应该为它写出哪种Espresso表达方式?

这是一个表格布局,所有单元格都是TextView的实例.单元格视图没有唯一的ID.感兴趣的视图可能包含也可能没有文本.我所知道的是,这种观点总是位于"食品集团"细胞之下.
任何线索都会受到欢迎.
android hamcrest android-layout android-testing android-espresso
我想滚动到显示的当前屏幕的底部,但是
我有一个托管片段F1的活动.单击按钮后,F1将被另一个片段F2替换.当按下后退按钮时,应用程序通过退出过渡动画从F2返回到F1 .
我的Espresso测试用例大致如下:
@Test
public void pressingBackRestorePreviousFragment() {
// we are in F1 and about to switch to F2
onView(withId(R.id.the_button)).perform(click());
// we are now in F2, pressing back should "return" to F1
Espresso.pressBack();
onView(withText("A specific text in F1")).check(matches(isDisplayed());
}
Run Code Online (Sandbox Code Playgroud)
当测试用例以逐步调试模式运行时,上述测试通过.但在正常运行模式下,它会失败.只有当我Thread.sleep(___)在onView(withText(__))测试之前插入才会通过.
我相信更好的技术是替换Thread.sleep()Espresso IdlingResource,但我不确定如何将它与视图动画线程结合.理想情况下,我想重写上面的测试用例
@Test
public void pressingBackRestorePreviousFragment() {
// we are in F1 and about to switch to F2
onView(withId(R.id.the_button)).perform(click());
// we are now in F2, pressing back should …Run Code Online (Sandbox Code Playgroud) android android-fragments android-espresso android-transitions
根据我在Espresso备忘单中看到的内容,有两种方法可以检查View的可见性,isDisplayed()以及isCompletelyDisplayed().
我在主屏幕上有滑动布局,我有几个视图.我正在通过以下命令检查其中一个:
onView(withId(R.id.payment_btn)).check(matches(isDisplayed()));
Run Code Online (Sandbox Code Playgroud)
但是,测试停止并显示以下错误:
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
Run Code Online (Sandbox Code Playgroud)
然后我认为,由于View不可见,我可以通过以下测试来测试它:
onView(withId(R.id.payment_btn)).check(doesNotExist());
Run Code Online (Sandbox Code Playgroud)
但是,测试停止并显示以下消息:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: View is present in the hierarchy:
Expected: is <false>
Got: <true>
Run Code Online (Sandbox Code Playgroud)
所以,任何想法如何检查屏幕上的视图的可见性将不胜感激.谢谢.
我有一个用于UI测试的Espresso测试套件,如下所示:
@RunWith(AndroidJUnit4.class)
public class SpecialUiTests {
@Rule
public final ActivityTestRule<SpecialActivity> activity
= new ActivityTestRule<>(SpecialActivity.class);
@Test
public void specialTest() {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
问题是,该活动需要捆绑包,并在找不到预期的值时崩溃
public class SpecialActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
final String specialValue = getIntent().getBundleExtra(ARG_SPECIAL_BUNDLE)
.getString(KEY_SPECIAL_VALUE);
//Do something with specialValue <--- Crash
}
...
}
Run Code Online (Sandbox Code Playgroud)
我可以设置测试规则并仍然传递活动所期望的参数(包)吗?
录像机产生的代码在录制后立即失败.
原因是,在录制时,我点击年份,年份微调器弹出,我向后滚动,然后选择其中一年.录像机不捕获滚动.
在Xcode中,他们添加了一种滚动到项目的方法.在Espresso中找不到类似的东西.
(使用Android Studio 2.3.)
我想在我的Android应用中测试错误消息。我的错误检查仅在我的editText的焦点更改时发生。
@Test
fun wrongFirstname(){
onView(withId(R.id.txtFirstNameRegister)).perform(typeText(""))
//here I need to lose my focus
onView(withId(R.id.txtFirstNameRegister)).check(matches(hasErrorText(R.string.firstNameCheckError.toString())))
}
Run Code Online (Sandbox Code Playgroud) 这是我的colors.xml
<color name="color_primary">#29c9b9</color>
Run Code Online (Sandbox Code Playgroud)
这是我的xml
<TextView
android:id="@+id/registerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/register"
android:textAllCaps="true"
android:textColor="@color/color_primary"/>
Run Code Online (Sandbox Code Playgroud)
所以我想在TextView中编写Espresso测试检查 android:textColor="@color/color_primary"
所以这是我的测试:
@Test
public void registerTextViewTextColor() {
onView(withId(R.id.registerTextView)).check(matches(withTextColor(Color.parseColor("#29c9b9"))));
}
Run Code Online (Sandbox Code Playgroud)
匹配器:
public static Matcher<View> withTextColor(final int expectedId) {
return new BoundedMatcher<View, TextView>(TextView.class) {
@Override
protected boolean matchesSafely(TextView textView) {
return expectedId == textView.getCurrentTextColor();
}
@Override
public void describeTo(Description description) {
description.appendText("with text color: ");
description.appendValue(expectedId);
}
};
}
Run Code Online (Sandbox Code Playgroud)
并且测试工作正常。
所以我将测试更改为使用color_primary:
@Test
public void registerTextViewTextColor() {
onView(withId(R.id.registerTextView)).check(matches(withTextColor(R.color.color_primary)));}
Run Code Online (Sandbox Code Playgroud)
但是现在我得到了错误:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text color: <2131099686>' doesn't match the selected view. …Run Code Online (Sandbox Code Playgroud)