Created
August 26, 2017 22:40
-
-
Save mandemeskel/394ec9db2e1912006d3435bbe7622d0a to your computer and use it in GitHub Desktop.
Record Espresso Test for button that becomes invisible when clicked, using Android Studio 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.enfant.maggie; | |
import android.support.test.espresso.ViewInteraction; | |
import android.support.test.rule.ActivityTestRule; | |
import android.support.test.runner.AndroidJUnit4; | |
import android.test.suitebuilder.annotation.LargeTest; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.ViewParent; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import static android.support.test.espresso.Espresso.onView; | |
import static android.support.test.espresso.action.ViewActions.click; | |
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; | |
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; | |
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription; | |
import static android.support.test.espresso.matcher.ViewMatchers.withId; | |
import static org.hamcrest.Matchers.allOf; | |
@LargeTest | |
@RunWith(AndroidJUnit4.class) | |
public class RecordBtnTests { | |
@Rule | |
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class); | |
@Test | |
public void recordBtnTests() { | |
ViewInteraction appCompatImageButton = onView( | |
allOf(withId(R.id.btnRecord), withContentDescription("record"), | |
childAtPosition( | |
allOf(withId(R.id.lytBtnWrapper), | |
childAtPosition( | |
withId(R.id.lytHome), | |
1)), | |
0), | |
isDisplayed())); | |
appCompatImageButton.perform(click()); | |
ViewInteraction imageButton = onView( | |
allOf(withId(R.id.btnRecord), withContentDescription("record"), | |
childAtPosition( | |
allOf(withId(R.id.lytBtnWrapper), | |
childAtPosition( | |
withId(R.id.lytHome), | |
1)), | |
0), | |
isDisplayed())); | |
imageButton.check(doesNotExist()); | |
} | |
private static Matcher<View> childAtPosition( | |
final Matcher<View> parentMatcher, final int position) { | |
return new TypeSafeMatcher<View>() { | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("Child at position " + position + " in parent "); | |
parentMatcher.describeTo(description); | |
} | |
@Override | |
public boolean matchesSafely(View view) { | |
ViewParent parent = view.getParent(); | |
return parent instanceof ViewGroup && parentMatcher.matches(parent) | |
&& view.equals(((ViewGroup) parent).getChildAt(position)); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment