Best Appium-espresso-driver code snippet using android.support.test.espresso.matcher.ViewMatchers.isChecked
SignInActivityTest.kt
Source:SignInActivityTest.kt
...21import android.support.test.espresso.action.ViewActions.click22import android.support.test.espresso.action.ViewActions.closeSoftKeyboard23import android.support.test.espresso.action.ViewActions.typeText24import android.support.test.espresso.assertion.ViewAssertions.matches25import android.support.test.espresso.matcher.ViewMatchers.isChecked26import android.support.test.espresso.matcher.ViewMatchers.isClickable27import android.support.test.espresso.matcher.ViewMatchers.isDisplayed28import android.support.test.espresso.matcher.ViewMatchers.isEnabled29import android.support.test.espresso.matcher.ViewMatchers.isFocusable30import android.support.test.espresso.matcher.ViewMatchers.withId31import android.support.test.espresso.matcher.ViewMatchers.withText32import android.support.test.filters.LargeTest33import android.support.test.rule.ActivityTestRule34import android.support.test.runner.AndroidJUnit435import android.view.View36import com.google.samples.apps.topeka.base.R37import com.google.samples.apps.topeka.TestLogin38import com.google.samples.apps.topeka.helper.login39import com.google.samples.apps.topeka.helper.logout40import com.google.samples.apps.topeka.model.Avatar41import com.google.samples.apps.topeka.model.TEST_AVATAR42import com.google.samples.apps.topeka.model.TEST_FIRST_NAME43import com.google.samples.apps.topeka.model.TEST_LAST_INITIAL44import org.hamcrest.Matcher45import org.hamcrest.Matchers.equalTo46import org.hamcrest.Matchers.isEmptyOrNullString47import org.hamcrest.Matchers.not48import org.junit.Before49import org.junit.Rule50import org.junit.Test51import org.junit.runner.RunWith52@RunWith(AndroidJUnit4::class)53@LargeTest54class SignInActivityTest {55 @Suppress("unused") // actually used by Espresso56 val rule57 @Rule get() = object :58 ActivityTestRule<SignInActivity>(SignInActivity::class.java) {59 override fun beforeActivityLaunched() {60 InstrumentationRegistry.getTargetContext().logout()61 login = TestLogin62 }63 override fun getActivityIntent(): Intent {64 val targetContext = InstrumentationRegistry.getTargetContext()65 return Intent(targetContext, SignInActivity::class.java).putExtra("EDIT", true)66 }67 }68 @Before fun clearPreferences() {69 InstrumentationRegistry.getTargetContext().logout()70 }71 @Test fun checkFab_initiallyNotDisplayed() {72 onView(withId(R.id.done)).check(matches(not(isDisplayed())))73 }74 @Test fun signIn_withoutFirstNameFailed() {75 inputData(null, TEST_LAST_INITIAL, TEST_AVATAR)76 onDoneView().check(matches(not(isDisplayed())))77 }78 @Test fun signIn_withoutLastInitialFailed() {79 inputData(TEST_FIRST_NAME, null, TEST_AVATAR)80 onDoneView().check(matches(not(isDisplayed())))81 }82 @Test fun signIn_withoutAvatarFailed() {83 inputData(TEST_FIRST_NAME, TEST_LAST_INITIAL, null)84 onDoneView().check(matches(not(isDisplayed())))85 }86 @Test fun signIn_withAllPlayerPreferencesSuccessfully() {87 inputData(TEST_FIRST_NAME, TEST_LAST_INITIAL, TEST_AVATAR)88 onDoneView().check(matches(isDisplayed()))89 }90 /* TODO Debug: Espresso does currently not continue after this test. Commenting to keep pace.91 @Test fun signIn_performSignIn() {92 inputData(TEST_FIRST_NAME, TEST_LAST_INITIAL, TEST_AVATAR)93 onDoneView().perform(click())94 assertThat(InstrumentationRegistry.getTargetContext().isLoggedIn(), `is`(true))95 }96 */97 private fun onDoneView() = onView(withId(R.id.done))98 @Test fun signIn_withLongLastName() {99 typeAndHideKeyboard(R.id.last_initial, TEST_FIRST_NAME)100 val expectedValue = TEST_FIRST_NAME[0].toString()101 onView(withId(R.id.last_initial)).check(matches(withText(expectedValue)))102 }103 private fun inputData(firstName: String?, lastInitial: String?, avatar: Avatar?) {104 if (firstName != null) typeAndHideKeyboard(R.id.first_name, firstName)105 if (lastInitial != null) typeAndHideKeyboard(R.id.last_initial, lastInitial)106 if (avatar != null) clickAvatar(avatar)107 }108 private fun typeAndHideKeyboard(targetViewId: Int, text: String) {109 onView(withId(targetViewId)).perform(typeText(text), closeSoftKeyboard())110 }111 private fun clickAvatar(avatar: Avatar) {112 onData(equalTo(avatar))113 .inAdapterView(withId(R.id.avatars))114 .perform(click())115 }116 @Test fun firstName_isInitiallyEmpty() = editTextIsEmpty(R.id.first_name)117 @Test fun lastInitial_isInitiallyEmpty() = editTextIsEmpty(R.id.last_initial)118 private fun editTextIsEmpty(id: Int) {119 onView(withId(id)).check(matches(withText(isEmptyOrNullString())))120 }121 @Test fun avatar_allDisplayed() = checkOnAvatar(isDisplayed())122 @Test fun avatar_isEnabled() = checkOnAvatar(isEnabled())123 @Test fun avatar_notFocusable() = checkOnAvatar(not(isFocusable()))124 @Test fun avatar_notClickable() = checkOnAvatar(not(isClickable()))125 @Test fun avatar_noneChecked() = checkOnAvatar(not(isChecked()))126 private fun checkOnAvatar(matcher: Matcher<View>) {127 (0 until Avatar.values().size).forEach {128 onData(equalTo(Avatar.values()[it]))129 .inAdapterView(withId(R.id.avatars))130 .check(matches(matcher))131 }132 }133}...
ViewMatchersTest.kt
Source:ViewMatchersTest.kt
...21import android.support.test.espresso.matcher.ViewMatchers.hasDescendant22import android.support.test.espresso.matcher.ViewMatchers.hasImeAction23import android.support.test.espresso.matcher.ViewMatchers.hasSibling24import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom25import android.support.test.espresso.matcher.ViewMatchers.isChecked26import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA27import android.support.test.espresso.matcher.ViewMatchers.isDisplayed28import android.support.test.espresso.matcher.ViewMatchers.isEnabled29import android.support.test.espresso.matcher.ViewMatchers.isFocusable30import android.support.test.espresso.matcher.ViewMatchers.isSelected31import android.support.test.espresso.matcher.ViewMatchers.supportsInputMethods32import android.support.test.espresso.matcher.ViewMatchers.withChild33import android.support.test.espresso.matcher.ViewMatchers.withClassName34import android.support.test.espresso.matcher.ViewMatchers.withContentDescription35import android.support.test.espresso.matcher.ViewMatchers.withHint36import android.support.test.espresso.matcher.ViewMatchers.withId37import android.support.test.espresso.matcher.ViewMatchers.withParent38import android.support.test.espresso.matcher.ViewMatchers.withText39import org.hamcrest.CoreMatchers.allOf40import org.hamcrest.CoreMatchers.`is`41import org.hamcrest.CoreMatchers.not42/**43 * Lists all ViewMatchers. ViewMatchers here are without functional load.44 * This is done for demonstration purposes.45 */46@RunWith(AndroidJUnit4::class)47class ViewMatchersTest {48 @Test49 fun userProperties() {50 onView(withId(R.id.fab_add_task))51 onView(withText("All TO-DOs"))52 onView(withContentDescription(R.string.menu_filter))53 onView(hasContentDescription())54 onView(withHint(R.string.name_hint))55 }56 @Test57 fun uiProperties() {58 onView(isDisplayed())59 onView(isEnabled())60 onView(isChecked())61 onView(isSelected())62 }63 @Test64 fun objectMatcher() {65 onView(not<View>(isChecked()))66 onView(allOf<View>(withText("item 1"), isChecked()))67 }68 @Test69 fun hierarchy() {70 onView(withParent(withId(R.id.todo_item)))71 onView(withChild(withText("item 2")))72 onView(isDescendantOfA(withId(R.id.todo_item)))73 onView(hasDescendant(isChecked()))74 .check(matches(isDisplayed()))75 .check(matches(isFocusable()))76 onView(hasSibling(withContentDescription(R.string.menu_filter)))77 }78 @Test79 fun input() {80 onView(supportsInputMethods())81 onView(hasImeAction(EditorInfo.IME_ACTION_SEND))82 }83 @Test84 fun classMatchers() {85 onView(isAssignableFrom(CheckBox::class.java))86 onView(withClassName(`is`(FloatingActionButton::class.java.canonicalName)))87 }...
ConfigureWidgetActivityTest.kt
Source:ConfigureWidgetActivityTest.kt
...7import android.support.test.espresso.action.ViewActions.click8import android.support.test.espresso.assertion.ViewAssertions.matches9import android.support.test.espresso.contrib.ActivityResultMatchers.hasResultCode10import android.support.test.espresso.matcher.ViewMatchers.assertThat11import android.support.test.espresso.matcher.ViewMatchers.isChecked12import android.support.test.espresso.matcher.ViewMatchers.isDisplayed13import android.support.test.espresso.matcher.ViewMatchers.isNotChecked14import android.support.test.espresso.matcher.ViewMatchers.withId15import android.support.test.filters.LargeTest16import android.support.test.rule.ActivityTestRule17import android.support.test.rule.GrantPermissionRule18import android.support.test.runner.AndroidJUnit419import me.thanel.linecalendar.R20import me.thanel.linecalendar.preference.IndicatorStyle21import me.thanel.linecalendar.preference.WidgetPreferences22import org.hamcrest.CoreMatchers.not23import org.hamcrest.Matchers.equalTo24import org.junit.Before25import org.junit.Rule26import org.junit.Test27import org.junit.runner.RunWith28@LargeTest29@RunWith(AndroidJUnit4::class)30class ConfigureWidgetActivityTest {31 @Suppress("MemberVisibilityCanBePrivate")32 @get:Rule33 val activityTestRule =34 object : ActivityTestRule<ConfigureWidgetActivity>(ConfigureWidgetActivity::class.java) {35 override fun getActivityIntent(): Intent {36 val targetContext = InstrumentationRegistry.getInstrumentation().targetContext37 return Intent(targetContext, ConfigureWidgetActivity::class.java)38 .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 1)39 }40 }41 @Suppress("unused")42 @get:Rule43 val grantPermissionRule: GrantPermissionRule =44 GrantPermissionRule.grant(android.Manifest.permission.READ_CALENDAR)45 private lateinit var preferences: WidgetPreferences46 private lateinit var tempPreferences: WidgetPreferences47 @Before48 fun initPreferences() {49 val targetContext = InstrumentationRegistry.getInstrumentation().targetContext50 preferences = WidgetPreferences(targetContext, 1)51 preferences.clear()52 tempPreferences = WidgetPreferences(targetContext, AppWidgetManager.INVALID_APPWIDGET_ID)53 tempPreferences.clear()54 }55 @Test56 fun uncheckHeaderSwitch_hidesHeaderAndUpdatesPreference() {57 // By default should enable header58 onView(withId(R.id.headerEnabledSwitch)).check(matches(isChecked()))59 onView(withId(R.id.eventsHeader)).check(matches(isDisplayed()))60 // Uncheck header switch61 onView(withId(R.id.headerEnabledSwitch)).perform(click())62 // Verify that the header has been hidden63 onView(withId(R.id.headerEnabledSwitch)).check(matches(isNotChecked()))64 onView(withId(R.id.eventsHeader)).check(matches(not(isDisplayed())))65 // ...and preference updated66 assertThat(tempPreferences.isHeaderEnabled, equalTo(false))67 }68 @Test69 fun clickFinishFab_closesActivityWithOkResult() {70 onView(withId(R.id.finishFab)).perform(click())71 assertThat(activityTestRule.activity.isFinishing, equalTo(true))72 assertThat(activityTestRule.activityResult, hasResultCode(Activity.RESULT_OK))73 }74 @Test75 fun indicatorStyle_updatesViewsAndPreference() {76 // By default should check circle style77 onView(withId(R.id.indicatorStyleNone)).check(matches(not(isChecked())))78 onView(withId(R.id.indicatorStyleCircle)).check(matches(isChecked()))79 onView(withId(R.id.indicatorStyleRoundedRect)).check(matches(not(isChecked())))80 // Click on "none" style81 onView(withId(R.id.indicatorStyleNone)).perform(click())82 // Verify that radio button state has been updated83 onView(withId(R.id.indicatorStyleNone)).check(matches(isChecked()))84 onView(withId(R.id.indicatorStyleCircle)).check(matches(not(isChecked())))85 onView(withId(R.id.indicatorStyleRoundedRect)).check(matches(not(isChecked())))86 // ...and preference modified87 assertThat(tempPreferences.indicatorStyle, equalTo(IndicatorStyle.None))88 }89}...
MortgageSimulationInstrumentTest.kt
Source:MortgageSimulationInstrumentTest.kt
...23 @Test24 @LargeTest25 fun testMortgageSimulation(){26 onView(ViewMatchers.withId(R.id.action_mortgage_simulation)).perform(ViewActions.click())27 assertTrue(activityRule.activity.bottom_Navigation_View.menu.findItem(R.id.action_mortgage_simulation).isChecked)28 onView(ViewMatchers.withId(R.id.price_mortgage_edit_text)).perform(ViewActions.typeText("210000"))29 onView(withId(R.id.price_mortgage_edit_text)).check(matches(withText("210000")))30 onView(ViewMatchers.withId(R.id.provision_mortgage_editext)).perform(ViewActions.typeText("10000"))31 onView(withId(R.id.provision_mortgage_editext)).check(matches(withText("10000")))32 onView(ViewMatchers.withId(R.id.simulate_mortgage_button)).perform(ViewActions.click())33 assertEquals("Borrowed money: $ 200000", activityRule.activity.money_mortgage_textView.text.toString())34 assertEquals("Interest rate: 0.92%", activityRule.activity.rate_mortgage_textview.text.toString())35 assertEquals("Your monthly: $ 8413.43", activityRule.activity.monthly_mortgage_textView.text.toString())36 assertEquals("Cost of the mortgage: $ 1922.32", activityRule.activity.cost_mortgage_textview.text.toString())37 }38}...
ViewMatchers.kt
Source:ViewMatchers.kt
...6import mozilla.components.support.android.test.Matchers.maybeInvertMatcher7import org.hamcrest.CoreMatchers.not8import org.hamcrest.Matcher9import androidx.test.espresso.matcher.ViewMatchers.hasFocus as espressoHasFocus10import androidx.test.espresso.matcher.ViewMatchers.isChecked as espressoIsChecked11import androidx.test.espresso.matcher.ViewMatchers.isDisplayed as espressoIsDisplayed12import androidx.test.espresso.matcher.ViewMatchers.isEnabled as espressoIsEnabled13import androidx.test.espresso.matcher.ViewMatchers.isSelected as espressoIsSelected14// These functions are defined at the top-level so they appear in autocomplete, like the static methods on15// Android's [ViewMatchers] class.16/**17 * The existing [espressoHasFocus] function but uses a boolean to invert rather than requiring the [not] matcher.18 */19fun hasFocus(hasFocus: Boolean): Matcher<View> = maybeInvertMatcher(espressoHasFocus(), hasFocus)20/**21 * The existing [espressoIsChecked] function but uses a boolean to invert rather than requiring the [not] matcher.22 */23fun isChecked(isChecked: Boolean): Matcher<View> = maybeInvertMatcher(espressoIsChecked(), isChecked)24/**25 * The existing [espressoIsDisplayed] function but uses a boolean to invert rather than requiring the [not] matcher.26 */27fun isDisplayed(isDisplayed: Boolean): Matcher<View> = maybeInvertMatcher(espressoIsDisplayed(), isDisplayed)28/**29 * The existing [espressoIsEnabled] function but uses a boolean to invert rather than requiring the [not] matcher.30 */31fun isEnabled(isEnabled: Boolean): Matcher<View> = maybeInvertMatcher(espressoIsEnabled(), isEnabled)32/**33 * The existing [espressoIsSelected] function but uses a boolean to invert rather than requiring the [not] matcher.34 */35fun isSelected(isSelected: Boolean): Matcher<View> = maybeInvertMatcher(espressoIsSelected(), isSelected)...
LoginActivityTest.kt
Source:LoginActivityTest.kt
...29 onView(withId(R.id.et_password))30 .perform(clearText(), replaceText("123456"), closeSoftKeyboard())31 .check(matches(withText("123456")))32 onView(withId(R.id.cb_remember_pass))33 .check(matches(isChecked()))34 onView(withId(R.id.cb_autologin))35 .perform(click())36 .check(matches(isChecked()))37 //ç¹å»ç»å½æé®38 onView(withId(R.id.bt_login))39 .perform(click())40 Thread.sleep(1000)41 }42}...
ViewMatchersKtTest.kt
Source:ViewMatchersKtTest.kt
...9import org.hamcrest.Matcher10import org.junit.Test11private val BOOLEAN_VIEW_MATCHER_TO_UNDERLYING_MATCHER: List<Pair<(Boolean) -> Matcher<View>, Matcher<View>>> = listOf(12 ::hasFocus to ViewMatchers.hasFocus(),13 ::isChecked to ViewMatchers.isChecked(),14 ::isDisplayed to ViewMatchers.isDisplayed(),15 ::isEnabled to ViewMatchers.isEnabled(),16 ::isSelected to ViewMatchers.isSelected()17)18class ViewMatchersKtTest {19 @Test20 fun `WHEN checking the unmodified ViewMatcher THEN it equals the underlying ViewMatcher`() {21 BOOLEAN_VIEW_MATCHER_TO_UNDERLYING_MATCHER.forEach { (input, expected) ->22 assertEqualsMatchers(expected, input(true))23 }24 }25 @Test26 fun `WHEN checking the modified ViewMatcher tHEN it equals the inversion of the underlying ViewMatcher`() {27 BOOLEAN_VIEW_MATCHER_TO_UNDERLYING_MATCHER.forEach { (input, inversionOfExpected) ->...
ViewAssertions.kt
Source:ViewAssertions.kt
...12fun ViewInteraction.hasText(@StringRes textId: Int) = check(matches(withText(textId)))13fun ViewInteraction.hasText(text: String) = check(matches(withText(text)))14fun ViewInteraction.isEnabled() = check(matches(ViewMatchers.isEnabled()))15fun ViewInteraction.isDisabled() = check(matches(not(ViewMatchers.isEnabled())))16fun ViewInteraction.isChecked() = check(matches(ViewMatchers.isChecked()))17fun ViewInteraction.isNotChecked() = check(matches(not(ViewMatchers.isChecked())))...
isChecked
Using AI Code Generation
1import static android.support.test.espresso.matcher.ViewMatchers.isChecked;2import static android.support.test.espresso.matcher.ViewMatchers.isClickable;3import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;4import static android.support.test.espresso.matcher.ViewMatchers.isFocusable;5import static android.support.test.espresso.matcher.ViewMatchers.isFocused;6import static android.support.test.espresso.matcher.ViewMatchers.isLongClickable;7import static android.support.test.espresso.matcher.ViewMatchers.isRoot;8import static android.support.test.espresso.matcher.ViewMatchers.isScrollable;9import static android.support.test.espresso.matcher.ViewMatchers.isClickable;10import static android.support.test.espresso.matcher.ViewMatchers.isSelected;11import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;12import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;13import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;14import static android.support.test.espresso.matcher.ViewMatchers.withHint;15import static android.support.test.espresso.matcher.ViewMatchers.withId;16import static android.support.test.espresso.matcher.ViewMatchers.withInputType;17import static android.support.test.espresso.matcher.ViewMatchers.withParent;
isChecked
Using AI Code Generation
1public void testCheckBox() throws Exception {2 onView(withId(R.id.checkbox)).perform(click());3 onView(withId(R.id.checkbox)).check(matches(isChecked()));4}5public void testCheckBox() throws Exception {6 onView(withId(R.id.checkbox)).perform(click());7 onView(withId(R.id.checkbox)).check(matches(isNotChecked()));8}9public void testCheckBox() throws Exception {10 onView(withId(R.id.checkbox)).check(matches(isDisplayed()));11}12public void testCheckBox() throws Exception {13 onView(withId(R.id.checkbox)).check(matches(isNotDisplayed()));14}15public void testCheckBox() throws Exception {16 onView(withId(R.id.checkbox)).check(matches(isEnabled()));17}18public void testCheckBox() throws Exception {19 onView(withId(R.id.checkbox)).check(matches(isNotEnabled()));20}21public void testCheckBox() throws Exception {22 onView(withId(R.id.checkbox)).check(matches(withText("Checkbox")));23}24public void testCheckBox() throws Exception {25 onView(withId(R.id.checkbox)).check(matches(withHint("Checkbox")));26}27public void testCheckBox() throws Exception {28 onView(withId(R.id.checkbox)).check(matches(withContentDescription("Checkbox
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!