标签: android-espresso

控制何时自动化测试结束 - Espresso

我正在测试一个不寻常的情况.我正在使用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次.


注意:

  • 检索信息大约需要200毫秒(平均而言).
  • sendSpeedChanges()在检查了价值之前我无法打电话onSpeedChange()
  • 我无法使用,Thread.sleep();因为监听器在主线程上.

我试过添加一个getInstrumentation().wait(2000);getInstrumentation().waitForIdleSync();很明显,没有工作.

在一个完美的世界里,我会这样做:

   for (int i …
Run Code Online (Sandbox Code Playgroud)

java android android-testing android-espresso

2
推荐指数
1
解决办法
2408
查看次数

检查ListView是否具有特定的项目数,并使用Espresso滚动到最后一项

我正在尝试使用谷歌的Espresso实现这一点,但是我找不到ViewAssertion或者ViewAction允许我这样做.

我不确定这些是否可以使用捆绑在匹配器中完成,或者我应该自己编写.

谢谢!

android integration-testing android-espresso

2
推荐指数
1
解决办法
8513
查看次数

如何使用Espresso匹配此表格单元格?

我需要匹配由红色矩形突出显示的视图.我应该为它写出哪种Espresso表达方式?

在此输入图像描述

这是一个表格布局,所有单元格都是TextView的实例.单元格视图没有唯一的ID.感兴趣的视图可能包含也可能没有文本.我所知道的是,这种观点总是位于"食品集团"细胞之下.

任何线索都会受到欢迎.

android hamcrest android-layout android-testing android-espresso

2
推荐指数
1
解决办法
963
查看次数

滚动屏幕到底部没有scrollview和没有ID,没有firstChild android-espresso自动化

我想滚动到显示的当前屏幕的底部,但是

  1. 应用程序没有任何ScrollView.应用程序具有Horizo​​ntalScrollView但它不在上下文中.
  2. 在屏幕上使用带有ID的onView,TableLayout.但它会在层次结构中匹配多个视图时抛出错误.
  3. 使用onView,通过获取firstChild()但仍然会抛出错误而无法执行操作,使用类型parentMatcher的第一个子视图执行'滚动到'视图'时出错'.
  4. 试过onData(hasToString(startsWith().但是它会在层次结构中匹配多个视图时抛出错误.
  5. 试过其他方法,如获取当前的监视器和活动但仍然无法正常工作.

android-espresso

2
推荐指数
1
解决办法
3400
查看次数

Android Espresso IdlingResources和Fragment/Activity Transitions

我有一个托管片段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

2
推荐指数
1
解决办法
1801
查看次数

如何使用Espresso检查屏幕外的View的可见性?

根据我在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)

所以,任何想法如何检查屏幕上的视图的可见性将不胜感激.谢谢.

android android-espresso

2
推荐指数
1
解决办法
5099
查看次数

Junit ActivityTestRule中缺少参数的活动崩溃

我有一个用于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)

我可以设置测试规则并仍然传递活动所期望的参数(包)吗?

android junit4 android-espresso uitest

2
推荐指数
1
解决办法
333
查看次数

使用DatePicker记录Espresso测试

录像机产生的代码在录制后立即失败.

原因是,在录制时,我点击年份,年份微调器弹出,我向后滚动,然后选择其中一年.录像机不捕获滚动.

在Xcode中,他们添加了一种滚动到项目的方法.在Espresso中找不到类似的东西.

(使用Android Studio 2.3.)

android android-espresso

2
推荐指数
1
解决办法
2301
查看次数

Android:使用espresso测试时如何失去对editText的关注

我想在我的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)

android kotlin android-espresso

2
推荐指数
1
解决办法
821
查看次数

Android-Espresso:使用R.color.xxx无法检查textView文本颜色

这是我的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)

android-espresso

2
推荐指数
1
解决办法
984
查看次数