Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just interact with visible things #51

Merged
merged 16 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import android.util.Log;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.BaristaScrollActions.scrollTo;
import static com.schibsted.spain.barista.custom.AutocompleteViewActions.replaceAutocomplete;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaAutoCompleteTextViewActions {

Expand All @@ -18,7 +18,7 @@ public static void writeToAutoCompleteTextView(@IdRes int id, String text) {
Log.d("Barista",
"The View's parent is not a ScrollView. Due to the power of Barista, you can ignore this error message");
} finally {
onView(withId(id)).perform(replaceAutocomplete(text));
onView(displayedWithId(id)).perform(replaceAutocomplete(text));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithText;

public class BaristaClickActions {

public static void click(@IdRes int id) {
try {
onView(withId(id)).perform(scrollTo(), ViewActions.click());
onView(displayedWithId(id)).perform(scrollTo(), ViewActions.click());
} catch (PerformException e) {
onView(withId(id)).perform(ViewActions.click());
onView(displayedWithId(id)).perform(ViewActions.click());
}
}

public static void click(String text) {
try {
onView(withText(text)).perform(scrollTo(), ViewActions.click());
onView(displayedWithText(text)).perform(scrollTo(), ViewActions.click());
} catch (PerformException e) {
onView(withText(text)).perform(ViewActions.click());
onView(displayedWithText(text)).perform(ViewActions.click());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.BaristaScrollActions.scrollTo;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaEditTextActions {

Expand All @@ -18,7 +18,7 @@ public static void writeToEditText(@IdRes int id, String text) {
Log.d("Barista",
"The View's parent is not a ScrollView. Due to the power of Barista, you can ignore this error message");
} finally {
onView(withId(id)).perform(replaceText(text));
onView(displayedWithId(id)).perform(replaceText(text));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import android.support.test.espresso.contrib.PickerActions;
import android.widget.DatePicker;

import org.hamcrest.Matchers;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaPickerActions {

public static void setDateOnPicker(int year, int month, int day) {
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, month, day));
onView(withId(android.R.id.button1)).perform(click());
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(
PickerActions.setDate(year, month, day));
onView(displayedWithId(android.R.id.button1)).perform(click());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithText;
import static com.schibsted.spain.barista.custom.HelperMatchers.atPosition;
import static org.hamcrest.Matchers.allOf;

public class BaristaRadioButtonActions {

public static void clickRadioButtonItem(@IdRes int radioGroupId, @IdRes int itemToClickId) {
onView(allOf(withParent(withId(radioGroupId)), withId(itemToClickId))).perform(click());
onView(
allOf(withParent(displayedWithId(radioGroupId)), displayedWithId(itemToClickId))).perform(
click());
}

public static void clickRadioButtonItem(@IdRes int radioGroupId, String text) {
onView(allOf(withParent(withId(radioGroupId)), withText(text))).perform(click());
onView(allOf(withParent(displayedWithId(radioGroupId)), displayedWithText(text))).perform(
click());
}

public static void clickRadioButtonPosition(@IdRes int radioGroupId, int position) {
onView(atPosition(position, withParent(withId(radioGroupId)))).perform(click());
onView(atPosition(position, withParent(displayedWithId(radioGroupId)))).perform(click());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.custom.ClickChildAction.clickChildWithId;
import static com.schibsted.spain.barista.custom.ClickChildAction.clickChildWithText;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;
import static com.schibsted.spain.barista.custom.PerformClickAction.clickUsingPerformClick;
import static org.hamcrest.core.AllOf.allOf;

public class BaristaRecyclerViewActions {

Expand All @@ -26,21 +24,23 @@ public static void clickRecyclerViewItem(@IdRes int recyclerViewId, int... posit
}

private static void performClick(@IdRes int recyclerViewId, int position) {
onView(allOf(withId(recyclerViewId), isDisplayed())).perform(
onView(displayedWithId(recyclerViewId)).perform(
RecyclerViewActions.actionOnItemAtPosition(position, clickUsingPerformClick()));
}

public static void scrollTo(int recyclerViewId, int position) {
onView(allOf(withId(recyclerViewId), isDisplayed())).perform(scrollToPosition(position));
onView(displayedWithId(recyclerViewId)).perform(scrollToPosition(position));
}

public static void clickRecyclerViewItemChild(@IdRes int recyclerViewId, int position, @IdRes int itemToClickId) {
onView(allOf(withId(recyclerViewId), isDisplayed())).perform(
public static void clickRecyclerViewItemChild(@IdRes int recyclerViewId, int position,
@IdRes int itemToClickId) {
onView(displayedWithId(recyclerViewId)).perform(
RecyclerViewActions.actionOnItemAtPosition(position, clickChildWithId(itemToClickId)));
}

public static void clickRecyclerViewItemChild(@IdRes int recyclerViewId, int position, String text) {
onView(allOf(withId(recyclerViewId), isDisplayed())).perform(
public static void clickRecyclerViewItemChild(@IdRes int recyclerViewId, int position,
String text) {
onView(displayedWithId(recyclerViewId)).perform(
RecyclerViewActions.actionOnItemAtPosition(position, clickChildWithText(text)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.swipeLeft;
import static android.support.test.espresso.action.ViewActions.swipeRight;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaSwipeActions {

public static void swipePagerForward(@IdRes int id) {
onView(withId(id)).perform(swipeLeft());
onView(displayedWithId(id)).perform(swipeLeft());
}

public static void swipePagerBack(@IdRes int id) {
onView(withId(id)).perform(swipeRight());
onView(displayedWithId(id)).perform(swipeRight());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.schibsted.spain.barista.custom;

import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.view.View;

import org.hamcrest.Matcher;

import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;

public class DisplayedMatchers {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some javadocs to the class or methods might be helpful for users, to understand the magic of these matchers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wo... Actually, I think that the code is autoexplained. In addition, in this great example, the really nice method name also explains the implementation 😂

displayedWithId(int id) = allOf(withId(id), isDisplayed());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me change the order of the matchers, to make them more readable if possible :·D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-explaining code doesn't work for public APIs, IMHO ^^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this code is not part of the public API, isn't it? It's code that we call from Barista. I know that someone can call it cos it's public, but... that's not what Barista aims to do.

In the other hand, the method does exactly what it explains. I mean, if I know how Espresso works, if I see a matcher called displayedWithId(@ResId int id), it sounds that it matches with displayed views that had the given ID.

Let me skip adding the Javadoc. If it's a issue, we will add in as soon as it adds value to Barista users.


@NonNull
public static Matcher<View> displayedWithId(@IdRes int id) {
return allOf(isDisplayed(), withId(id));
}

@NonNull
public static Matcher<View> displayedWithText(@StringRes int text) {
return allOf(isDisplayed(), withText(text));
}

@NonNull
public static Matcher<View> displayedWithText(String text) {
return allOf(isDisplayed(), withText(text));
}
}