For #26286 new web control UI tests

This commit is contained in:
AndiAJ 2022-10-20 13:52:06 +03:00 committed by mergify[bot]
parent ce3d6b84ca
commit 6998809039
3 changed files with 393 additions and 0 deletions

View File

@ -99,6 +99,18 @@ object TestAssetHelper {
return TestAsset(url, "", "")
}
fun getHTMLControlsFormAsset(server: MockWebServer): TestAsset {
val url = server.url("pages/htmlControls.html").toString().toUri()!!
return TestAsset(url, "", "")
}
fun getExternalLinksAsset(server: MockWebServer): TestAsset {
val url = server.url("pages/externalLinks.html").toString().toUri()!!
return TestAsset(url, "", "")
}
fun getAudioPageAsset(server: MockWebServer): TestAsset {
val url = server.url("pages/audioMediaPage.html").toString().toUri()!!
val title = "Audio_Test_Page"

View File

@ -0,0 +1,150 @@
/* 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.ui
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.helpers.AndroidAssetDispatcher
import org.mozilla.fenix.helpers.HomeActivityTestRule
import org.mozilla.fenix.helpers.TestAssetHelper
import org.mozilla.fenix.ui.robots.navigationToolbar
/**
* Tests for verifying basic interactions with web control elements
*
*/
class WebControlsTest {
private lateinit var mockWebServer: MockWebServer
private val hour = 10
private val minute = 10
private val colorHexValue = "#5b2067"
@get:Rule
val activityTestRule = HomeActivityTestRule(
isJumpBackInCFREnabled = false,
isTCPCFREnabled = false,
)
@Before
fun setUp() {
mockWebServer = MockWebServer().apply {
dispatcher = AndroidAssetDispatcher()
start()
}
}
@After
fun tearDown() {
mockWebServer.shutdown()
}
@Test
fun cancelCalendarFormTest() {
val htmlControlsPage = TestAssetHelper.getHTMLControlsFormAsset(mockWebServer)
navigationToolbar {
}.enterURLAndEnterToBrowser(htmlControlsPage.url) {
clickForm("Calendar Form")
clickFormViewButton("CANCEL")
clickSubmitDateButton()
verifyNoDateIsSelected()
}
}
@Test
fun setAndClearCalendarFormTest() {
val htmlControlsPage = TestAssetHelper.getHTMLControlsFormAsset(mockWebServer)
navigationToolbar {
}.enterURLAndEnterToBrowser(htmlControlsPage.url) {
clickForm("Calendar Form")
selectDate()
clickFormViewButton("OK")
clickSubmitDateButton()
verifySelectedDate()
clickForm("Calendar Form")
clickFormViewButton("CLEAR")
clickSubmitDateButton()
verifyNoDateIsSelected()
}
}
@Test
fun cancelClockFormTest() {
val htmlControlsPage = TestAssetHelper.getHTMLControlsFormAsset(mockWebServer)
navigationToolbar {
}.enterURLAndEnterToBrowser(htmlControlsPage.url) {
clickForm("Clock Form")
clickFormViewButton("CANCEL")
clickSubmitTimeButton()
verifyNoTimeIsSelected(hour, minute)
}
}
@Test
fun setAndClearClockFormTest() {
val htmlControlsPage = TestAssetHelper.getHTMLControlsFormAsset(mockWebServer)
navigationToolbar {
}.enterURLAndEnterToBrowser(htmlControlsPage.url) {
clickForm("Clock Form")
selectTime(hour, minute)
clickFormViewButton("OK")
clickSubmitTimeButton()
verifySelectedTime(hour, minute)
clickForm("Clock Form")
clickFormViewButton("CLEAR")
clickSubmitTimeButton()
verifyNoTimeIsSelected(hour, minute)
}
}
@Test
fun cancelColorFormTest() {
val htmlControlsPage = TestAssetHelper.getHTMLControlsFormAsset(mockWebServer)
navigationToolbar {
}.enterURLAndEnterToBrowser(htmlControlsPage.url) {
clickForm("Color Form")
selectColor(colorHexValue)
clickFormViewButton("CANCEL")
clickSubmitColorButton()
verifyColorIsNotSelected(colorHexValue)
}
}
@Test
fun setColorFormTest() {
val htmlControlsPage = TestAssetHelper.getHTMLControlsFormAsset(mockWebServer)
navigationToolbar {
}.enterURLAndEnterToBrowser(htmlControlsPage.url) {
clickForm("Color Form")
selectColor(colorHexValue)
clickFormViewButton("SET")
clickSubmitColorButton()
verifySelectedColor(colorHexValue)
}
}
@Test
fun verifyDropdownMenuTest() {
val htmlControlsPage = TestAssetHelper.getHTMLControlsFormAsset(mockWebServer)
navigationToolbar {
}.enterURLAndEnterToBrowser(htmlControlsPage.url) {
clickForm("Drop-down Form")
selectDropDownOption("The National")
clickSubmitDropDownButton()
verifySelectedDropDownOption("The National")
}
}
}

View File

@ -11,14 +11,17 @@ import android.content.Intent
import android.net.Uri
import android.os.SystemClock
import android.util.Log
import android.widget.TimePicker
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.PickerActions
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.BundleMatchers
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.matcher.RootMatchers.isDialog
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
@ -34,6 +37,7 @@ import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.mediasession.MediaSession
import org.hamcrest.CoreMatchers.allOf
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.mozilla.fenix.R
@ -49,6 +53,7 @@ import org.mozilla.fenix.helpers.TestHelper.packageName
import org.mozilla.fenix.helpers.TestHelper.waitForObjects
import org.mozilla.fenix.helpers.click
import org.mozilla.fenix.helpers.ext.waitNotNull
import java.time.LocalDate
class BrowserRobot {
private lateinit var sessionLoadedIdlingResource: SessionLoadedIdlingResource
@ -651,6 +656,227 @@ class BrowserRobot {
assertTrue(hintMessage.waitForExists(waitingTime))
}
fun clickForm(formType: String) {
when (formType) {
"Calendar Form" -> {
clickPageObject(webPageItemWithResourceId("calendar"))
mDevice.waitForIdle(waitingTime)
}
"Clock Form" -> {
clickPageObject(webPageItemWithResourceId("clock"))
mDevice.waitForIdle(waitingTime)
}
"Color Form" -> {
clickPageObject(webPageItemWithResourceId("colorPicker"))
mDevice.waitForIdle(waitingTime)
}
"Drop-down Form" -> {
clickPageObject(webPageItemWithResourceId("dropDown"))
mDevice.waitForIdle(waitingTime)
}
}
}
fun clickFormViewButton(button: String) = mDevice.findObject(UiSelector().textContains(button)).click()
fun selectDate() {
mDevice.findObject(UiSelector().resourceId("android:id/month_view")).waitForExists(waitingTime)
mDevice.findObject(
UiSelector()
.textContains("$currentDay")
.descriptionContains("$currentDay $currentMonth $currentYear"),
).click()
}
fun selectTime(hour: Int, minute: Int) =
onView(
isAssignableFrom(TimePicker::class.java),
).inRoot(
isDialog(),
).perform(PickerActions.setTime(hour, minute))
fun selectColor(hexValue: String) {
mDevice.findObject(
UiSelector()
.textContains("Choose a color")
.resourceId("$packageName:id/alertTitle"),
).waitForExists(waitingTime)
val colorSelection =
mDevice.findObject(
UiSelector()
.resourceId("$packageName:id/color_item")
.descriptionContains(hexValue),
)
colorSelection.click()
}
fun selectDropDownOption(optionName: String) {
mDevice.findObject(
UiSelector().resourceId("$packageName:id/customPanel"),
).waitForExists(waitingTime)
mDevice.findObject(UiSelector().textContains(optionName)).click()
}
fun clickSubmitDateButton() = clickPageObject(webPageItemWithResourceId("submitDate"))
fun clickSubmitTimeButton() = clickPageObject(webPageItemWithResourceId("submitTime"))
fun clickSubmitColorButton() = clickPageObject(webPageItemWithResourceId("submitColor"))
fun clickSubmitDropDownButton() = clickPageObject(webPageItemWithResourceId("submitOption"))
fun verifySelectedDate() {
for (i in 1..RETRY_COUNT) {
try {
assertTrue(
mDevice.findObject(
UiSelector()
.text("Selected date is: $currentDate"),
).waitForExists(waitingTime),
)
break
} catch (e: AssertionError) {
Log.e("TestLog", "Selected time isn't displayed ${e.localizedMessage}")
clickForm("Calendar Form")
selectDate()
clickFormViewButton("OK")
clickSubmitDateButton()
}
}
assertTrue(
mDevice.findObject(
UiSelector()
.text("Selected date is: $currentDate"),
).waitForExists(waitingTime),
)
}
fun verifyNoDateIsSelected() {
assertFalse(
mDevice.findObject(
UiSelector()
.text("Selected date is: $currentDate"),
).waitForExists(waitingTime),
)
}
fun verifySelectedTime(hour: Int, minute: Int) {
for (i in 1..RETRY_COUNT) {
try {
assertTrue(
mDevice.findObject(
UiSelector()
.text("Selected time is: $hour:$minute"),
).waitForExists(waitingTime),
)
break
} catch (e: AssertionError) {
Log.e("TestLog", "Selected time isn't displayed ${e.localizedMessage}")
clickForm("Clock Form")
clickFormViewButton("CLEAR")
clickForm("Clock Form")
selectTime(hour, minute)
clickFormViewButton("OK")
clickSubmitTimeButton()
}
}
assertTrue(
mDevice.findObject(
UiSelector()
.text("Selected time is: $hour:$minute"),
).waitForExists(waitingTime),
)
}
fun verifySelectedColor(hexValue: String) {
for (i in 1..RETRY_COUNT) {
try {
assertTrue(
mDevice.findObject(
UiSelector()
.text("Selected color is: $hexValue"),
).waitForExists(waitingTime),
)
break
} catch (e: AssertionError) {
Log.e("TestLog", "Selected color isn't displayed ${e.localizedMessage}")
clickForm("Color Form")
selectColor(hexValue)
clickFormViewButton("SET")
clickSubmitColorButton()
}
}
assertTrue(
mDevice.findObject(
UiSelector()
.text("Selected color is: $hexValue"),
).waitForExists(waitingTime),
)
}
fun verifySelectedDropDownOption(optionName: String) {
for (i in 1..RETRY_COUNT) {
try {
mDevice.findObject(
UiSelector()
.textContains("Submit drop down option")
.resourceId("submitOption"),
).waitForExists(waitingTime)
assertTrue(
mDevice.findObject(
UiSelector()
.text("Selected option is: $optionName"),
).waitForExists(waitingTime),
)
break
} catch (e: AssertionError) {
Log.e("TestLog", "Selected option isn't displayed ${e.localizedMessage}")
clickForm("Drop-down Form")
selectDropDownOption(optionName)
clickSubmitDropDownButton()
}
}
assertTrue(
mDevice.findObject(
UiSelector()
.text("Selected option is: $optionName"),
).waitForExists(waitingTime),
)
}
fun verifyNoTimeIsSelected(hour: Int, minute: Int) {
assertFalse(
mDevice.findObject(
UiSelector()
.text("Selected date is: $hour:$minute"),
).waitForExists(waitingTime),
)
}
fun verifyColorIsNotSelected(hexValue: String) {
assertFalse(
mDevice.findObject(
UiSelector()
.text("Selected date is: $hexValue"),
).waitForExists(waitingTime),
)
}
class Transition {
private fun threeDotButton() = onView(
allOf(
@ -983,3 +1209,8 @@ private fun setPageObjectText(webPageItem: UiObject, text: String) {
}
}
}
private val currentDate = LocalDate.now()
private val currentDay = currentDate.dayOfMonth
private val currentMonth = currentDate.month
private val currentYear = currentDate.year