Closes 10927: Improve wait on session loaded for UI tests (#10928)
parent
29be9e4b9f
commit
507ccaec7e
|
@ -0,0 +1,48 @@
|
||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
package org.mozilla.fenix.helpers
|
||||||
|
|
||||||
|
import androidx.test.core.app.ApplicationProvider
|
||||||
|
import androidx.test.espresso.IdlingResource
|
||||||
|
import org.mozilla.fenix.FenixApplication
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An IdlingResource implementation that waits until the current session is not loading anymore.
|
||||||
|
* Only after loading has completed further actions will be performed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class SessionLoadedIdlingResource : IdlingResource {
|
||||||
|
private var resourceCallback: IdlingResource.ResourceCallback? = null
|
||||||
|
|
||||||
|
override fun getName(): String {
|
||||||
|
return SessionLoadedIdlingResource::class.java.simpleName
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isIdleNow(): Boolean {
|
||||||
|
val context = ApplicationProvider.getApplicationContext<FenixApplication>()
|
||||||
|
val sessionManager = context.components.core.sessionManager
|
||||||
|
val session = sessionManager.selectedSession
|
||||||
|
|
||||||
|
return if (session?.loading == true) {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
if (session?.progress == 100) {
|
||||||
|
invokeCallback()
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun invokeCallback() {
|
||||||
|
if (resourceCallback != null) {
|
||||||
|
resourceCallback!!.onTransitionToIdle()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback) {
|
||||||
|
this.resourceCallback = callback
|
||||||
|
}
|
||||||
|
}
|
|
@ -79,7 +79,7 @@ class BrowserRobot {
|
||||||
*/
|
*/
|
||||||
fun verifyPageContent(expectedText: String) {
|
fun verifyPageContent(expectedText: String) {
|
||||||
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
|
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
|
||||||
mDevice.waitNotNull(Until.findObject(text(expectedText)), waitingTime)
|
mDevice.waitNotNull(Until.findObject(By.textContains(expectedText)), waitingTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun verifyTabCounter(expectedText: String) {
|
fun verifyTabCounter(expectedText: String) {
|
||||||
|
|
|
@ -8,25 +8,31 @@ package org.mozilla.fenix.ui.robots
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.test.espresso.Espresso.onView
|
import androidx.test.espresso.Espresso.onView
|
||||||
|
import androidx.test.espresso.IdlingRegistry
|
||||||
|
import androidx.test.espresso.IdlingResource
|
||||||
import androidx.test.espresso.action.ViewActions.pressImeActionButton
|
import androidx.test.espresso.action.ViewActions.pressImeActionButton
|
||||||
import androidx.test.espresso.action.ViewActions.replaceText
|
import androidx.test.espresso.action.ViewActions.replaceText
|
||||||
import androidx.test.espresso.action.ViewActions.typeText
|
import androidx.test.espresso.action.ViewActions.typeText
|
||||||
import androidx.test.espresso.assertion.ViewAssertions.matches
|
import androidx.test.espresso.assertion.ViewAssertions.matches
|
||||||
import androidx.test.espresso.matcher.ViewMatchers.withId
|
import androidx.test.espresso.matcher.ViewMatchers
|
||||||
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
|
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
|
||||||
import androidx.test.espresso.matcher.ViewMatchers.withText
|
|
||||||
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
|
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
|
||||||
|
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
|
||||||
|
import androidx.test.espresso.matcher.ViewMatchers.withId
|
||||||
|
import androidx.test.espresso.matcher.ViewMatchers.withText
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
import androidx.test.uiautomator.By
|
import androidx.test.uiautomator.By
|
||||||
import androidx.test.uiautomator.UiDevice
|
import androidx.test.uiautomator.UiDevice
|
||||||
import androidx.test.uiautomator.Until
|
import androidx.test.uiautomator.Until
|
||||||
|
import org.hamcrest.CoreMatchers.anyOf
|
||||||
import org.hamcrest.CoreMatchers.not
|
import org.hamcrest.CoreMatchers.not
|
||||||
import org.mozilla.fenix.R
|
import org.mozilla.fenix.R
|
||||||
|
import org.mozilla.fenix.helpers.SessionLoadedIdlingResource
|
||||||
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
|
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
|
||||||
import org.mozilla.fenix.helpers.click
|
|
||||||
import org.mozilla.fenix.helpers.ext.waitNotNull
|
|
||||||
import org.mozilla.fenix.helpers.assertions.AwesomeBarAssertion.Companion.suggestionsAreEqualTo
|
import org.mozilla.fenix.helpers.assertions.AwesomeBarAssertion.Companion.suggestionsAreEqualTo
|
||||||
import org.mozilla.fenix.helpers.assertions.AwesomeBarAssertion.Companion.suggestionsAreGreaterThan
|
import org.mozilla.fenix.helpers.assertions.AwesomeBarAssertion.Companion.suggestionsAreGreaterThan
|
||||||
|
import org.mozilla.fenix.helpers.click
|
||||||
|
import org.mozilla.fenix.helpers.ext.waitNotNull
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of Robot Pattern for the URL toolbar.
|
* Implementation of Robot Pattern for the URL toolbar.
|
||||||
|
@ -43,12 +49,15 @@ class NavigationToolbarRobot {
|
||||||
|
|
||||||
class Transition {
|
class Transition {
|
||||||
|
|
||||||
|
private lateinit var sessionLoadedIdlingResource: SessionLoadedIdlingResource
|
||||||
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
|
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
|
||||||
|
|
||||||
fun enterURLAndEnterToBrowser(
|
fun enterURLAndEnterToBrowser(
|
||||||
url: Uri,
|
url: Uri,
|
||||||
interact: BrowserRobot.() -> Unit
|
interact: BrowserRobot.() -> Unit
|
||||||
): BrowserRobot.Transition {
|
): BrowserRobot.Transition {
|
||||||
|
sessionLoadedIdlingResource = SessionLoadedIdlingResource()
|
||||||
|
|
||||||
mDevice.waitNotNull(
|
mDevice.waitNotNull(
|
||||||
Until.findObject(By.res("org.mozilla.fenix.debug:id/toolbar")),
|
Until.findObject(By.res("org.mozilla.fenix.debug:id/toolbar")),
|
||||||
waitingTime
|
waitingTime
|
||||||
|
@ -58,14 +67,28 @@ class NavigationToolbarRobot {
|
||||||
Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_edit_url_view")),
|
Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_edit_url_view")),
|
||||||
waitingTime
|
waitingTime
|
||||||
)
|
)
|
||||||
|
|
||||||
awesomeBar().perform(replaceText(url.toString()), pressImeActionButton())
|
awesomeBar().perform(replaceText(url.toString()), pressImeActionButton())
|
||||||
|
|
||||||
|
runWithIdleRes(sessionLoadedIdlingResource) {
|
||||||
|
onView(
|
||||||
|
anyOf(
|
||||||
|
ViewMatchers.withResourceName("browserLayout"),
|
||||||
|
ViewMatchers.withResourceName("onboarding_message") // Req ETP dialog
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
|
||||||
|
}
|
||||||
|
|
||||||
BrowserRobot().interact()
|
BrowserRobot().interact()
|
||||||
return BrowserRobot.Transition()
|
return BrowserRobot.Transition()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun openThreeDotMenu(interact: ThreeDotMenuMainRobot.() -> Unit): ThreeDotMenuMainRobot.Transition {
|
fun openThreeDotMenu(interact: ThreeDotMenuMainRobot.() -> Unit): ThreeDotMenuMainRobot.Transition {
|
||||||
mDevice.waitNotNull(Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_menu")), waitingTime)
|
mDevice.waitNotNull(
|
||||||
|
Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_menu")),
|
||||||
|
waitingTime
|
||||||
|
)
|
||||||
threeDotButton().click()
|
threeDotButton().click()
|
||||||
|
|
||||||
ThreeDotMenuMainRobot().interact()
|
ThreeDotMenuMainRobot().interact()
|
||||||
|
@ -76,10 +99,21 @@ class NavigationToolbarRobot {
|
||||||
url: Uri,
|
url: Uri,
|
||||||
interact: BrowserRobot.() -> Unit
|
interact: BrowserRobot.() -> Unit
|
||||||
): BrowserRobot.Transition {
|
): BrowserRobot.Transition {
|
||||||
|
sessionLoadedIdlingResource = SessionLoadedIdlingResource()
|
||||||
mDevice.waitNotNull(Until.findObject(By.descContains("Add tab")), waitingTime)
|
mDevice.waitNotNull(Until.findObject(By.descContains("Add tab")), waitingTime)
|
||||||
newTab().click()
|
newTab().click()
|
||||||
awesomeBar().perform(replaceText(url.toString()), pressImeActionButton())
|
awesomeBar().perform(replaceText(url.toString()), pressImeActionButton())
|
||||||
|
|
||||||
|
runWithIdleRes(sessionLoadedIdlingResource) {
|
||||||
|
onView(
|
||||||
|
anyOf(
|
||||||
|
ViewMatchers.withResourceName("browserLayout"),
|
||||||
|
ViewMatchers.withResourceName("onboarding_message") // Req for ETP dialog
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
|
||||||
|
}
|
||||||
|
|
||||||
BrowserRobot().interact()
|
BrowserRobot().interact()
|
||||||
return BrowserRobot.Transition()
|
return BrowserRobot.Transition()
|
||||||
}
|
}
|
||||||
|
@ -151,3 +185,11 @@ private fun newTab() = onView(withContentDescription("Add tab"))
|
||||||
private fun fillLinkButton() = onView(withId(R.id.fill_link_from_clipboard))
|
private fun fillLinkButton() = onView(withId(R.id.fill_link_from_clipboard))
|
||||||
private fun clearAddressBar() = onView(withId(R.id.mozac_browser_toolbar_clear_view))
|
private fun clearAddressBar() = onView(withId(R.id.mozac_browser_toolbar_clear_view))
|
||||||
private fun goBackButton() = mDevice.pressBack()
|
private fun goBackButton() = mDevice.pressBack()
|
||||||
|
inline fun runWithIdleRes(ir: IdlingResource?, pendingCheck: () -> Unit) {
|
||||||
|
try {
|
||||||
|
IdlingRegistry.getInstance().register(ir)
|
||||||
|
pendingCheck()
|
||||||
|
} finally {
|
||||||
|
IdlingRegistry.getInstance().unregister(ir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue