android.support.test.espresso.matcher.ViewMatchers Java Examples

The following examples show how to use android.support.test.espresso.matcher.ViewMatchers. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Cortado_Tests.java    From cortado with Apache License 2.0 6 votes vote down vote up
@Test
public void addMatcher_doesNotNegateMatcher_when_negateNextMatcher_isFalse() {
    final Start.Matcher matcher = Cortado.view();
    final Cortado cortado = matcher.getCortado();
    assertThat(cortado.matchers).hasSize(0);
    assertThat(cortado.negateNextMatcher).isFalse();
    // no matchers added, negateNextMatcher is false

    org.hamcrest.Matcher<View> viewMatcher = ViewMatchers.withText("test");
    org.hamcrest.Matcher<View> negatedViewMatcher = Matchers.not(viewMatcher);

    cortado.negateNextMatcher = false;

    cortado.addMatcher(viewMatcher);
    assertThat(cortado.matchers).hasSize(1);
    // one matcher added

    assertThat(cortado.negateNextMatcher).isFalse();
    // negateNextMatcher is still false

    final Matcher<? super View> addedMatcher = cortado.matchers.get(0);
    Utils.assertThat(addedMatcher).isEqualTo(viewMatcher);
    Utils.assertThat(addedMatcher).isNotEqualTo(negatedViewMatcher);
}
 
Example #2
Source File: InjectArrayExtrasTest.java    From PrettyBundle with Apache License 2.0 6 votes vote down vote up
public void testArrayExtrasDisplay() throws Exception {
    // Open TestArrayActivity
    Espresso.onView(ViewMatchers.withText(R.string.test_array_extras)).perform(ViewActions.click());

    // Verify result.
    // int arrays
    Espresso.onView(ViewMatchers.withText("{1,2,3}")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    // long arrays
    Espresso.onView(ViewMatchers.withText("{4,5,6}")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    // float arrays
    Espresso.onView(ViewMatchers.withText("{4.1,5.1,6.1}")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    // double arrays
    Espresso.onView(ViewMatchers.withText("{7.2,8.2,9.2}")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    // boolean arrays
    Espresso.onView(ViewMatchers.withText("{true,false,false,true}")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    // string arrays
    Espresso.onView(ViewMatchers.withText("{One,Two,Three}")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    // people
    Espresso.onView(ViewMatchers.withText("{{name='p1',age=18},{name='p2',age=19}}")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

}
 
Example #3
Source File: ShowErrorWithMessageTabActivityTest.java    From android-material-stepper with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldChangeBackToSubtitleFromErrorMessage() {
    //given
    onView(allOf(withId(R.id.button), isCompletelyDisplayed())).perform(doubleClick());
    onView(withId(R.id.stepperLayout)).perform(clickNext());
    onView(withId(R.id.stepperLayout)).perform(clickNext());
    onView(allOf(withId(R.id.button), isCompletelyDisplayed())).perform(doubleClick());

    //when
    onView(withId(R.id.stepperLayout)).perform(clickNext());

    //then
    checkCurrentStepIs(2);
    checkTabSubtitle(0, withEffectiveVisibility(ViewMatchers.Visibility.GONE));
    checkTabSubtitle(1, withText(STEP_SUBTITLE));
    SpoonScreenshotAction.perform(getScreenshotTag(5, "Change back to subtitle test"));
}
 
Example #4
Source File: UsedUpDiscountValidator.java    From px-android with MIT License 6 votes vote down vote up
private void validateAmountView() {
    final Matcher<View> amountDescription = withId(R.id.amount_description);
    final Matcher<View> maxCouponAmount = withId(R.id.max_coupon_amount);
    final Matcher<View> amountBeforeDiscount =
        withId(R.id.amount_before_discount);
    final Matcher<View> finalAmount = withId(R.id.final_amount);
    final Matcher<View> arrow = withId(R.id.blue_arrow);
    onView(amountDescription).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
    onView(maxCouponAmount).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
    onView(amountBeforeDiscount).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
    onView(finalAmount).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
    onView(arrow).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
    onView(amountDescription).check(matches(withText(
        getInstrumentation().getTargetContext().getString(R.string.px_used_up_discount_row))));
    onView(amountDescription).check(matches(hasTextColor(R.color.px_form_text)));
}
 
Example #5
Source File: TestMasterScreen.java    From AndroidMvc with Apache License 2.0 6 votes vote down vote up
@Test
public void should_be_able_to_show_network_error_when_getting_ip_failed() throws Exception{
    //Pre check
    onView(withId(R.id.fragment_master_ipValue)).check(
            matches(withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE)));

    //Prepare
    //Throw an IOException to simulate an network error
    IOException ioExceptionMock = mock(IOException.class);
    when(ipServiceCallMock.execute()).thenThrow(ioExceptionMock);

    //Action
    onView(withId(R.id.fragment_master_ipRefresh)).perform(click());

    //Check value
    onView(withText(R.string.network_error_to_get_ip)).inRoot(
            withDecorView(not(is(rule.getActivity().getWindow().getDecorView()))))
            .check(matches(isDisplayed()));
}
 
Example #6
Source File: AuthActivityTest.java    From AndroidSchool with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyInputFields() throws Exception {
    onView(withId(R.id.loginEdit)).check(matches(allOf(
            withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),
            isFocusable(),
            isClickable(),
            withText("")
    )));

    onView(withId(R.id.passwordEdit)).check(matches(allOf(
            withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE),
            isFocusable(),
            isClickable(),
            withInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD),
            withText("")
    )));
}
 
Example #7
Source File: Cortado_Tests.java    From cortado with Apache License 2.0 6 votes vote down vote up
@Test
public void addMatcher_negatesMatcher_when_negateNextMatcher_isTrue() {
    final Start.Matcher matcher = Cortado.view();
    final Cortado cortado = matcher.getCortado();
    assertThat(cortado.matchers).hasSize(0);
    assertThat(cortado.negateNextMatcher).isFalse();
    // no matchers added, negateNextMatcher is false

    org.hamcrest.Matcher<View> viewMatcher = ViewMatchers.withText("test");
    org.hamcrest.Matcher<View> negatedViewMatcher = Matchers.not(viewMatcher);

    cortado.negateNextMatcher = true;

    cortado.addMatcher(viewMatcher);
    assertThat(cortado.matchers).hasSize(1);
    // one matcher added

    assertThat(cortado.negateNextMatcher).isFalse();
    // negateNextMatcher is back to false

    final Matcher<? super View> addedMatcher = cortado.matchers.get(0);
    Utils.assertThat(addedMatcher).isNotEqualTo(viewMatcher);
    Utils.assertThat(addedMatcher).isEqualTo(negatedViewMatcher);
}
 
Example #8
Source File: EspIsDisplayedMatcher.java    From espresso-macchiato with MIT License 6 votes vote down vote up
@Override
public boolean matchesSafely(View view) {
    // get visible area for selected view
    // it also tell us if at least a small part visible, when not we can abort here
    Rect visibleParts = new Rect();
    boolean isAtLeastSomethingVisible = view.getGlobalVisibleRect(visibleParts);
    if (!isAtLeastSomethingVisible) {
        return false;
    }

    Rect screen = getScreenWithoutStatusBarActionBar(view);
    int viewHeight = (view.getHeight() > screen.height()) ? screen.height() : view.getHeight();
    int viewWidth = (view.getWidth() > screen.width()) ? screen.width() : view.getWidth();

    double maxArea = viewHeight * viewWidth;
    double visibleArea = visibleParts.height() * visibleParts.width();
    int displayedPercentage = (int) ((visibleArea / maxArea) * 100);
    boolean isViewFullVisible = displayedPercentage >= areaPercentage;
    return isViewFullVisible && withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE).matches(view);
}
 
Example #9
Source File: WireMockActivityInstrumentationTestCase2.java    From AndroidHttpMockingExamples with Apache License 2.0 6 votes vote down vote up
/**
 * Test WireMock
 */
@Test
public void testWiremock() {
    activity = activityRule.getActivity();
    String jsonBody = asset(activity, "atlanta-conditions.json");
    stubFor(get(urlMatching("/api/.*"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withBody(jsonBody)));

    String serviceEndpoint = "http://127.0.0.1:" + BuildConfig.PORT;
    logger.debug("WireMock Endpoint: " + serviceEndpoint);
    activity.setWeatherServiceManager(new WeatherServiceManager(serviceEndpoint));

    onView(ViewMatchers.withId(R.id.editText)).perform(replaceText("atlanta"));
    onView(withId(R.id.button)).perform(click());
    onView(withId(R.id.textView)).check(matches(withText(containsString("GA"))));
}
 
Example #10
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void withResourceName_withMatcher_addsCorrectMatcher() {
    //given
    org.hamcrest.Matcher<String> testMatcher = SimpleMatcher.instance();

    //when
    notCompletable.withResourceName(testMatcher);

    //then
    assertExpectedAddedMatcher(ViewMatchers.withResourceName(testMatcher));
}
 
Example #11
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void withChild_withMatcher_addsCorrectMatcher() {
    //given
    org.hamcrest.Matcher<View> testMatcher = SimpleMatcher.instance();

    //when
    notCompletable.withChild(testMatcher);

    //then
    assertExpectedAddedMatcher(ViewMatchers.withChild(testMatcher));
}
 
Example #12
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void hasErrorText_withMatcher_addsCorrectMatcher() {
    //given
    org.hamcrest.Matcher<String> testMatcher = SimpleMatcher.instance();

    //when
    notCompletable.hasErrorText(testMatcher);

    //then
    assertExpectedAddedMatcher(ViewMatchers.hasErrorText(testMatcher));
}
 
Example #13
Source File: Cortado.java    From cortado with Apache License 2.0 5 votes vote down vote up
@NonNull
final synchronized org.hamcrest.Matcher<View> get() {
    if (cached == null) {
        cached = linker.link(matchers);
        if (assignableFromClass != null) {
            List<org.hamcrest.Matcher<? super View>> assignedMatchers = new ArrayList<>(2);
            assignedMatchers.add(ViewMatchers.isAssignableFrom(assignableFromClass));
            assignedMatchers.add(cached);
            cached = Linker.AND.link(assignedMatchers);
        }
    }
    //noinspection unchecked
    return (org.hamcrest.Matcher<View>) cached;
}
 
Example #14
Source File: MainMenuActivityInstrumentedTest.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
private void checkOneToOneChatsRecyclerViewContent(final List<String> chatNames) {
    RecyclerViewInteraction.<String>onRecyclerView(allOf(withId(R.id.oneToOneChatsRecyclerView), ViewMatchers.isDisplayed()))
            .withItems(chatNames)
            .check(new RecyclerViewInteraction.ItemViewAssertion<String>() {
                @Override
                public void check(String chatName, View view, NoMatchingViewException e) {
                    matches(hasDescendant(withText(chatName))).check(view, e);
                }
            });
}
 
Example #15
Source File: TestManualInputActivity.java    From polling-station-app with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testInvalidData() {
    closeSoftKeyboard();
    onView(withId(R.id.submit_button)).perform(click());
    onView(ViewMatchers.withId(R.id.doc_num))
            .check(ViewAssertions.matches(
                    ViewMatchers.hasErrorText(
                            activityRule.getActivity().getString(R.string.errInputDocNum))));
}
 
Example #16
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void hasImeAction_withInteger_addsCorrectMatcher() {
    //given
    //when
    notCompletable.hasImeAction(1);

    //then
    assertExpectedAddedMatcher(ViewMatchers.hasImeAction(1));
}
 
Example #17
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void hasContentDescription_addsCorrectMatcher() {
    //given
    //when
    notCompletable.hasContentDescription();

    //then
    assertExpectedAddedMatcher(ViewMatchers.hasContentDescription());
}
 
Example #18
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void isAssignableFrom_addsCorrectMatcher() {
    //given
    final Class<ImageView> test = ImageView.class;

    //when
    notCompletable.isAssignableFrom(test);
    assertExpectedAddedMatcher(ViewMatchers.isAssignableFrom(test));
}
 
Example #19
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void withContentDescription_withString_addsCorrectMatcher() {
    //given
    //when
    notCompletable.withContentDescription("test");

    //then
    assertExpectedAddedMatcher(ViewMatchers.withContentDescription("test"));
}
 
Example #20
Source File: ChatMembersActivityInstrumentedTest.java    From mangosta-android with Apache License 2.0 5 votes vote down vote up
private void checkRecyclerViewContent(final List<String> users) {
    RecyclerViewInteraction.<String>onRecyclerView(allOf(withId(R.id.chatMembersRecyclerView), ViewMatchers.isDisplayed()))
            .withItems(users)
            .check(new RecyclerViewInteraction.ItemViewAssertion<String>() {
                @Override
                public void check(String user, View view, NoMatchingViewException e) {
                    matches(hasDescendant(withText(XMPPUtils.fromJIDToUserName(user)))).check(view, e);
                }
            });
}
 
Example #21
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void withSpinnerText_withString_addsCorrectMatcher() {
    //given
    //when
    notCompletable.withSpinnerText("test");

    //then
    assertExpectedAddedMatcher(ViewMatchers.withSpinnerText("test"));
}
 
Example #22
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void withTagKey_addsCorrectMatcher() {
    //given
    //when
    notCompletable.withTagKey(1);

    //then
    assertExpectedAddedMatcher(ViewMatchers.withTagKey(1));
}
 
Example #23
Source File: ShowErrorWithMessageTabActivityTest.java    From android-material-stepper with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGoToTheNextStepWhenVerificationSucceeds() {
    //given
    onView(allOf(withId(R.id.button), isCompletelyDisplayed())).perform(doubleClick());

    //when
    onView(withId(R.id.stepperLayout)).perform(clickNext());

    //then
    checkCurrentStepIs(1);
    checkTabSubtitle(0, withEffectiveVisibility(ViewMatchers.Visibility.GONE));
    checkTabSubtitle(1, withText(STEP_SUBTITLE));
    SpoonScreenshotAction.perform(getScreenshotTag(2, "Verification success test"));
}
 
Example #24
Source File: UserSearchActivityTest.java    From GithubUsersSearchApp with Apache License 2.0 5 votes vote down vote up
@Test
public void searchText_ServiceCallFails_DisplayError(){
    String errorMsg = "Server Error";
    MockGithubUserRestServiceImpl.setDummySearchGithubCallResult(Observable.error(new Exception(errorMsg)));

    onView(allOf(withId(R.id.menu_search), withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))).perform(
            click());  // When using a SearchView, there are two views that match the id menu_search - one that represents the icon, and the other the edit text view. We want to click on the visible one.
    onView(withId(R.id.search_src_text)).perform(typeText("riggaroo"), pressKey(KeyEvent.KEYCODE_ENTER));

   onView(withText(errorMsg)).check(matches(isDisplayed()));

}
 
Example #25
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void withText_withString_addsCorrectMatcher() {
    //given
    //when
    notCompletable.withText("test");

    //then
    assertExpectedAddedMatcher(ViewMatchers.withText("test"));
}
 
Example #26
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void isEnabled_addsCorrectMatcher() {
    //given
    //when
    notCompletable.isEnabled();

    //then
    assertExpectedAddedMatcher(ViewMatchers.isEnabled());
}
 
Example #27
Source File: GalleryActivityTest.java    From u2020-mvp with Apache License 2.0 5 votes vote down vote up
@Test
public void loadData() {
    String tag = "1. Root view visible, progress bar showing";
    onView(isRoot()).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
    tag = "2. Grid shown with images loaded";
    // TODO: remove wait delay, use RxJava's TestScheduler or Espresso.registerIdlingResource
    onView(isRoot()).perform(ViewActions.waitAtLeast(Constants.WAIT_DELAY));
    onView(withId(R.id.gallery_grid)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
}
 
Example #28
Source File: InjectStringExtrasTest.java    From PrettyBundle with Apache License 2.0 5 votes vote down vote up
public void testStartActivityTestStringExtra2UsingDefaultValue() throws Exception {
    final String extra2 = "Nguyen";
    Espresso.onView(ViewMatchers.withHint(R.string.string_extra_2)).perform(ViewActions.typeText(extra2));

    Espresso.closeSoftKeyboard();

    Espresso.onView(ViewMatchers.withText(R.string.submit)).perform(ExtViewActions.waitForSoftKeyboard(), ViewActions.click());

    Espresso.onView(ViewMatchers.withText("Default")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(extra2)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
 
Example #29
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void isJavascriptEnabled_addsCorrectMatcher() {
    //given
    //when
    notCompletable.isJavascriptEnabled();

    //then
    assertExpectedAddedMatcher(ViewMatchers.isJavascriptEnabled());
}
 
Example #30
Source File: NotCompletable_Tests.java    From cortado with Apache License 2.0 5 votes vote down vote up
@Test
public void withParent_withCortadoMatcher_addsCorrectMatcher() {
    //given
    Matcher testMatcher = Cortado.view().withText("Test");

    //when
    notCompletable.withParent(testMatcher);

    //then
    assertExpectedAddedMatcher(ViewMatchers.withParent(testMatcher));
}