Best Appium-espresso-driver code snippet using android.support.test.espresso.matcher.ViewMatchers.hasFocus
Assertions.kt
Source:Assertions.kt
...96 * Checks if the view is focused97 */98 fun isFocused() {99 view.check(ViewAssertions.matches(100 ViewMatchers.hasFocus()))101 }102 /**103 * Checks if the view is not focused104 */105 fun isNotFocused() {106 view.check(ViewAssertions.matches(107 Matchers.not(ViewMatchers.hasFocus())))108 }109 /**110 * Checks if the view is focusable111 */112 fun isFocusable() {113 view.check(ViewAssertions.matches(114 ViewMatchers.isFocusable()))115 }116 /**117 * Checks if the view is not focusable118 */119 fun isNotFocusable() {120 view.check(ViewAssertions.matches(121 Matchers.not(ViewMatchers.isFocusable())))...
MainActivityTest.kt
Source:MainActivityTest.kt
...67 onView(withId(R.id.saveWord)).perform(click())68 addWordActivity.activity.finish()69 mainActivity.launchActivity(null)70 onData(withRowString(1, "sun"))71 .inAdapterView(allOf(withId(R.id.list_view_words), hasFocus()))72 .perform(click())73 onView(withText("УдалиÑÑ")).perform(click())74 val listView = mainActivity.activity.findViewById<ListView>(R.id.list_view_words)75 val adapter = listView.adapter as WordsCursorAdapter76 val cursor = adapter.cursor77 cursor.moveToFirst()78 val wordLists = ArrayList<String>()79 while (cursor.moveToNext()) {80 wordLists.add(cursor.getString(1))81 }82 Assert.assertFalse(wordLists.contains("sun"))83 // test below should check same thing that above,84 // but it throw exception RuntimeException(Row with "sun" not found)85 // it's problem of 'кÑивоÑÑкие пÑогÑаммиÑÑÑ' who writes Espresso framework86// onData(withRowString(1, "sun"))87// .inAdapterView(allOf(withId(R.id.list_view_words), hasFocus()))88// .check(doesNotExist())89 }90 @Test91 public fun testWordToStudied() {92 val intent = getIntent(AddWordActivity::class.java)93 intent.action = Intent.ACTION_SEND94 intent.type = "text/plain"95 intent.putExtra(Intent.EXTRA_TEXT, "sun")96 addWordActivity.launchActivity(intent)97 onView(withId(R.id.saveWord)).perform(click())98 addWordActivity.activity.finish()99 mainActivity.launchActivity(null)100 onData(withRowString(1, "sun"))101 .inAdapterView(allOf(withId(R.id.list_view_words), isFocusable()))...
PullDownToRefreshTest.kt
Source:PullDownToRefreshTest.kt
...27import android.support.test.espresso.action.ViewActions.replaceText28import android.support.test.espresso.action.ViewActions.swipeDown29import android.support.test.espresso.assertion.ViewAssertions.matches30import android.support.test.espresso.matcher.ViewMatchers.hasChildCount31import android.support.test.espresso.matcher.ViewMatchers.hasFocus32import android.support.test.espresso.matcher.ViewMatchers.isDisplayed33import android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast34import android.support.test.espresso.matcher.ViewMatchers.withId35import android.support.test.espresso.matcher.ViewMatchers.withText36import android.support.test.espresso.web.assertion.WebViewAssertions.webMatches37import android.support.test.espresso.web.sugar.Web.onWebView38import android.support.test.espresso.web.webdriver.DriverAtoms.findElement39import android.support.test.espresso.web.webdriver.DriverAtoms.getText40import org.hamcrest.Matchers.containsString41// https://testrail.stage.mozaws.net/index.php?/cases/view/9414642@RunWith(AndroidJUnit4::class)43@Ignore("Pull to refresh is currently disabled in all builds")44class PullDownToRefreshTest {45 private var webServer: MockWebServer? = null46 @Rule47 var mActivityTestRule: ActivityTestRule<MainActivity> = MainActivityFirstrunTestRule(false)48 @Before49 @Throws(IOException::class)50 fun setUpWebServer() {51 webServer = MockWebServer()52 // Test page53 webServer?.enqueue(MockResponse().setBody(TestHelper.readTestAsset("counter.html")))54 webServer?.enqueue(MockResponse().setBody(TestHelper.readTestAsset("counter.html")))55 }56 @After57 fun tearDownWebServer() {58 try {59 webServer?.close()60 webServer?.shutdown()61 } catch (e: IOException) {62 throw AssertionError("Could not stop web server", e)63 }64 }65 @Test66 fun pullDownToRefreshTest() {67 onView(withId(R.id.urlView))68 .check(matches(isDisplayed()))69 .check(matches(hasFocus()))70 .perform(click(), replaceText(webServer?.url("/").toString()), pressImeActionButton())71 onView(withId(R.id.display_url))72 .check(matches(isDisplayed()))73 .check(matches(withText(containsString(webServer?.hostName))))74 onWebView()75 .withElement(findElement(Locator.ID, COUNTER))76 .check(webMatches(getText(), containsString(FIRST_TIME)))77 onView(withId(R.id.swipe_refresh))78 .check(matches(isDisplayed()))79 checkSpinnerAndProgressBarAreShown()80 }81 companion object {82 private val COUNTER = "counter"83 private val FIRST_TIME = "1"...
EditItemActivity_SearchInWebViewTest.kt
Source:EditItemActivity_SearchInWebViewTest.kt
...51 Assert.viewIsNotVisible(getMatcherForSearchControls())52 onView(getMatcherForButtonToggleSearchControlsVisibility()).perform(click())53 TestUtil.sleep(2000)54 Assert.viewIsVisible(getMatcherForSearchControls())55 onView(getMatcherForSearchField()).check(matches(hasFocus()))56 onView(getMatcherForCountSearchResultsLabel()).check(matches(withText(57 testRule.activity.getString(net.dankito.richtexteditor.android.R.string.count_search_results_label, 0, 0))))58 onView(getMatcherForSearchField()).perform(typeText("Test"))59 onView(getMatcherForCountSearchResultsLabel()).check(matches(withText(containsString("46")))) // 46 search results60 onView(getMatcherForButtonToggleSearchControlsVisibility()).perform(click())61 Assert.viewIsNotVisible(getMatcherForSearchControls())62 Assert.viewIsVisible(R.id.lytFullscreenWebViewOptionsBar)63 }64 private fun getMatcherForButtonToggleSearchControlsVisibility(): Matcher<View> {65 return allOf(instanceOf(ImageButton::class.java), isDescendantOfA(withId(R.id.lytFullscreenWebViewOptionsBar)), withParent(instanceOf(SearchView::class.java)))66 }67 private fun getMatcherForSearchControls(): Matcher<View> {68 return allOf(instanceOf(LinearLayout::class.java), isDescendantOfA(withId(R.id.lytFullscreenWebViewOptionsBar)), isDescendantOfA(instanceOf(SearchView::class.java)))69 }...
NewsActivityTest.kt
Source:NewsActivityTest.kt
...40 }41 @Test42 fun activityNewsFragmentDisplaysProperly() {43 onView(withId(R.id.fragmentContainer))44 .check(matches(allOf(isDisplayed(), not(isClickable()), hasFocus())))45 onView(withId(R.id.fragmentContainer))46 .check(matches(hasChildCount(1)))47 }48 @Test49 fun activityNewsToolBar() {50 onView(withId(R.id.toolbar))51 .check(matches(allOf(isDisplayed(), not(isFocusable()), isEnabled())))52 onView(withId(R.id.toolbar))53 .check(matches(withToolbarTitle("News Application")))54 }55 @Test56 fun newsActivityNavBarDisplaysProperly() {57 onView(withId(R.id.bottomNavigationBar))58 .check(matches(allOf(isCompletelyDisplayed(),...
LoginActivityTest.kt
Source:LoginActivityTest.kt
...59 given(loginInteractor.doLogin(anyString()))60 .willReturn(Observable.error(Throwable("error")))61 onView(withId(R.id.email))62 .perform(click())63 .check(matches(hasFocus()))64 .perform(typeText("abcd"), closeSoftKeyboard())65 onView(withId(R.id.login))66 .perform(click())67 onView(allOf(withId(android.support.design.R.id.snackbar_text), withText("error")))68 .check(matches(isDisplayed()))69 }70 @Test71 fun testLoginActivity_shouldOpenFeed_whenEmailIsCorrect() {72 val expectedUser = User("henriquecocito@gmail.com", "123")73 given(loginInteractor.doLogin(anyString()))74 .willReturn(Observable.just(expectedUser))75 given(accountInteractor.save(expectedUser))76 .willReturn(Observable.just(expectedUser))77 onView(withId(R.id.email))78 .perform(click())79 .check(matches(hasFocus()))80 .perform(typeText(expectedUser.email), closeSoftKeyboard())81 onView(withId(R.id.login))82 .perform(click())83 intended(hasComponent(FeedActivity::class.java.name))84 }85}...
ViewMatchers.kt
Source:ViewMatchers.kt
...5import android.view.View6import 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....
ViewMatchersKtTest.kt
Source:ViewMatchersKtTest.kt
...8import org.hamcrest.CoreMatchers.not9import 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`() {...
hasFocus
Using AI Code Generation
1ViewInteraction appCompatEditText = onView(2allOf(withId(R.id.editText),3childAtPosition(4childAtPosition(5withId(android.R.id.content),6isDisplayed()));7appCompatEditText.perform(replaceText("hello"), closeSoftKeyboard());8ViewInteraction appCompatButton = onView(9allOf(withId(R.id.button), withText("Say Hello"),10childAtPosition(11childAtPosition(12withId(android.R.id.content),13isDisplayed()));14appCompatButton.perform(click());15ViewInteraction appCompatEditText2 = onView(16allOf(withId(R.id.editText), withText("hello"),17childAtPosition(18childAtPosition(19withId(android.R.id.content),20isDisplayed()));21appCompatEditText2.perform(click());22ViewInteraction appCompatEditText3 = onView(23allOf(withId(R.id.editText), withText("hello"),24childAtPosition(25childAtPosition(26withId(android.R.id.content),27isDisplayed()));28appCompatEditText3.perform(replaceText("hello world"));29ViewInteraction appCompatEditText4 = onView(30allOf(withId(R.id.editText), withText("hello world"),31childAtPosition(32childAtPosition(33withId(android.R.id.content),34isDisplayed()));35appCompatEditText4.perform(closeSoftKeyboard());36ViewInteraction appCompatButton2 = onView(37allOf(withId(R.id.button), withText("Say Hello"),38childAtPosition(39childAtPosition(40withId(android.R.id.content),41isDisplayed()));42appCompatButton2.perform(click());43}44}45ViewInteraction appCompatEditText2 = onView(46allOf(withId(R.id.editText), withText("hello"),47childAtPosition(48childAtPosition(49withId(android.R.id.content),50isDisplayed()));51appCompatEditText2.perform(click());52ViewInteraction appCompatEditText3 = onView(53allOf(withId(R.id.editText), withText("hello"),54childAtPosition(55childAtPosition(56withId(android.R.id.content),57isDisplayed()));
hasFocus
Using AI Code Generation
1ViewInteraction viewInteraction = onView(withId(R.id.fab));2viewInteraction.perform(click());3viewInteraction.check(matches(hasFocus()));4ViewInteraction viewInteraction = onView(withId(R.id.fab));5viewInteraction.perform(click());6viewInteraction.check(matches(isFocused()));7ViewInteraction viewInteraction = onView(withId(R.id.fab));8viewInteraction.perform(click());9viewInteraction.check(matches(isFocusable()));10ViewInteraction viewInteraction = onView(withId(R.id.fab));11viewInteraction.perform(click());12viewInteraction.check(matches(isFocusableInTouchMode()));13ViewInteraction viewInteraction = onView(withId(R.id.fab));14viewInteraction.perform(click());15viewInteraction.check(matches(isNotFocusable()));16ViewInteraction viewInteraction = onView(withId(R.id.fab));17viewInteraction.perform(click());18viewInteraction.check(matches(isNotFocusableInTouchMode()));19ViewInteraction viewInteraction = onView(withId(R.id.fab));20viewInteraction.perform(click());21viewInteraction.check(matches(isNotFocused()));22ViewInteraction viewInteraction = onView(withId(R.id.fab));23viewInteraction.perform(click());24viewInteraction.check(matches(isClickable()));25ViewInteraction viewInteraction = onView(withId(R.id.fab));26viewInteraction.perform(click());27viewInteraction.check(matches(isNotClickable()));28ViewInteraction viewInteraction = onView(withId(R.id.fab));29viewInteraction.perform(click());30viewInteraction.check(matches(isLongClickable()));31ViewInteraction viewInteraction = onView(withId(R.id.fab));32viewInteraction.perform(click());33viewInteraction.check(matches(isNotLongClickable()));34ViewInteraction viewInteraction = onView(withId(R.id.fab));
hasFocus
Using AI Code Generation
1ViewInteraction appCompatEditText = onView(2allOf(withId(R.id.etName), isDisplayed()));3appCompatEditText.perform(replaceText("Test"), closeSoftKeyboard());4ViewInteraction appCompatButton = onView(5allOf(withId(R.id.btnNext), withText("Next"), isDisplayed()));6appCompatButton.perform(click());7onView(withId(R.id.etName)).check(matches(hasFocus()));8}9}
hasFocus
Using AI Code Generation
1ViewInteraction view = onView(2allOf(withId(R.id.button), withText("Click Me"),3childAtPosition(4childAtPosition(5withClassName(is("android.widget.LinearLayout")),6isDisplayed()));7view.perform(click());8ViewInteraction textView = onView(9allOf(withId(R.id.textView), withText("Hello World!"),10childAtPosition(11childAtPosition(12withId(android.R.id.content),13isDisplayed()));14textView.check(matches(withText("Hello World!")));15ViewInteraction textView2 = onView(16allOf(withId(R.id.textView), withText("Hello World!"),17childAtPosition(18childAtPosition(19withId(android.R.id.content),20isDisplayed()));21textView2.check(matches(isDisplayed()));22}23}24ViewInteraction view = onView(25allOf(withId(R.id.button), withText("Click Me"),26childAtPosition(27childAtPosition(28withClassName(is("android.widget.LinearLayout")),29isDisplayed()));30view.perform(click());31ViewInteraction textView = onView(32allOf(withId(R.id.textView), withText("Hello World!"),33childAtPosition(34childAtPosition(35withId(android.R.id.content),36isDisplayed()));37textView.check(matches(hasFocus()));38ViewInteraction textView2 = onView(39allOf(withId(R.id.textView), withText("Hello World!"),40childAtPosition(41childAtPosition(42withId(android.R.id.content),43isDisplayed()));44textView2.check(matches(isDisplayed()));45}46}47ViewInteraction view = onView(48allOf(withId(R.id.button), withText("Click Me"),49childAtPosition(50childAtPosition(51withClassName(is("android.widget.LinearLayout")),52isDisplayed()));53view.perform(click());54ViewInteraction textView = onView(55allOf(withId(R.id.textView), withText("Hello World!"),56childAtPosition(57childAtPosition(58withId(android.R.id.content),59isDisplayed()));60textView.check(matches(isFocusable()));61ViewInteraction textView2 = onView(62allOf(withId(R.id.textView), withText("Hello World!"),63childAtPosition(64childAtPosition(65withId(android.R.id.content),66isDisplayed()));67textView2.check(matches(isDisplayed()));68}69}
hasFocus
Using AI Code Generation
1ViewInteraction viewInteraction = onView(allOf(withId(R.id.editText), isDisplayed()));2viewInteraction.perform(click());3viewInteraction.perform(replaceText("test"), closeSoftKeyboard());4ViewInteraction viewInteraction1 = onView(allOf(withId(R.id.button), isDisplayed()));5viewInteraction1.perform(click());6ViewInteraction viewInteraction2 = onView(allOf(withId(R.id.editText), isDisplayed()));7viewInteraction2.perform(click());8viewInteraction2.perform(replaceText("test"), closeSoftKeyboard());9ViewInteraction viewInteraction3 = onView(allOf(withId(R.id.button), isDisplayed()));10viewInteraction3.perform(click());11ViewInteraction viewInteraction4 = onView(allOf(withId(R.id.editText), isDisplayed()));12viewInteraction4.perform(click());13viewInteraction4.perform(replaceText("test"), closeSoftKeyboard());14ViewInteraction viewInteraction5 = onView(allOf(withId(R.id.button), isDisplayed()));15viewInteraction5.perform(click());16ViewInteraction viewInteraction6 = onView(allOf(withId(R.id.editText), isDisplayed()));17viewInteraction6.perform(click());18viewInteraction6.perform(replaceText("test"), closeSoftKeyboard());19ViewInteraction viewInteraction7 = onView(allOf(withId(R.id.button), isDisplayed()));20viewInteraction7.perform(click());21ViewInteraction viewInteraction8 = onView(allOf(withId(R.id.editText), isDisplayed()));22viewInteraction8.perform(click());23viewInteraction8.perform(replaceText("test"), closeSoftKeyboard());24ViewInteraction viewInteraction9 = onView(allOf(withId(R.id.button), isDisplayed()));25viewInteraction9.perform(click());26ViewInteraction viewInteraction10 = onView(allOf(withId(R.id.editText), isDisplayed()));27viewInteraction10.perform(click());28viewInteraction10.perform(replaceText("test"), closeSoftKeyboard());29ViewInteraction viewInteraction11 = onView(allOf(withId(R.id.button), isDisplayed()));30viewInteraction11.perform(click());31ViewInteraction viewInteraction12 = onView(allOf(withId(R.id.editText), isDisplayed()));32viewInteraction12.perform(click());33viewInteraction12.perform(replaceText("test"), closeSoftKeyboard());34ViewInteraction viewInteraction13 = onView(allOf(withId(R.id.button), isDisplayed()));35viewInteraction13.perform(click());36ViewInteraction viewInteraction14 = onView(allOf(withId(R.id.editText), isDisplayed()));37viewInteraction14.perform(click());38viewInteraction14.perform(replaceText("test"), closeSoftKeyboard());39ViewInteraction viewInteraction15 = onView(allOf(withId(R.id.button), isDisplayed()));40viewInteraction15.perform(click());41ViewInteraction viewInteraction16 = onView(allOf(withId(R.id.edit
hasFocus
Using AI Code Generation
1ViewActions.closeSoftKeyboard();2ViewMatchers.hasFocus();3ViewActions.closeSoftKeyboard();4ViewActions.typeTextInFocusedView("Hello World");5ViewActions.closeSoftKeyboard();6ViewActions.clearText();7ViewActions.closeSoftKeyboard();8ViewActions.pressKey(KeyEvent.KEYCODE_1);9ViewActions.closeSoftKeyboard();10ViewActions.pressImeActionButton();11ViewActions.closeSoftKeyboard();12ViewActions.pressBack();13ViewActions.closeSoftKeyboard();14ViewActions.pressMenuKey();15ViewActions.closeSoftKeyboard();16ViewActions.pressMenuItemId(R.id.menu_item_id);17ViewActions.closeSoftKeyboard();18ViewActions.pressBackUnconditionally();19ViewActions.closeSoftKeyboard();20ViewActions.pressKey(KeyEvent.KEYCODE_1);
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!!