Best Appium-espresso-driver code snippet using android.support.test.espresso.contrib.DrawerMatchers.isOpen
DrawerTest.kt
Source:DrawerTest.kt
...11import androidx.test.espresso.Espresso.onView12import androidx.test.espresso.assertion.ViewAssertions.matches13import androidx.test.espresso.contrib.DrawerActions.open14import androidx.test.espresso.contrib.DrawerMatchers.isClosed15import androidx.test.espresso.contrib.DrawerMatchers.isOpen16import androidx.test.espresso.contrib.NavigationViewActions.navigateTo17import androidx.test.espresso.matcher.ViewMatchers18import androidx.test.espresso.matcher.ViewMatchers.hasDescendant19import androidx.test.espresso.matcher.ViewMatchers.withId20import androidx.test.ext.junit.runners.AndroidJUnit421import androidx.test.filters.MediumTest22import com.chari.ic.todoapp.data.source.FakeToDoRepository23import com.chari.ic.todoapp.data.source.LoggedInStubDataStoreRepository24import com.chari.ic.todoapp.di.RepositoryModule25import com.chari.ic.todoapp.repository.Repository26import com.chari.ic.todoapp.repository.datastore.IDataStoreRepository27import dagger.Binds28import dagger.Module29import dagger.hilt.InstallIn30import dagger.hilt.android.testing.HiltAndroidRule31import dagger.hilt.android.testing.HiltAndroidTest32import dagger.hilt.android.testing.UninstallModules33import dagger.hilt.components.SingletonComponent34import kotlinx.coroutines.ExperimentalCoroutinesApi35import kotlinx.coroutines.test.runBlockingTest36import org.junit.After37import org.junit.Assert.*38import org.junit.Before39import org.junit.Rule40import org.junit.Test41import org.junit.runner.RunWith42import java.io.IOException43import javax.inject.Inject44import javax.inject.Singleton45//@MediumTest46@HiltAndroidTest47@UninstallModules(RepositoryModule::class)48@ExperimentalCoroutinesApi49@RunWith(AndroidJUnit4::class)50class DrawerTest {51 @get: Rule(order = 0)52 val hiltRule = HiltAndroidRule(this)53 @get:Rule(order = 1)54 var instantTaskExecutorRule = InstantTaskExecutorRule()55 @get:Rule(order = 2)56 var mainCoroutineRule = MainCoroutineRule()57 @Inject58 lateinit var fakeRepository: FakeToDoRepository59 @Before60 fun setUp() {61 hiltRule.inject()62 }63 @After64 @Throws(IOException::class)65 fun tearDown() {66 mainCoroutineRule.runBlockingTest {67 fakeRepository.resetRepository("1")68 }69 }70 @Module71 @InstallIn(SingletonComponent::class)72 abstract class RepositoryTestModule {73 @Singleton74 @Binds75 abstract fun bindToDoRepository(repository: FakeToDoRepository): Repository76 @Singleton77 @Binds78 abstract fun bindDataStoreRepository(dataStoreRepository: LoggedInStubDataStoreRepository): IDataStoreRepository79 }80 @Test81 fun signOut_navigateToIntroFragment() {82 val navController = TestNavHostController(ApplicationProvider.getApplicationContext())83 navController.setViewModelStore(ViewModelStore())84 navController.setGraph(R.navigation.my_nav)85 val activityScenario = ActivityScenario.launch(MainActivity::class.java)86 activityScenario.onActivity {87 val navHostFragment = it.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_container) as NavHostFragment88 Navigation.setViewNavController(navHostFragment.requireView(), navController)89 }90 onView(withId(R.id.drawer_layout))91 .check(matches(isClosed(Gravity.START)))92 .perform(open())93 .check(matches(isOpen(Gravity.START)))94 .check(matches(hasDescendant(withId(R.id.nav_view))))95 onView(withId(R.id.nav_view))96 .perform(navigateTo(R.id.nav_sign_out))97 onView(withId(R.id.sign_in_btn))98 .check(matches(ViewMatchers.isDisplayed()))99 activityScenario.moveToState(Lifecycle.State.DESTROYED)100 activityScenario.close()101 }102}...
GardenActivityTest.kt
Source:GardenActivityTest.kt
...20import android.support.test.espresso.action.ViewActions21import android.support.test.espresso.action.ViewActions.click22import android.support.test.espresso.assertion.ViewAssertions.matches23import android.support.test.espresso.contrib.DrawerMatchers.isClosed24import android.support.test.espresso.contrib.DrawerMatchers.isOpen25import android.support.test.espresso.contrib.NavigationViewActions.navigateTo26import android.support.test.espresso.matcher.ViewMatchers.isDisplayed27import android.support.test.espresso.matcher.ViewMatchers.isRoot28import android.support.test.espresso.matcher.ViewMatchers.withContentDescription29import android.support.test.espresso.matcher.ViewMatchers.withId30import android.support.test.rule.ActivityTestRule31import android.view.Gravity32import com.google.samples.apps.sunflower.utilities.getToolbarNavigationContentDescription33import org.junit.Assert.assertEquals34import org.junit.Rule35import org.junit.Test36class GardenActivityTest {37 @Rule @JvmField38 var activityTestRule = ActivityTestRule(GardenActivity::class.java)39 @Test fun clickOnAndroidHomeIcon_OpensAndClosesNavigation() {40 // Check that drawer is closed at startup41 onView(withId(R.id.drawer_layout)).check(matches(isClosed(Gravity.START)))42 clickOnHomeIconToOpenNavigationDrawer()43 checkDrawerIsOpen()44 }45 @Test fun onRotate_NavigationStaysOpen() {46 clickOnHomeIconToOpenNavigationDrawer()47 // Rotate device to landscape48 activityTestRule.activity.requestedOrientation = SCREEN_ORIENTATION_LANDSCAPE49 checkDrawerIsOpen()50 // Rotate device back to portrait51 activityTestRule.activity.requestedOrientation = SCREEN_ORIENTATION_PORTRAIT52 checkDrawerIsOpen()53 }54 @Test fun clickOnPlantListDrawerMenuItem_StartsPlantListActivity() {55 clickOnHomeIconToOpenNavigationDrawer()56 // Press on Plant List navigation item57 onView(withId(R.id.navigation_view))58 .perform(navigateTo(R.id.plant_list_fragment))59 // Check that the PlantListFragment is visible60 onView(withId(R.id.plant_list)).check(matches(isDisplayed()))61 }62 @Test fun pressDeviceBack_CloseDrawer_Then_PressBack_Close_App() {63 clickOnHomeIconToOpenNavigationDrawer()64 onView(isRoot()).perform(ViewActions.pressBack())65 checkDrawerIsNotOpen()66 assertEquals(activityTestRule.activity.isFinishing, false)67 assertEquals(activityTestRule.activity.isDestroyed, false)68 }69 private fun clickOnHomeIconToOpenNavigationDrawer() {70 onView(withContentDescription(getToolbarNavigationContentDescription(71 activityTestRule.activity, R.id.toolbar))).perform(click())72 }73 private fun checkDrawerIsOpen() {74 onView(withId(R.id.drawer_layout)).check(matches(isOpen(Gravity.START)))75 }76 private fun checkDrawerIsNotOpen() {77 onView(withId(R.id.drawer_layout)).check(matches(isClosed(Gravity.START)))78 }79}...
ContainerActivityInstrumentedTest.kt
Source:ContainerActivityInstrumentedTest.kt
...28 //TODO how to mock AccountManager in the fragment?29 @Test30 fun userCanOpenAppDrawer() {31 onView(withId(R.id.drawer_layout)).perform(DrawerActions.open())32 onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen()))33 }34 @Test35 fun userCanCloseAppDrawerWithBackButton() {36 onView(withId(R.id.drawer_layout)).perform(DrawerActions.open())37 pressBack()38 onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isClosed()))39 }40 @Test41 fun userCanClickFiveThingsNavButton() {42 onView(withId(R.id.drawer_layout)).perform(DrawerActions.open())43 onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen()))44 onView(withId(R.id.navigation_view)).perform(NavigationViewActions.navigateTo(R.id.five_things_item))45 onView(withId(R.id.five_things_container)).check(matches(isDisplayed()))46 }47 @Test48 fun userCanClickDesignsNavButton() {49 onView(withId(R.id.drawer_layout)).perform(DrawerActions.open())50 onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen()))51 onView(withId(R.id.navigation_view)).perform(NavigationViewActions.navigateTo(R.id.templates_item))52 onView(withId(R.id.designs_container)).check(matches(isDisplayed()))53 }54 @Test55 fun userCanClickSettingsNavButton() {56 onView(withId(R.id.drawer_layout)).perform(DrawerActions.open())57 onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen()))58 onView(withId(R.id.navigation_view)).perform(NavigationViewActions.navigateTo(R.id.settings_item))59 onView(withId(R.id.settings_container)).check(matches(isDisplayed()))60 }61 @Test62 fun userCanClickAnalyticsNavButton() {63 onView(withId(R.id.drawer_layout)).perform(DrawerActions.open())64 onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen()))65 onView(withId(R.id.navigation_view)).perform(NavigationViewActions.navigateTo(R.id.analytics_item))66 onView(withId(R.id.analytics_container)).check(matches(isDisplayed()))67 }68}...
EspressoUtils.kt
Source:EspressoUtils.kt
...40 Espresso.onView(ViewMatchers.withId(R.id.drawerLayout))41 .check(42 ViewAssertions.matches(43 if (isClosed) DrawerMatchers.isClosed(Gravity.START)44 else DrawerMatchers.isOpen(Gravity.START)45 )46 )47}48fun openNavigationDrawer() {49 Espresso.onView(ViewMatchers.withId(R.id.drawerLayout))50 .perform(DrawerActions.open())51}52fun clickResource(resourceId: Int) {53 Espresso.onView(ViewMatchers.withId(resourceId))54 .perform(ViewActions.click())55}56fun isResourceVisible(resourceId: Int) {57 Espresso.onView(ViewMatchers.withId(resourceId))58 .check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))...
AppNavigationTest.kt
Source:AppNavigationTest.kt
...13import android.support.test.espresso.action.ViewActions.click14import android.support.test.espresso.assertion.ViewAssertions.matches15import android.support.test.espresso.contrib.DrawerActions.open16import android.support.test.espresso.contrib.DrawerMatchers.isClosed17import android.support.test.espresso.contrib.DrawerMatchers.isOpen18import android.support.test.espresso.contrib.NavigationViewActions.navigateTo19import android.support.test.espresso.matcher.ViewMatchers.withContentDescription20import android.support.test.espresso.matcher.ViewMatchers.withId21import android.support.test.espresso.matcher.ViewMatchers.withText22/**23 * Tests for the [DrawerLayout] layout component in [RoomsActivity] which manages24 * navigation within the app.25 */26@RunWith(AndroidJUnit4::class)27@LargeTest28class AppNavigationTest {29 /**30 * [ActivityTestRule] is a JUnit [@Rule][Rule] to launch your activity under test.31 *32 *33 *34 * Rules are interceptors which are executed for each test method and are important building35 * blocks of Junit tests.36 */37 @Rule38 var mActivityTestRule = ActivityTestRule(RoomsActivity::class.java)39 @Test40 fun clickOnStatisticsNavigationItem_ShowsStatisticsScreen() {41 // Open Drawer to click on navigation.42 onView(withId(R.id.drawer_layout))43 .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.44 .perform(open()) // Open Drawer45 // Start statistics screen.46 onView(withId(R.id.nav_view))47 .perform(navigateTo(R.id.nav_view))48 // Check that statistics Activity was opened.49 val expectedNoStatisticsText = InstrumentationRegistry.getTargetContext()50 .getString(R.string.no_statistics_available)51 // onView(withId(R.id.no_statistics)).check(matches(withText(expectedNoStatisticsText)));52 }53 @Test54 fun clickOnAndroidHomeIcon_OpensNavigation() {55 // Check that left drawer is closed at startup56 onView(withId(R.id.drawer_layout))57 .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.58 // Open Drawer59 val navigateUpDesc = mActivityTestRule.activity60 .getString(android.support.v7.appcompat.R.string.abc_action_bar_up_description)61 onView(withContentDescription(navigateUpDesc)).perform(click())62 // Check if drawer is open63 onView(withId(R.id.drawer_layout))64 .check(matches(isOpen(Gravity.LEFT))) // Left drawer is open open.65 }66}
MainActivityTest.kt
Source:MainActivityTest.kt
...29 Espresso.onView(instanceOf(DrawerLayout::class.java))30 .check(matches(isClosed()))31 .perform(DrawerActions.open())32 Thread.sleep(1000)33 matches(DrawerMatchers.isOpen())34 }35/*36 @Test37 fun scheduleOpenActivityTest() {38 onView(withId(R.id.material_drawer_slider_layout))39 //.check(ViewAssertions.matches(DrawerMatchers.isOpen(Gravity.START)))40 .perform(DrawerActions.open())41 .perform(NavigationViewActions.navigateTo(R.id.schedule))42 Thread.sleep(1000)43 onView(Matchers.instanceOf(TimetableLayout::class.java))44 .perform(clickXY(20, 20))45 .perform(clickXY(20, 20))46 }*/47 private fun clickXY(x: Int, y: Int): ViewAction {48 return GeneralClickAction(49 Tap.SINGLE,50 CoordinatesProvider { view ->51 val screenPos = IntArray(2)52 view.getLocationOnScreen(screenPos)53 val screenX = (screenPos[0] + x).toFloat()...
UIBillingActivityTest.kt
Source:UIBillingActivityTest.kt
2import android.content.Intent3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.assertion.ViewAssertions.matches5import android.support.test.espresso.contrib.DrawerActions6import android.support.test.espresso.contrib.DrawerMatchers.isOpen7import android.support.test.espresso.contrib.NavigationViewActions8import android.support.test.espresso.matcher.ViewMatchers.*9import android.support.test.rule.ActivityTestRule10import android.widget.TextView11import me.androidbox.enershared.R12import me.androidbox.enershared.home.HomeActivity13import org.hamcrest.Matchers.allOf14import org.hamcrest.Matchers.instanceOf15import org.junit.Before16import org.junit.Rule17import org.junit.Test18class UIBillingActivityTest {19 @Rule20 @JvmField21 val billingActivity = ActivityTestRule(HomeActivity::class.java, false, false)22 @Before23 fun setup() {24 /* no-op */25 }26 @Test27 fun testBillActivityIsDisplayed() {28 billingActivity.launchActivity(Intent())29 checkTheHomeScreenIsDisplayed()30 openNavigationDrawer()31 checkNavigationDrawerIsOpened()32 tapOnBillingItem()33 checkTheBillingScreenIsDisplayed()34 }35 private fun checkTheHomeScreenIsDisplayed() {36 onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.tbHome))))37 .check(matches(withText(R.string.home)))38 }39 private fun openNavigationDrawer() {40 onView(withId(R.id.homeDrawerLayout)).perform(DrawerActions.open())41 }42 private fun checkNavigationDrawerIsOpened() {43 onView(withId(R.id.homeDrawerLayout)).check(matches(isOpen()))44 }45 private fun tapOnBillingItem() {46 onView(withId(R.id.nvHome)).perform(NavigationViewActions.navigateTo(R.id.menuBilling))47 }48 private fun checkTheBillingScreenIsDisplayed() {49 onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.tbHome))))50 .check(matches(withText(R.string.billing)))51 }52}...
NavigatorTest.kt
Source:NavigatorTest.kt
...3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions.click5import android.support.test.espresso.assertion.ViewAssertions.matches6import android.support.test.espresso.contrib.DrawerMatchers.isClosed7import android.support.test.espresso.contrib.DrawerMatchers.isOpen8import android.support.test.espresso.matcher.ViewMatchers.*9import android.support.test.filters.LargeTest10import android.support.test.rule.ActivityTestRule11import android.support.test.runner.AndroidJUnit412import android.view.Gravity13import org.junit.Test14import org.junit.runner.RunWith15import org.junit.Assert.*16import org.junit.Before17import org.junit.ClassRule18import org.junit.Rule19/**20 * Instrumented test, which will execute on an Android device.21 *22 * See [testing documentation](http://d.android.com/tools/testing).23 */24@RunWith(AndroidJUnit4::class)25@LargeTest26class NavigatorTest27{28 @Rule @JvmField29 var activityTestRule = ActivityTestRule(MainActivity::class.java)30 31 private lateinit var activity: MainActivity32 33 @Before34 fun setUpActivity()35 {36 this.activity = activityTestRule.activity37 }38 39 @Test40 fun use_app_context()41 {42 val appContext = InstrumentationRegistry.getTargetContext()43 assertEquals("me.liuqingwen.android.projectandroidtest", appContext.packageName)44 }45 46 @Test47 fun click_on_android_toolbar_home_button()48 {49 onView(withId(MainUI.ID_LAYOUT_DRAWER)).check(matches(isClosed(Gravity.LEFT)))50 onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click())51 onView(withId(MainUI.ID_LAYOUT_DRAWER)).check(matches(isOpen(Gravity.LEFT)))52 }53}...
isOpen
Using AI Code Generation
1ViewInteraction drawerLayout = onView(allOf(withId(R.id.drawer_layout),childAtPosition(allOf(withId(R.id.drawer_layout),childAtPosition(withId(android.R.id.content),0)),0),isDisplayed()));2ViewInteraction appCompatImageButton = onView(allOf(withContentDescription("Open navigation drawer"),childAtPosition(allOf(withId(R.id.toolbar),childAtPosition(withId(R.id.app_bar),0)),1),isDisplayed()));3appCompatImageButton.perform(click());4ViewInteraction linearLayout = onView(allOf(childAtPosition(allOf(withId(R.id.design_navigation_view),childAtPosition(withId(R.id.nav_view),0)),0),isDisplayed()));5linearLayout.perform(click());6ViewInteraction drawerLayout2 = onView(allOf(withId(R.id.drawer_layout),childAtPosition(allOf(withId(R.id.drawer_layout),childAtPosition(withId(android.R.id.content),0)),0),isDisplayed()));7ViewInteraction appCompatImageButton2 = onView(allOf(withContentDescription("Open navigation drawer"),childAtPosition(allOf(withId(R.id.toolbar),childAtPosition(withId(R.id.app_bar),0)),1),isDisplayed()));8appCompatImageButton2.perform(click());9ViewInteraction linearLayout2 = onView(allOf(childAtPosition(allOf(withId(R.id.design_navigation_view),childAtPosition(withId(R.id.nav_view),0)),0),isDisplayed()));10linearLayout2.perform(click());11ViewInteraction drawerLayout3 = onView(allOf(withId(R.id.drawer_layout),childAtPosition(allOf(withId(R.id.drawer_layout),childAtPosition(withId(android.R.id.content),0)),0),isDisplayed()));12ViewInteraction appCompatImageButton3 = onView(allOf(withContentDescription("Open navigation drawer"),childAtPosition(allOf(withId(R.id.toolbar),childAtPosition(withId(R.id.app_bar),0)),1),isDisplayed()));13appCompatImageButton3.perform(click());14ViewInteraction linearLayout3 = onView(allOf(childAtPosition(allOf(withId(R.id.design_navigation_view),childAtPosition(withId(R.id.nav_view),0)),0),isDisplayed()));15linearLayout3.perform(click());16ViewInteraction drawerLayout4 = onView(allOf(withId(R.id.drawer_layout),childAtPosition(allOf(withId(R.id.drawer_layout),childAtPosition(withId(android.R.id.content),0)),0),isDisplayed
isOpen
Using AI Code Generation
1onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());2onView(withId(R.id.drawer_layout)).perform(DrawerActions.close());3onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isClosed(Gravity.LEFT)));4onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen(Gravity.LEFT)));5onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen(Gravity.RIGHT)));6onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isClosed(Gravity.RIGHT)));7onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isClosed(Gravity.START)));8onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen(Gravity.START)));9onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isOpen(Gravity.END)));10onView(withId(R.id.drawer_layout)).check(matches(DrawerMatchers.isClosed(Gravity.END)));
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!!