Best Appium-espresso-driver code snippet using android.support.test.espresso.contrib.PickerActions
EditPresentationActivityTest.kt
Source:EditPresentationActivityTest.kt
2import android.support.test.InstrumentationRegistry3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions.*5import android.support.test.espresso.assertion.ViewAssertions.matches6import android.support.test.espresso.contrib.PickerActions7import android.support.test.espresso.matcher.ViewMatchers.*8import android.support.test.rule.ActivityTestRule9import android.support.test.runner.AndroidJUnit410import android.support.test.uiautomator.UiDevice11import org.junit.*12import org.junit.runner.RunWith13import java.lang.Thread.sleep14import android.widget.NumberPicker15import android.support.test.espresso.matcher.ViewMatchers16import android.support.test.espresso.UiController17import android.support.test.espresso.ViewAction18import android.view.View19import org.hamcrest.Matcher20import android.support.test.espresso.matcher.ViewMatchers.withClassName21import android.support.test.espresso.Espresso.onView22import org.hamcrest.Matchers23@RunWith(AndroidJUnit4::class)24class EditPresentationActivityTest : BaseInstrumentedTest() {25 @Rule26 @JvmField27 var activityTestRule = ActivityTestRule<StartPageActivity>(StartPageActivity::class.java)28 lateinit var helper: TestHelper29 @Before30 fun enableDebugMode() {31 helper = TestHelper(activityTestRule.activity)32 helper.setTrainingPresentationMod(true) // вклÑÑение ÑеÑÑовой пÑезенÑаÑии33 }34 @After35 fun disableDebugMode() {36 helper.setTrainingPresentationMod(false) // вÑклÑÑение ÑеÑÑовой пÑезенÑаÑии37 helper.removeDebugSlides()38 }39 @Test40 fun datePickerExist() {41 InstrumentationRegistry.getInstrumentation().waitForIdleSync()42 onView(withId(R.id.addBtn)).perform(click())43 onView(withId(R.id.datePicker)).check(matches(isDisplayed()))44 onView(withId(R.id.addPresentation)).perform(click())45 sleep(2000)46 }47 @Test48 fun setDateForPresentation() {49 // Ðзменение даÑÑ Ð¿Ñи добавлении50 onView(withId(R.id.addBtn)).perform(click())51 onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2035, 5, 12))52 onView(withId(R.id.addPresentation)).perform(click())53 sleep(2000)54 InstrumentationRegistry.getInstrumentation().waitForIdleSync()55 onView(withText("2035-5-12")).check(matches(isDisplayed()))56 // Ðзменение даÑÑ Ð¿Ñи ÑедакÑиÑовании57 onView(withText("2035-5-12")).perform(longClick())58 sleep(2000)59 onView(withText(R.string.edit)).perform(click())60 InstrumentationRegistry.getInstrumentation().waitForIdleSync()61 sleep(2000)62 onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2036, 5, 12))63 onView(withId(R.id.addPresentation)).perform(click())64 InstrumentationRegistry.getInstrumentation().waitForIdleSync()65 sleep(2000)66 onView(withText("2036-5-12")).check(matches(isDisplayed()))67 }68 @Test69 fun setNameOfPresentation() {70 // Ðзменение Ð½Ð°Ð·Ð²Ð°Ð½Ð¸Ñ Ð¿ÑезенÑаÑии пÑи добавлении71 onView(withId(R.id.addBtn)).perform(click())72 onView(withId(R.id.presentationName)).perform(replaceText(activityTestRule.activity.getString(R.string.first_debug_presentation_name)))73 onView(withId(R.id.addPresentation)).perform(click())74 sleep(2000)75 onView(withText(R.string.first_debug_presentation_name)).check(matches(isDisplayed()))76 // Ðзменение Ð½Ð°Ð·Ð²Ð°Ð½Ð¸Ñ Ð¿ÑезенÑаÑии пÑи ÑедакÑиÑовании...
AddEditScreenTest.kt
Source:AddEditScreenTest.kt
...3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions.*5import android.support.test.espresso.assertion.ViewAssertions.doesNotExist6import android.support.test.espresso.assertion.ViewAssertions.matches7import android.support.test.espresso.contrib.PickerActions8import android.support.test.espresso.matcher.ViewMatchers.*9import android.support.test.rule.ActivityTestRule10import android.support.test.runner.AndroidJUnit411import android.widget.DatePicker12import com.robyn.dayplus2.R13import com.robyn.dayplus2.click14import com.robyn.dayplus2.pickDateOnDatePicker15import org.hamcrest.Matchers16import org.hamcrest.Matchers.*17import org.joda.time.DateTime18import org.joda.time.format.DateTimeFormat19import org.junit.Rule20import org.junit.Test21import org.junit.runner.RunWith22import android.support.test.InstrumentationRegistry23import com.robyn.dayplus2.data.MyEvent24import org.junit.Before25/**26 * 7 edit sections in [AddEditFragment]. Those are:27 * 1, background pic28 * 2, date29 * 3, repeat mode30 * 4, title31 * 5, description32 * 6, star33 * 7, category34 */35@RunWith(AndroidJUnit4::class)36class AddEditScreenTest {37 lateinit var mDb: com.robyn.dayplus2.data.source.local.EventDatabase38 @Rule39 @JvmField40 var mRule = ActivityTestRule<AddEditActivity>(AddEditActivity::class.java)41 @Test42 fun titleLayoutIsDisplayed_test() {43 onView(withId(R.id.title_layout)).check(matches(isDisplayed()))44 }45 // click on camera fab, show options in btm sheet46 @Test47 fun clickFab_showBtmSheet() {48 // click on camera fab49 onView(withId(R.id.camera_fab)).perform(click())50 // check btm sheet display, by check one view it contains51 onView(withId(R.id.camera_btn)).check(matches(isDisplayed()))52 }53 @Test54 fun clickCancel_dismissBtmSheet_pickPic() {55 // click on camera fab.56 onView(withId(R.id.camera_fab)).perform(click())57 // if btm sheet displays, click on its cancel btn.58 onView(withId(R.id.cancel_btn_pic)).perform(click())59 // check if the camera btn not exist, which means dial dismissed.60 onView(withId(R.id.camera_btn)).check(doesNotExist())61 }62 @Test63 fun clickRepeatModeOptions_viewUpdates() {64 onView(withId(R.id.repeat_layout_edit)).perform(click())65 onView(withId(R.id.weekly)).perform(click())66 onView(withText(containsString("Week"))).check(matches(isDisplayed()))67 onView(withId(R.id.repeat_layout_edit)).perform(click())68 onView(withId(R.id.monthly)).perform(click())69 onView(withText(containsString("Month"))).check(matches(isDisplayed()))70 onView(withId(R.id.repeat_layout_edit)).perform(click())71 onView(withId(R.id.annually)).perform(click())72 onView(withText(containsString("Year"))).check(matches(isDisplayed()))73 onView(withId(R.id.repeat_layout_edit)).perform(click())74 onView(withId(R.id.never)).perform(click())75 onView(withText(containsString("Never"))).check(matches(isDisplayed()))76 }77 @Test78 fun datePickerPicksDate_showDate() {79 // prepare the date string to check if shows on date picker80 val year = 201881 val monthOfYear = 382 val dayOfMonth = 2183 val formatDateStr = formatDateStr(year, monthOfYear, dayOfMonth)84 // perform setting this date on date picker85 pickDateOnDatePicker(R.id.date_layout_edit, year, monthOfYear, dayOfMonth)86 // check if formatted date string is showing on date text view.87 onView(withId(R.id.date_text_view_edit)).check(matches(withText(formatDateStr)))88 }89 /**90 * returns the format date string the date text view supposed to show.91 */92 private fun formatDateStr(year: Int, monthOfYear: Int, dayOfMonth: Int): String {93 val fmt = DateTimeFormat.forPattern("dd MMM, yyyy")94 return DateTime(year, monthOfYear, dayOfMonth, 0, 0).toString(fmt)95 }96 /**97 * perform picking a date on date picker98 */99 private fun pickDateOnDatePicker(100 datePickerTriggerViewId: Int,101 year: Int,102 monthOfYear: Int,103 dayOfMonth: Int104 ) {105 // open the date picker dial106 onView(withId(datePickerTriggerViewId)).perform(click())107 // find date picker by class name, perform picker action from espresso-contrib108 onView(withClassName(Matchers.equalTo(DatePicker::class.java.name)))109 .perform(PickerActions.setDate(year, monthOfYear, dayOfMonth))110 // dials' ok button id = button1111 onView(withId(android.R.id.button1)).perform(click())112 }113 // Click on category textview, show dial, pick image, dial dismisses, textview shows the picked image114 @Test115 fun pickCategory_showPickedImage() {116 checkCategoryByTag(R.id.cake, R.drawable.ic_action_cake)117 checkCategoryByTag(R.id.loved, R.drawable.ic_action_loved)118 checkCategoryByTag(R.id.face, R.drawable.ic_action_face)119 checkCategoryByTag(R.id.social, R.drawable.ic_action_social)120 checkCategoryByTag(R.id.work, R.drawable.ic_action_work)121 }122 private fun checkCategoryByTag(imageViewId:Int, drawableResInt:Int) {123 click(R.id.category_textview_addedit)...
EditQuizDetailsPage.kt
Source:EditQuizDetailsPage.kt
...16 */17package com.instructure.teacher.ui.pages18import android.support.test.espresso.Espresso19import android.support.test.espresso.assertion.ViewAssertions20import android.support.test.espresso.contrib.PickerActions21import android.support.test.espresso.matcher.ViewMatchers.*22import android.widget.DatePicker23import android.widget.TimePicker24import com.instructure.canvasapi2.utils.DateHelper25import com.instructure.espresso.*26import com.instructure.espresso.matchers.has27import com.instructure.espresso.matchers.hasTextInputLayoutErrorText28import com.instructure.espresso.matchers.withIndex29import com.instructure.espresso.page.*30import com.instructure.teacher.R31import com.instructure.teacher.view.AssignmentOverrideView32import org.hamcrest.CoreMatchers.allOf33import org.hamcrest.Matchers34import java.text.SimpleDateFormat35import java.util.*36class EditQuizDetailsPage : BasePage() {37 private val quizTitleEditText by OnViewWithId(R.id.editQuizTitle)38 private val publishSwitch by WaitForViewWithId(R.id.publishSwitch)39 private val accessCodeSwitch by WaitForViewWithId(R.id.accessCodeSwitch)40 private val accessCodeEditText by WaitForViewWithId(R.id.editAccessCode)41 private val saveButton by OnViewWithId(R.id.menuSave)42 private val descriptionWebView by OnViewWithId(R.id.descriptionWebView, autoAssert = false)43 private val noDescriptionTextView by OnViewWithId(R.id.noDescriptionTextView, autoAssert = false)44 fun saveQuiz() {45 callOnClick(withId(R.id.menuSave))46 }47 fun editQuizTitle(newName: String) {48 // Combination of scroll and click randomly selects the text sometimes.49 // This opens a contextual menu and blocks the background view to receive focus and fails the test.50 // That is why, `quizTitleEditText.scrollTo()` is not needed. 51 quizTitleEditText.replaceText(newName)52 saveQuiz()53 }54 fun clickAccessCode() {55 accessCodeSwitch.scrollTo()56 accessCodeSwitch.click()57 }58 fun clickAccessCodeEditText() {59 accessCodeEditText.scrollTo()60 accessCodeEditText.click()61 }62 fun editAccessCode(): String {63 val code = randomString()64 accessCodeEditText.scrollTo()65 accessCodeEditText.replaceText(code)66 saveQuiz()67 return code68 }69 fun editDate(year: Int, month: Int, dayOfMonth: Int) {70 waitForViewWithClassName(Matchers.equalTo<String>(DatePicker::class.java.name))71 .perform(PickerActions.setDate(year, month, dayOfMonth))72 onViewWithId(android.R.id.button1).click()73 }74 fun editTime(hour: Int, min: Int) {75 waitForViewWithClassName(Matchers.equalTo<String>(TimePicker::class.java.name))76 .perform(PickerActions.setTime(hour, min))77 onViewWithId(android.R.id.button1).click()78 }79 fun removeSecondOverride() {80 // scroll to bottom to make the 2nd override button visible81 addOverrideButton().scrollTo()82 ClickUntilMethod.run(83 onView(withContentDescription("remove_override_button_1")),84 onView(withText("Remove Due Date"))85 )86 // Wait for alert dialog to display before clicking "Remove"87 waitForViewWithText(R.string.removeDueDate).assertVisible()88 waitForViewWithText(R.string.remove).click()89 }90 fun assertDateChanged(year: Int, month: Int, dayOfMonth: Int, id: Int) {...
EditAssignmentDetailsPage.kt
Source:EditAssignmentDetailsPage.kt
...14 * limitations under the License.15 */16package com.instructure.teacher.ui.pages17import android.support.test.espresso.assertion.ViewAssertions.matches18import android.support.test.espresso.contrib.PickerActions19import android.support.test.espresso.matcher.ViewMatchers20import android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility21import android.widget.DatePicker22import android.widget.TimePicker23import com.instructure.canvasapi2.utils.DateHelper24import com.instructure.espresso.*25import com.instructure.espresso.matchers.has26import com.instructure.espresso.matchers.hasTextInputLayoutErrorText27import com.instructure.espresso.matchers.withIndex28import com.instructure.espresso.page.*29import com.instructure.teacher.R30import com.instructure.teacher.view.AssignmentOverrideView31import org.hamcrest.CoreMatchers.allOf32import org.hamcrest.Matchers33import java.text.DecimalFormat34import java.text.SimpleDateFormat35import java.util.*36class EditAssignmentDetailsPage : BasePage() {37 private val assignmentNameEditText by OnViewWithId(R.id.editAssignmentName)38 private val pointsPossibleEditText by WaitForViewWithId(R.id.editGradePoints)39 private val publishSwitch by WaitForViewWithId(R.id.publishSwitch)40 private val saveButton by OnViewWithId(R.id.menuSave)41 private val descriptionWebView by OnViewWithId(R.id.descriptionWebView, autoAssert = false)42 private val noDescriptionTextView by OnViewWithId(R.id.noDescriptionTextView, autoAssert = false)43 private val overlayContainer by OnViewWithId(R.id.overrideContainer, autoAssert = false)44 fun saveAssignment() {45 saveButton.click()46 }47 fun clickAssignmentNameEditText() {48 assignmentNameEditText.click()49 }50 fun clickPointsPossibleEditText() {51 scrollTo(R.id.editGradePoints)52 pointsPossibleEditText.click()53 }54 fun editAssignmentName(newName: String) {55 assignmentNameEditText.replaceText(newName)56 saveAssignment()57 }58 fun editAssignmentPoints(newPoints: Double) {59 val df = DecimalFormat("#")60 pointsPossibleEditText.replaceText(df.format(newPoints))61 saveAssignment()62 }63 fun editAssignees() = waitScrollClick(R.id.assignTo)64 fun clickEditDueDate() = waitScrollClick(R.id.dueDate)65 fun clickEditDueTime() = waitScrollClick(R.id.dueTime)66 fun clickEditUnlockDate() = waitScrollClick(R.id.fromDate)67 fun clickEditUnlockTime() = waitScrollClick(R.id.fromTime)68 fun clickEditLockDate() = waitScrollClick(R.id.toDate)69 fun clickEditLockTime() = waitScrollClick(R.id.toTime)70 fun clickAddOverride() = onView(allOf(withId(R.id.addOverride), withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))).scrollTo().click()71 fun removeFirstOverride() {72 waitForViewWithContentDescription("remove_override_button_0").scrollTo().click()73 waitForViewWithText(R.string.remove).click()74 }75 fun editDate(year: Int, month: Int, dayOfMonth: Int) {76 waitForViewWithClassName(Matchers.equalTo<String>(DatePicker::class.java.name))77 .perform(PickerActions.setDate(year, month, dayOfMonth))78 onViewWithId(android.R.id.button1).click()79 }80 fun editTime(hour: Int, min: Int) {81 waitForViewWithClassName(Matchers.equalTo<String>(TimePicker::class.java.name))82 .perform(PickerActions.setTime(hour, min))83 onViewWithId(android.R.id.button1).click()84 }85 fun assertDateChanged(year: Int, month: Int, dayOfMonth: Int, id: Int) {86 val cal = Calendar.getInstance().apply {set(year, month, dayOfMonth)}87 waitForViewWithId(id).assertHasText(DateHelper.getFullMonthNoLeadingZeroDateFormat().format(cal.time))88 }89 fun assertTimeChanged(hour: Int, min: Int, id: Int) {90 val cal = Calendar.getInstance().apply {set(0, 0, 0, hour, min)}91 val sdh = SimpleDateFormat("H:mm a", Locale.US)92 waitForViewWithId(id).assertHasText(sdh.format(cal.time))93 }94 fun assertNewOverrideCreated() {95 waitForViewWithId(R.id.overrideContainer).check(has(2, Matchers.instanceOf(AssignmentOverrideView::class.java)))96 }...
BasicTest.kt
Source:BasicTest.kt
...4import android.support.test.espresso.Espresso.onView5import android.support.test.espresso.UiController6import android.support.test.espresso.ViewAction7import android.support.test.espresso.action.ViewActions.click8import android.support.test.espresso.contrib.PickerActions9import android.support.test.espresso.matcher.ViewMatchers10import android.support.test.espresso.matcher.ViewMatchers.withId11import android.support.test.rule.ActivityTestRule12import android.util.Log13import android.view.View14import android.widget.NumberPicker15import com.kishan.employeetracker.ui.LaunchActivity16import org.hamcrest.Description17import org.hamcrest.Matcher18import org.hamcrest.TypeSafeMatcher19import org.joda.time.LocalDate20import org.junit.After21import org.junit.Before22import org.junit.Test23/**24 * Created by Kishan P Rao on 28/04/18.25 */26class BasicTest : ActivityTestRule<LaunchActivity>(LaunchActivity::class.java) {27 companion object {28 private val TAG = BasicTest::class.java.simpleName29 }30 31 private fun withIndex(matcher: Matcher<View>, index: Int): Matcher<View> {32 return object : TypeSafeMatcher<View>() {33 internal var currentIndex: Int = 034 internal var viewObjHash: Int = 035 36 @SuppressLint("DefaultLocale")37 override fun describeTo(description: Description) {38 description.appendText(String.format("with index: %d ", index))39 matcher.describeTo(description)40 }41 42 override fun matchesSafely(view: View): Boolean {43 if (matcher.matches(view) && currentIndex++ == index) {44 viewObjHash = view.hashCode()45 }46 return view.hashCode() === viewObjHash47 }48 }49 }50 51 52 @Before53 internal fun setUp() {54 Log.d(TAG, "setUp, ")55 launchActivity(null)56 }57 58 private fun setNumber(num: Int): ViewAction {59 return object : ViewAction {60 override fun perform(uiController: UiController, view: View) {61 val np = view as NumberPicker62 np.value = num63 64 }65 66 override fun getDescription(): String {67 return "Set the passed number into the NumberPicker"68 }69 70 override fun getConstraints(): Matcher<View> {71 return ViewMatchers.isAssignableFrom(NumberPicker::class.java)72 }73 }74 }75 76// private fun launchActivity() {77// }78 79 private val index = 080 81 private val samplesStart = arrayOf(82 LocalDate(2016, 5, 16),83 LocalDate(2018, 4, 24)84 )85 private val samplesResign = arrayOf(86 LocalDate(2018, 2, 20),87 LocalDate(2018, 4, 26)88 )89 90 private val samplesNotice = arrayOf(91 90,92 293 )94 95 private fun testWait() {96 Thread.sleep(2 * 1000)97 }98 99 private fun completeWithoutNext() {100 var year = samplesStart[index].year101 var month = samplesStart[index].monthOfYear102 var day = samplesStart[index].dayOfMonth103 104 var datePicker = withIndex(withId(R.id.datePicker), 0)105 onView(datePicker).perform(PickerActions.setDate(year, month, day))106 onView(withIndex(withId(R.id.launch_next), 0)).perform(click())107// testWait()108 109 year = samplesResign[index].year110 month = samplesResign[index].monthOfYear111 day = samplesResign[index].dayOfMonth112 datePicker = withIndex(withId(R.id.datePicker), 1)113 onView(datePicker).perform(PickerActions.setDate(year, month, day))114 onView(withIndex(withId(R.id.launch_next), 1)).perform(click())115// testWait()116 117 val notice = samplesNotice[index]118 onView(withId(R.id.numberPicker)).perform(setNumber(notice))119 testWait()120 }121 122// @Test123 internal fun testWithoutResign() {124 var year = samplesStart[index].year125 var month = samplesStart[index].monthOfYear126 var day = samplesStart[index].dayOfMonth127 128 var datePicker = withIndex(withId(R.id.datePicker), 0)129 onView(datePicker).perform(PickerActions.setDate(year, month, day))130 onView(withIndex(withId(R.id.launch_next), 0)).perform(click())131 testWait()132 133 onView(withIndex(withId(R.id.launch_skip), 1)).perform(click())134 }135 136 @Test137 internal fun testComplete() {138 Log.d(TAG, "complete, ")139 completeWithoutNext()140 onView(withId(R.id.launch_notice_next)).perform(click())141 }142 143 @After...
NotificationsTest.kt
Source:NotificationsTest.kt
2import android.support.test.InstrumentationRegistry3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions.*5import android.support.test.espresso.assertion.ViewAssertions.matches6import android.support.test.espresso.contrib.PickerActions7import android.support.test.espresso.matcher.ViewMatchers.*8import android.support.test.rule.ActivityTestRule9import android.support.test.runner.AndroidJUnit410import android.support.test.uiautomator.By11import android.support.test.uiautomator.UiDevice12import android.support.test.uiautomator.UiSelector13import android.support.test.uiautomator.Until14import junit.framework.Assert.*15import org.junit.After16import org.junit.Before17import org.junit.Rule18import org.junit.Test19import org.junit.runner.RunWith20import ru.spb.speech.notifications.NotificationsHelper21import java.util.*22@RunWith(AndroidJUnit4::class)23class NotificationsTest: BaseInstrumentedTest() {24 val TIMEOUT = 2500L25 @Rule26 @JvmField27 var activityTestRule = ActivityTestRule<StartPageActivity>(StartPageActivity::class.java)28 lateinit var notificationsHelper: NotificationsHelper29 lateinit var testHelper: TestHelper30 lateinit var device: UiDevice31 @Before32 fun before() {33 device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())34 testHelper = TestHelper(activityTestRule.activity)35 notificationsHelper = NotificationsHelper(activityTestRule.activity)36 testHelper.setTrainingPresentationMod(true)37 }38 @After39 fun after() {40 testHelper.setTrainingPresentationMod(false)41 }42 @Test43 fun defaultNotificationsCall() {44 val presentationName = testHelper.addDebugPresentation(notifications = true)45 Thread.sleep(TIMEOUT)46 assertTrue(notificationsHelper.validateNotification())47 val calendar = Calendar.getInstance()48 onView(withText(presentationName)).perform(longClick())49 onView(withText(activityTestRule.activity.getString(R.string.change))).perform(click())50 onView(withId(R.id.datePicker)).perform(PickerActions.setDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) - 1))51 onView(withId(R.id.addPresentation)).perform(click())52 Thread.sleep(TIMEOUT)53 assertFalse(notificationsHelper.validateNotification())54 testHelper.removeDebugSlides()55 }56 @Test57 fun notificationExists() {58 val expectedApplicationName = activityTestRule.activity.getString(R.string.app_name)59 val expectedTitle = activityTestRule.activity.getString(R.string.notifications_title)60 val expectedText = activityTestRule.activity.getString(R.string.notifications_text)61 notificationsHelper.sendNotification()62 device.openNotification()63 device.wait(Until.findObject(By.textStartsWith(expectedApplicationName)), TIMEOUT)64 val title = device.findObject(By.text(expectedTitle))...
MainActivityTest.kt
Source:MainActivityTest.kt
...3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions.click5import android.support.test.espresso.assertion.ViewAssertions.doesNotExist6import android.support.test.espresso.assertion.ViewAssertions.matches7import android.support.test.espresso.contrib.PickerActions8import android.support.test.espresso.contrib.RecyclerViewActions9import android.support.test.espresso.matcher.ViewMatchers.*10import android.support.test.rule.ActivityTestRule11import android.support.test.runner.AndroidJUnit412import android.support.v7.widget.RecyclerView13import android.widget.DatePicker14import com.myd.movies.R15import com.myd.movies.mvp.model.remote.FakeDetailsDataSource16import com.myd.movies.mvp.model.remote.FakeMovieDataSource17import com.myd.movies.util.RecyclerViewMatcher18import com.myd.movies.util.TestUtil19import org.hamcrest.Matchers20import org.junit.Before21import org.junit.Rule22import org.junit.Test23import org.junit.runner.RunWith24/**25 * Created by MYD on 6/1/18.26 */27@RunWith(AndroidJUnit4::class)28class MainActivityTest {29 @Rule @JvmField30 var activityTestRule = ActivityTestRule(MainActivity::class.java)31 @Before32 @Throws(Exception::class)33 fun setUp() {34 val source = FakeMovieDataSource()35 val movies = TestUtil.createMovies()36 val moviesRemoteResponse = TestUtil.createMoviesRemoteResponse(1, movies.size, 1, movies)37 source.setServiceData(moviesRemoteResponse)38 activityTestRule.launchActivity(Intent())39 }40 @Test41 @Throws(Exception::class)42 fun testListFragment() {43 onView(withId(R.id.fragment)).check(matches(isDisplayed()))44 onView(withId(R.id.fragment_movie_list_rcv)).check(matches(isDisplayed()))45 onView(withId(R.id.fragment_movie_list_rcv)).perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(3))46 val movie3 = FakeMovieDataSource.SERVICE_DATA.results?.get(3)47 onView(RecyclerViewMatcher.withRecyclerView(R.id.fragment_movie_list_rcv).atPosition(3)).check(matches(hasDescendant(withText(movie3?.releaseDate!!))))48 }49 @Test50 @Throws(Exception::class)51 fun testDetailFragment() {52 val detailsDataSource = FakeDetailsDataSource()53 val movie3 = FakeMovieDataSource.SERVICE_DATA.results?.get(3)54 detailsDataSource.setServiceData(TestUtil.createMovieDetails(movie3))55 onView(withId(R.id.fragment)).check(matches(isDisplayed()))56 onView(withId(R.id.fragment_movie_list_rcv)).check(matches(isDisplayed()))57 onView(withId(R.id.fragment_movie_list_rcv)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(3, click()))58 onView(withId(R.id.action_filter)).check(doesNotExist())59 onView(withId(R.id.fragment_movie_details_date_txv)).check(matches(isDisplayed())).check(matches(withText(movie3?.releaseDate)))60 onView(withContentDescription(R.string.abc_action_bar_up_description)).check(matches(isDisplayed())).perform(click())61 onView(withId(R.id.fragment_movie_list_rcv)).check(matches(isDisplayed()))62 }63 @Test64 @Throws(Exception::class)65 fun testDatePicker() {66 onView(withId(R.id.action_filter)).check(matches(isDisplayed()))67 onView(withId(R.id.action_filter)).perform(click())68 onView(withClassName(Matchers.equalTo(DatePicker::class.java.name))).perform(PickerActions.setDate(2018, 2, 27))69 onView(withId(android.R.id.button1)).perform(click())70 onView(withId(R.id.fragment_movie_list_rcv)).check(matches(isDisplayed()))71 onView(RecyclerViewMatcher.withRecyclerView(R.id.fragment_movie_list_rcv).atPosition(0)).check(matches(hasDescendant(withText("2018-2-27"))))72 }73}...
EspressoUtils.kt
Source:EspressoUtils.kt
2import android.support.test.espresso.Espresso3import android.support.test.espresso.Espresso.onView4import android.support.test.espresso.action.ViewActions5import android.support.test.espresso.assertion.ViewAssertions.matches6import android.support.test.espresso.contrib.PickerActions7import android.support.test.espresso.matcher.ViewMatchers8import android.support.test.espresso.matcher.ViewMatchers.*9import android.widget.DatePicker10import org.hamcrest.Matchers11fun click(viewResId: Int) {12 Espresso.onView(ViewMatchers.withId(viewResId)).perform(ViewActions.click())13}14fun click(text: String) {15 Espresso.onView(ViewMatchers.withText(text)).perform(ViewActions.click())16}17fun checkDisplayed(idInt:Int) {18 onView(withId(idInt)).check(matches(isDisplayed()))19}20fun checkDisplayed(text: String) {21 onView(withText(text)).check(matches(isDisplayed()))22}23// NOT equivalent to Espresso.pressBack24fun pressUp() {25 Espresso.onView(ViewMatchers.withContentDescription(R.string.abc_action_bar_up_description))26 .perform(ViewActions.click())27}28// Pick a date on DatePicker, using espresso's PickerActions29fun pickDateOnDatePicker(30 year: Int,31 monthOfYear: Int,32 dayOfMonth: Int33) {34 Espresso.onView(ViewMatchers.withClassName(Matchers.equalTo(DatePicker::class.java.name)))35 .perform(36 PickerActions.setDate(37 year,38 monthOfYear,39 dayOfMonth40 )41 )42 click(android.R.id.button1)43}...
PickerActions
Using AI Code Generation
1import android.support.test.espresso.contrib.PickerActions;2onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));3onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));4onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));5onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));6onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));7onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));8onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));9onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));10onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));11onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));12onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));13onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));14onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));15onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));16onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));17onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));18onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));19onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));20onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));21onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));22onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));23onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));24onView(withId(R.id.datePicker)).perform(PickerActions.setDate(2018,1,1));25onView(withId(R.id.timePicker)).perform(PickerActions.setTime(10, 10));
PickerActions
Using AI Code Generation
1PickerActions.setTime(12, 30);2public static ViewAction click() {3return new ViewAction() {4public Matcher<View> getConstraints() {5return allOf(isDisplayed(), isEnabled());6}7public String getDescription() {8return "clicking on view";9}10public void perform(UiController uiController, View view) {11view.performClick();12}13};14}15ViewActions.click();16ViewActions.closeSoftKeyboard();17ViewActions.pressBack();18ViewActions.pressImeActionButton();19ViewActions.pressKey(KeyEvent.KEYCODE_BACK);20ViewActions.pressMenuKey();21ViewActions.pressMenuItemKey();22ViewActions.pressVolumeDown();23ViewActions.pressVolumeUp();24ViewActions.scrollTo();25ViewActions.swipeDown();26ViewActions.swipeLeft();27ViewActions.swipeRight();28ViewActions.swipeUp();29ViewActions.typeText("Hello");30ViewActions.typeTextIntoFocusedView("Hello");31ViewActions.typeTextIntoFocusedView("Hello");32ViewActions.openLinkWithText("Hello");33ViewActions.openLinkWithText("Hello");
PickerActions
Using AI Code Generation
1PickerActions.setTime(23, 30);2PickerActions.setDate(2017, 5, 26);3PickerActions.setNumber(25);4PickerActions.setNumber(25);5PickerActions.setNumber(25);6PickerActions.setNumber(25);7PickerActions.setNumber(25);8PickerActions.setNumber(25);9PickerActions.setNumber(25);10PickerActions.setNumber(25);11PickerActions.setNumber(25);12PickerActions.setNumber(25);
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!!