diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt index aa60544f7b..c9e2d18a90 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt @@ -11,7 +11,6 @@ import android.content.Intent import android.net.Uri import android.os.SystemClock import android.util.Log -import android.widget.EditText import androidx.test.espresso.Espresso.onView import androidx.test.espresso.action.ViewActions import androidx.test.espresso.assertion.ViewAssertions.matches @@ -27,6 +26,7 @@ import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.uiautomator.By import androidx.test.uiautomator.By.text +import androidx.test.uiautomator.UiObject import androidx.test.uiautomator.UiObjectNotFoundException import androidx.test.uiautomator.UiSelector import androidx.test.uiautomator.Until @@ -344,14 +344,8 @@ class BrowserRobot { }.bookmarkPage { } } - fun clickLinkMatchingText(expectedText: String) { - mDevice.findObject(UiSelector().resourceId("$packageName:id/engineView")) - .waitForExists(waitingTime) - mDevice.findObject(UiSelector().textContains(expectedText)).waitForExists(waitingTimeLong) - - val element = mDevice.findObject(UiSelector().textContains(expectedText)) - element.click() - } + fun clickLinkMatchingText(expectedText: String) = + clickPageObject(webPageItemContainingText(expectedText)) fun longClickMatchingText(expectedText: String) { try { @@ -475,35 +469,10 @@ class BrowserRobot { switchButton.clickAndWaitForNewWindow(waitingTime) } - fun verifySaveLoginPromptIsShown() { - for (i in 1..RETRY_COUNT) { - try { - mDevice.findObject( - UiSelector() - .text("submit") - .resourceId("submit") - .className("android.widget.Button"), - ) - .also { it.waitForExists(waitingTime) } - .also { it.clickAndWaitForNewWindow(waitingTime) } - - break - } catch (e: UiObjectNotFoundException) { - if (i == RETRY_COUNT) { - throw e - } else { - browserScreen { - }.openThreeDotMenu { - }.refreshPage { } - } - } - } - } + fun verifySaveLoginPromptIsShown() = clickPageObject(webPageItemWithResourceId("submit")) fun verifyUpdateLoginPromptIsShown() { - val submitButton = mDevice.findObject(By.res("submit")) - submitButton.clickAndWait(Until.newWindow(), waitingTime) - + clickPageObject(webPageItemWithResourceId("submit")) mDevice.waitNotNull(Until.findObjects(text("Update"))) } @@ -517,63 +486,13 @@ class BrowserRobot { } fun enterPassword(password: String) { - val passwordField = mDevice.findObject( - UiSelector() - .resourceId("password") - .className(EditText::class.java), - ) - try { - passwordField.waitForExists(waitingTime) - mDevice.findObject( - By - .res("password") - .clazz(EditText::class.java), - ).click() - passwordField.clearTextField() - passwordField.text = password - // wait until the password is hidden - assertTrue(mDevice.findObject(UiSelector().text(password)).waitUntilGone(waitingTime)) - } catch (e: UiObjectNotFoundException) { - println(e) + clickPageObject(webPageItemWithResourceId("password")) + setPageObjectText(webPageItemWithResourceId("password"), password) - // Lets refresh the page and try again - browserScreen { - }.openThreeDotMenu { - }.refreshPage { - mDevice.waitForIdle() - } - } finally { - passwordField.waitForExists(waitingTime) - mDevice.findObject( - By - .res("password") - .clazz(EditText::class.java), - ).click() - passwordField.clearTextField() - passwordField.text = password - // wait until the password is hidden - assertTrue(mDevice.findObject(UiSelector().text(password)).waitUntilGone(waitingTime)) - } + assertTrue(mDevice.findObject(UiSelector().text(password)).waitUntilGone(waitingTime)) } - fun clickMediaPlayerPlayButton() { - for (i in 1..RETRY_COUNT) { - try { - mediaPlayerPlayButton().waitForExists(waitingTime) - mediaPlayerPlayButton().click() - - break - } catch (e: UiObjectNotFoundException) { - if (i == RETRY_COUNT) { - throw e - } else { - browserScreen { - }.openThreeDotMenu { - }.refreshPage { } - } - } - } - } + fun clickMediaPlayerPlayButton() = clickPageObject(webPageItemWithText("Play")) /** * Get the current playback state of the currently selected tab. @@ -637,26 +556,17 @@ class BrowserRobot { } fun fillAndSubmitLoginCredentials(userName: String, password: String) { - var currentTries = 0 - while (currentTries++ < 3) { - try { - mDevice.waitForIdle(waitingTime) - userNameTextBox.setText(userName) - passwordTextBox.setText(password) - submitLoginButton.click() - - mDevice.waitForObjects(mDevice.findObject(UiSelector().resourceId("$packageName:id/save_confirm"))) + mDevice.waitForIdle(waitingTime) + setPageObjectText(webPageItemWithResourceId("username"), userName) + setPageObjectText(webPageItemWithResourceId("password"), password) + clickPageObject(webPageItemWithResourceId("submit")) - break - } catch (e: UiObjectNotFoundException) { - Log.e("BROWSER_ROBOT", "Failed to find locator: ${e.localizedMessage}") - } - } + mDevice.waitForObjects(mDevice.findObject(UiSelector().resourceId("$packageName:id/save_confirm"))) } fun clearUserNameLoginCredential() { - mDevice.waitForObjects(userNameTextBox) - userNameTextBox.clearTextField() + mDevice.waitForObjects(webPageItemWithResourceId("username")) + webPageItemWithResourceId("username").clearTextField() mDevice.waitForIdle(waitingTime) } @@ -669,25 +579,19 @@ class BrowserRobot { mDevice.waitForObjects(suggestedLogins) break } catch (e: UiObjectNotFoundException) { - userNameTextBox.click() + clickPageObject(webPageItemWithResourceId("username")) } } } - fun clickStreetAddressTextBox() { - streetAddressTextBox().waitForExists(waitingTime) - streetAddressTextBox().click() - } + fun clickStreetAddressTextBox() = clickPageObject(webPageItemWithResourceId("streetAddress")) fun clickSelectAddressButton() { selectAddressButton.waitForExists(waitingTime) selectAddressButton.clickAndWaitForNewWindow(waitingTime) } - fun clickCardNumberTextBox() { - creditCardNumberTextBox().waitForExists(waitingTime) - creditCardNumberTextBox().click() - } + fun clickCardNumberTextBox() = clickPageObject(webPageItemWithResourceId("cardNumber")) fun clickSelectCreditCardButton() { selectCreditCardButton.waitForExists(waitingTime) @@ -731,8 +635,8 @@ class BrowserRobot { // Sometimes the assertion of the pre-filled logins fails so we are re-trying after refreshing the page while (currentTries++ < 3) { try { - mDevice.waitForObjects(userNameTextBox) - assertTrue(userNameTextBox.text.equals(userName)) + mDevice.waitForObjects(webPageItemWithResourceId("username")) + assertTrue(webPageItemWithResourceId("username").text.equals(userName)) break } catch (e: AssertionError) { @@ -746,18 +650,24 @@ class BrowserRobot { } } } - mDevice.waitForObjects(userNameTextBox) - assertTrue(userNameTextBox.text.equals(userName)) + mDevice.waitForObjects(webPageItemWithResourceId("username")) + assertTrue(webPageItemWithResourceId("username").text.equals(userName)) } fun verifyAutofilledAddress(streetAddress: String) { - mDevice.waitForObjects(streetAddressTextBox(streetAddress)) - assertTrue(streetAddressTextBox(streetAddress).waitForExists(waitingTime)) + mDevice.waitForObjects(webPageItemContainingTextAndResourceId("streetAddress", streetAddress)) + assertTrue( + webPageItemContainingTextAndResourceId("streetAddress", streetAddress) + .waitForExists(waitingTime), + ) } fun verifyAutofilledCreditCard(creditCardNumber: String) { - mDevice.waitForObjects(creditCardNumberTextBox(creditCardNumber)) - assertTrue(creditCardNumberTextBox(creditCardNumber).waitForExists(waitingTime)) + mDevice.waitForObjects(webPageItemContainingTextAndResourceId("cardNumber", creditCardNumber)) + assertTrue( + webPageItemContainingTextAndResourceId("cardNumber", creditCardNumber) + .waitForExists(waitingTime), + ) } fun verifyPrefilledPWALoginCredentials(userName: String, shortcutTitle: String) { @@ -766,9 +676,9 @@ class BrowserRobot { var currentTries = 0 while (currentTries++ < 3) { try { - assertTrue(submitLoginButton.waitForExists(waitingTime)) - submitLoginButton.click() - assertTrue(userNameTextBox.text.equals(userName)) + assertTrue(webPageItemWithResourceId("submit").waitForExists(waitingTime)) + webPageItemWithResourceId("submit").click() + assertTrue(webPageItemWithResourceId("username").text.equals(userName)) break } catch (e: AssertionError) { addToHomeScreen { @@ -811,11 +721,7 @@ class BrowserRobot { } } - fun clickSetCookiesButton() { - val setCookiesButton = mDevice.findObject(UiSelector().resourceId("setCookies")) - setCookiesButton.waitForExists(waitingTimeLong) - setCookiesButton.click() - } + fun clickSetCookiesButton() = clickPageObject(webPageItemWithResourceId("setCookies")) fun verifyCookiesProtectionHint() { val hintMessage = @@ -934,13 +840,12 @@ class BrowserRobot { } fun clickDownloadLink(title: String, interact: DownloadRobot.() -> Unit): DownloadRobot.Transition { - val downloadLink = mDevice.findObject(UiSelector().textContains(title)) - assertTrue( "$title download link not found", - downloadLink.waitForExists(waitingTime), + webPageItemContainingText(title).waitForExists(waitingTime), ) - downloadLink.click() + + clickPageObject(webPageItemContainingText(title)) DownloadRobot().interact() return DownloadRobot.Transition() @@ -948,8 +853,7 @@ class BrowserRobot { fun clickStartCameraButton(interact: SitePermissionsRobot.() -> Unit): SitePermissionsRobot.Transition { // Test page used for testing permissions located at https://mozilla-mobile.github.io/testapp/permissions - cameraButton.waitForExists(waitingTime) - cameraButton.click() + clickPageObject(webPageItemWithText("Open camera")) SitePermissionsRobot().interact() return SitePermissionsRobot.Transition() @@ -957,8 +861,7 @@ class BrowserRobot { fun clickStartMicrophoneButton(interact: SitePermissionsRobot.() -> Unit): SitePermissionsRobot.Transition { // Test page used for testing permissions located at https://mozilla-mobile.github.io/testapp/permissions - microphoneButton.waitForExists(waitingTime) - microphoneButton.click() + clickPageObject(webPageItemWithText("Open microphone")) SitePermissionsRobot().interact() return SitePermissionsRobot.Transition() @@ -966,8 +869,7 @@ class BrowserRobot { fun clickStartAudioVideoButton(interact: SitePermissionsRobot.() -> Unit): SitePermissionsRobot.Transition { // Test page used for testing permissions located at https://mozilla-mobile.github.io/testapp/permissions - audioVideoButton.waitForExists(waitingTime) - audioVideoButton.click() + clickPageObject(webPageItemWithText("Camera & Microphone")) SitePermissionsRobot().interact() return SitePermissionsRobot.Transition() @@ -975,8 +877,7 @@ class BrowserRobot { fun clickOpenNotificationButton(interact: SitePermissionsRobot.() -> Unit): SitePermissionsRobot.Transition { // Test page used for testing permissions located at https://mozilla-mobile.github.io/testapp/permissions - notificationButton.waitForExists(waitingTime) - notificationButton.click() + clickPageObject(webPageItemWithText("Open notifications dialogue")) mDevice.waitForObjects(mDevice.findObject(UiSelector().textContains("Allow to send notifications?"))) SitePermissionsRobot().interact() @@ -985,8 +886,7 @@ class BrowserRobot { fun clickGetLocationButton(interact: SitePermissionsRobot.() -> Unit): SitePermissionsRobot.Transition { // Test page used for testing permissions located at https://mozilla-mobile.github.io/testapp/permissions - getLocationButton.waitForExists(waitingTime) - getLocationButton.click() + clickPageObject(webPageItemWithText("Get Location")) SitePermissionsRobot().interact() return SitePermissionsRobot.Transition() @@ -1036,13 +936,6 @@ private fun assertMenuButton() { private fun tabsCounter() = mDevice.findObject(By.res("$packageName:id/counter_root")) -private fun mediaPlayerPlayButton() = - mDevice.findObject( - UiSelector() - .className("android.widget.Button") - .text("Play"), - ) - private var progressBar = mDevice.findObject( UiSelector().resourceId("$packageName:id/mozac_browser_toolbar_progress"), @@ -1059,24 +952,6 @@ private fun addressSuggestion(streetName: String) = .textContains(streetName), ) -private fun streetAddressTextBox(streetAddress: String = "") = - mDevice.findObject( - UiSelector() - .resourceId("streetAddress") - .textContains(streetAddress) - .className("android.widget.EditText") - .packageName("$packageName"), - ) - -private fun creditCardNumberTextBox(creditCardNumber: String = "") = - mDevice.findObject( - UiSelector() - .resourceId("cardNumber") - .textContains(creditCardNumber) - .className("android.widget.EditText") - .packageName("$packageName"), - ) - private fun creditCardSuggestion(creditCardNumber: String) = mDevice.findObject( UiSelector() @@ -1087,41 +962,64 @@ private fun creditCardSuggestion(creditCardNumber: String) = private fun siteSecurityToolbarButton() = mDevice.findObject(UiSelector().resourceId("$packageName:id/mozac_browser_toolbar_security_indicator")) -// Permissions test page elements & prompts -// Test page used located at https://mozilla-mobile.github.io/testapp/permissions -private val cameraButton = mDevice.findObject(UiSelector().text("Open camera")) +private fun clickPageObject(webPageItem: UiObject) { + for (i in 1..RETRY_COUNT) { + try { + webPageItem.also { + it.waitForExists(waitingTime) + it.click() + } -private val microphoneButton = mDevice.findObject(UiSelector().text("Open microphone")) + break + } catch (e: UiObjectNotFoundException) { + if (i == RETRY_COUNT) { + throw e + } else { + browserScreen { + }.openThreeDotMenu { + }.refreshPage { + progressBar.waitUntilGone(waitingTime) + } + } + } + } +} -private val audioVideoButton = mDevice.findObject(UiSelector().text("Camera & Microphone")) +private fun webPageItemContainingText(itemText: String) = + mDevice.findObject(UiSelector().textContains(itemText)) -private val notificationButton = mDevice.findObject(UiSelector().text("Open notifications dialogue")) +private fun webPageItemWithText(itemText: String) = + mDevice.findObject(UiSelector().text(itemText)) -private val getLocationButton = mDevice.findObject(UiSelector().text("Get Location")) +private fun webPageItemWithResourceId(resourceId: String) = + mDevice.findObject(UiSelector().resourceId(resourceId)) -// Login form test page elements -// Test page used located at https://mozilla-mobile.github.io/testapp/loginForm -val userNameTextBox = +private fun webPageItemContainingTextAndResourceId(resourceId: String, itemText: String) = mDevice.findObject( UiSelector() - .resourceId("username") - .className("android.widget.EditText") - .packageName("$packageName"), + .resourceId(resourceId) + .textContains(itemText), ) -private val submitLoginButton = - mDevice.findObject( - UiSelector() - .resourceId("submit") - .textContains("Submit Query") - .className("android.widget.Button") - .packageName("$packageName"), - ) +private fun setPageObjectText(webPageItem: UiObject, text: String) { + for (i in 1..RETRY_COUNT) { + try { + webPageItem.also { + it.waitForExists(waitingTime) + it.setText(text) + } -val passwordTextBox = - mDevice.findObject( - UiSelector() - .resourceId("password") - .className("android.widget.EditText") - .packageName("$packageName"), - ) + break + } catch (e: UiObjectNotFoundException) { + if (i == RETRY_COUNT) { + throw e + } else { + browserScreen { + }.openThreeDotMenu { + }.refreshPage { + progressBar.waitUntilGone(waitingTime) + } + } + } + } +} diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/CustomTabRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/CustomTabRobot.kt index 7730ec0d61..d4af3d7d4f 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/CustomTabRobot.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/CustomTabRobot.kt @@ -11,11 +11,13 @@ import androidx.test.espresso.matcher.ViewMatchers.withContentDescription import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.uiautomator.By +import androidx.test.uiautomator.UiObject import androidx.test.uiautomator.UiObjectNotFoundException import androidx.test.uiautomator.UiSelector import junit.framework.TestCase.assertTrue import org.mozilla.fenix.R import org.mozilla.fenix.helpers.Constants.LONG_CLICK_DURATION +import org.mozilla.fenix.helpers.Constants.RETRY_COUNT import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime import org.mozilla.fenix.helpers.TestHelper.appName import org.mozilla.fenix.helpers.TestHelper.mDevice @@ -92,49 +94,40 @@ class CustomTabRobot { } fun fillAndSubmitLoginCredentials(userName: String, password: String) { - var currentTries = 0 - while (currentTries++ < 3) { - try { - mDevice.waitForIdle(waitingTime) - userNameTextBox.setText(userName) - passwordTextBox.setText(password) - submitLoginButton.click() - mDevice.waitForObjects(mDevice.findObject(UiSelector().resourceId("$packageName:id/save_confirm"))) - break - } catch (e: UiObjectNotFoundException) { - customTabScreen { - }.openMainMenu { - refreshButton().click() - waitForPageToLoad() - } - } - } + mDevice.waitForIdle(waitingTime) + setPageObjectText(webPageItemWithResourceId("username"), userName) + setPageObjectText(webPageItemWithResourceId("password"), password) + clickPageObject(webPageItemWithResourceId("submit")) + mDevice.waitForObjects(mDevice.findObject(UiSelector().resourceId("$packageName:id/save_confirm"))) } - fun clickLinkMatchingText(expectedText: String) { - var currentTries = 0 - while (currentTries++ < 3) { + fun clickLinkMatchingText(expectedText: String) = clickPageObject(webPageItemContainingText(expectedText)) + + fun waitForPageToLoad() = progressBar.waitUntilGone(waitingTime) + + fun clickPageObject(webPageItem: UiObject) { + for (i in 1..RETRY_COUNT) { try { - mDevice.findObject(UiSelector().resourceId("$packageName:id/engineView")) - .waitForExists(waitingTime) - mDevice.findObject(UiSelector().textContains(expectedText)) - .waitForExists(waitingTime) + webPageItem.also { + it.waitForExists(waitingTime) + it.click() + } - val element = mDevice.findObject(UiSelector().textContains(expectedText)) - element.click() break } catch (e: UiObjectNotFoundException) { - customTabScreen { - }.openMainMenu { - refreshButton().click() - waitForPageToLoad() + if (i == RETRY_COUNT) { + throw e + } else { + browserScreen { + }.openThreeDotMenu { + }.refreshPage { + progressBar.waitUntilGone(waitingTime) + } } } } } - fun waitForPageToLoad() = progressBar.waitUntilGone(waitingTime) - class Transition { fun openMainMenu(interact: CustomTabRobot.() -> Unit): Transition { mainMenuButton().waitForExists(waitingTime) @@ -176,16 +169,36 @@ private fun closeButton() = onView(withContentDescription("Return to previous ap private fun customTabToolbar() = mDevice.findObject(By.res("$packageName:id/toolbar")) +private fun setPageObjectText(webPageItem: UiObject, text: String) { + for (i in 1..RETRY_COUNT) { + try { + webPageItem.also { + it.waitForExists(waitingTime) + it.setText(text) + } + + break + } catch (e: UiObjectNotFoundException) { + if (i == RETRY_COUNT) { + throw e + } else { + browserScreen { + }.openThreeDotMenu { + }.refreshPage { + progressBar.waitUntilGone(waitingTime) + } + } + } + } +} + private val progressBar = mDevice.findObject( UiSelector().resourceId("$packageName:id/mozac_browser_toolbar_progress"), ) -private val submitLoginButton = - mDevice.findObject( - UiSelector() - .resourceId("submit") - .textContains("Submit Query") - .className("android.widget.Button") - .packageName("$packageName"), - ) +private fun webPageItemContainingText(itemText: String) = + mDevice.findObject(UiSelector().textContains(itemText)) + +private fun webPageItemWithResourceId(resourceId: String) = + mDevice.findObject(UiSelector().resourceId(resourceId))