diff --git a/CHANGELOG.md b/CHANGELOG.md index 59a67c7..cd495c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ ## [Unreleased] +# [1.1.7] - 2023-12-15 + +Changed +- Updated target and compile SDK to 34 +- Updated UiAutomator to 2.3.0-beta01 +- Input text now uses the return value of the UiAutomator to check if the text was entered successfully. `inputShouldBeRecognizedTimeout` is not needed anymore and removed from the `inputText` method. + # [1.1.6] - 2023-12-08 Changed: @@ -159,7 +166,8 @@ New: Initial release. -[unreleased]: https://github.com/getyourguide/UiTestGlaze/compare/1.1.6...HEAD +[unreleased]: https://github.com/getyourguide/UiTestGlaze/compare/1.1.7...HEAD +[1.1.5]: https://github.com/getyourguide/UiTestGlaze/releases/tag/1.1.7 [1.1.5]: https://github.com/getyourguide/UiTestGlaze/releases/tag/1.1.6 [1.1.5]: https://github.com/getyourguide/UiTestGlaze/releases/tag/1.1.5 [1.1.4]: https://github.com/getyourguide/UiTestGlaze/releases/tag/1.1.4 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 06d3d8e..ae2ba62 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,7 @@ kotlin = "1.9.0" gradleplugin = "8.1.0" androidx-annotation = "1.6.0" androidx-test-monitor = "1.6.1" -androidx-test-uiautomator = "2.2.0" +androidx-test-uiautomator = "2.3.0-beta01" androidx-navigation = "2.5.3" androidx-compose-bom = "2023.04.00" jackson-kotlin = "2.14.2" diff --git a/sample/build.gradle b/sample/build.gradle index 9bce340..8f5561b 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -6,12 +6,12 @@ plugins { android { namespace 'com.getyourguide.uitestglazesample' - compileSdk 33 + compileSdk 34 defaultConfig { applicationId "com.getyourguide.uitestglazesample" minSdk 24 - targetSdk 33 + targetSdk 34 versionCode 1 versionName "1.0" diff --git a/uiTestGlaze/build.gradle b/uiTestGlaze/build.gradle index 7c67abd..e454670 100644 --- a/uiTestGlaze/build.gradle +++ b/uiTestGlaze/build.gradle @@ -14,11 +14,11 @@ apply from: "${rootDir}/scripts/publish-module.gradle" android { namespace 'com.getyourguide.uitestglaze' - compileSdk 33 + compileSdk 34 defaultConfig { minSdk 21 - targetSdk 33 + targetSdk 34 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -48,5 +48,5 @@ dependencies { testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' + implementation 'androidx.test.espresso:espresso-core:3.5.1' } diff --git a/uiTestGlaze/src/main/java/com/getyourguide/uitestglazesample/InputTextHelper.kt b/uiTestGlaze/src/main/java/com/getyourguide/uitestglazesample/InputTextHelper.kt index bb80631..260d84c 100644 --- a/uiTestGlaze/src/main/java/com/getyourguide/uitestglazesample/InputTextHelper.kt +++ b/uiTestGlaze/src/main/java/com/getyourguide/uitestglazesample/InputTextHelper.kt @@ -2,30 +2,28 @@ package com.getyourguide.uitestglazesample import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiSelector -import kotlin.time.Duration internal class InputTextHelper( - private val config: UiTestGlaze.Config, private val getHierarchyHelper: GetHierarchyHelper, private val findUiElementHelper: FindUiElementHelper, - private val hierarchySettleHelper: HierarchySettleHelper, - private val printHierarchyHelper: PrintHierarchyHelper ) { fun inputText( text: String, uiElementIdentifier: UiElementIdentifier, device: UiDevice, - inputShouldBeRecognizedTimeout: Duration, + hierarchy: TreeNode, numberOfRetries: Int, ) { + var isEnteringTextSuccessfully: Boolean + var currentHierarchy = hierarchy var currentTry = 0 - while (numberOfRetries >= currentTry) { - val hierarchy = getHierarchyHelper.getHierarchy(device) + + do { val foundUiElement = findUiElementHelper.getUiElement( uiElementIdentifier, - hierarchy, + currentHierarchy, false, device, ) @@ -34,8 +32,7 @@ internal class InputTextHelper( if (foundUiElement.text == text) { return } - - when (uiElementIdentifier) { + isEnteringTextSuccessfully = when (uiElementIdentifier) { is UiElementIdentifier.PositionInHierarchy -> enterText( uiElementIdentifier.inputIndicatorText, @@ -57,45 +54,35 @@ internal class InputTextHelper( is UiElementIdentifier.Id, is UiElementIdentifier.TestTag, -> { + if (foundUiElement.resourceId == null) { + throw IllegalStateException("ResourceId not available to enter text") + } device.findObject( UiSelector().resourceId(foundUiElement.resourceId) .instance(uiElementIdentifier.index), - ).text = text + ).setText(text) } is UiElementIdentifier.Text, is UiElementIdentifier.TextResource, is UiElementIdentifier.TextRegex, -> { + if (foundUiElement.text == null) { + throw IllegalStateException("Text not available to enter text") + } device.findObject( UiSelector().text(foundUiElement.text).instance(uiElementIdentifier.index), - ).text = text + ).setText(text) } } - val startTime = System.currentTimeMillis() - var hierarchyChanged = false - do { - val hierarchyAfterEnteringText = hierarchySettleHelper.waitTillHierarchySettles( - config.loadingResourceIds, - device, - config.waitTillLoadingViewsGoneTimeout, - config.waitTillHierarchySettlesTimeout, - ) - printHierarchyHelper.print(hierarchyAfterEnteringText, "After entering text ") - if (hierarchy != hierarchyAfterEnteringText) { - hierarchyChanged = true - break - } - } while ((System.currentTimeMillis() - startTime) < inputShouldBeRecognizedTimeout.inWholeMilliseconds) - - if (!hierarchyChanged) { - if (currentTry == numberOfRetries) { - throw IllegalStateException("Timeout hit while waiting for text to appear") - } + if (!isEnteringTextSuccessfully) { + currentHierarchy = getHierarchyHelper.getHierarchy(device) currentTry++ - } else { - return } + } while (!isEnteringTextSuccessfully && currentTry < numberOfRetries) + + if (!isEnteringTextSuccessfully) { + throw IllegalStateException("Can not enter text") } } @@ -105,17 +92,17 @@ internal class InputTextHelper( uiElementResId: String?, device: UiDevice, text: String, - ) { - if (inputIndicatorText) { + ): Boolean { + return if (inputIndicatorText) { if (uiElementText == null) { throw IllegalStateException("Can not find text to enter text") } - device.findObject(UiSelector().text(uiElementText)).text = text + device.findObject(UiSelector().text(uiElementText)).setText(text) } else { if (uiElementResId == null) { throw IllegalStateException("Can not find resourceId to enter text") } - device.findObject(UiSelector().resourceId(uiElementResId)).text = text + device.findObject(UiSelector().resourceId(uiElementResId)).setText(text) } } } diff --git a/uiTestGlaze/src/main/java/com/getyourguide/uitestglazesample/UiTestGlaze.kt b/uiTestGlaze/src/main/java/com/getyourguide/uitestglazesample/UiTestGlaze.kt index 7ee5ada..11ed83c 100644 --- a/uiTestGlaze/src/main/java/com/getyourguide/uitestglazesample/UiTestGlaze.kt +++ b/uiTestGlaze/src/main/java/com/getyourguide/uitestglazesample/UiTestGlaze.kt @@ -65,20 +65,37 @@ data class UiTestGlaze( } } - private val logger = Logger(config.logger) - private val printHierarchyHelper = PrintHierarchyHelper(logger) + private val logger = Logger(logger = config.logger) + private val printHierarchyHelper = PrintHierarchyHelper(logger = logger) private val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - private val getHierarchyHelper = GetHierarchyHelper(logger) - private val findUiElementHelper = FindUiElementHelper(logger, getHierarchyHelper) - private val assertionHelper = AssertionHelper(findUiElementHelper) + private val getHierarchyHelper = GetHierarchyHelper(logger = logger) + private val findUiElementHelper = FindUiElementHelper( + logger = logger, + getHierarchyHelper = getHierarchyHelper, + ) + private val assertionHelper = AssertionHelper(findUiElementHelper = findUiElementHelper) private val hierarchySettleHelper = - HierarchySettleHelper(getHierarchyHelper, findUiElementHelper, logger) - private val inputTextHelper = - InputTextHelper(config, getHierarchyHelper, findUiElementHelper, hierarchySettleHelper, printHierarchyHelper) + HierarchySettleHelper( + getHierarchyHelper = getHierarchyHelper, + findUiElementHelper = findUiElementHelper, + logger = logger, + ) + private val inputTextHelper = InputTextHelper( + findUiElementHelper = findUiElementHelper, + getHierarchyHelper = getHierarchyHelper, + ) private val scrollHelper = - ScrollHelper(findUiElementHelper, getHierarchyHelper, hierarchySettleHelper) + ScrollHelper( + findUiElementHelper = findUiElementHelper, + getHierarchyHelper = getHierarchyHelper, + hierarchySettleHelper = hierarchySettleHelper, + ) private val tapHelper = - TapHelper(config, findUiElementHelper, hierarchySettleHelper) + TapHelper( + config = config, + findUiElementHelper = findUiElementHelper, + hierarchySettleHelper = hierarchySettleHelper, + ) /** * Tap on an element and expecting the UI to change. @@ -103,7 +120,7 @@ data class UiTestGlaze( longPress, offsetX, offsetY, - device + device, ) } @@ -205,16 +222,14 @@ data class UiTestGlaze( * * @param text Text to input. * @param uiElementIdentifier Identifier of the element to input text. - * @param inputShouldBeRecognizedTimeout Timeout to wait till the input is recognized. * @param numberOfRetries Number of times to retry if the input is not recognized. */ fun inputText( text: String, uiElementIdentifier: UiElementIdentifier, - inputShouldBeRecognizedTimeout: Duration = 5.seconds, - numberOfRetries: Int = 3 + numberOfRetries: Int = 3, ) { - hierarchySettleHelper.waitTillHierarchySettles( + val hierarchy = hierarchySettleHelper.waitTillHierarchySettles( config.loadingResourceIds, device, config.waitTillLoadingViewsGoneTimeout, @@ -224,8 +239,8 @@ data class UiTestGlaze( text = text, uiElementIdentifier = uiElementIdentifier, device = device, - inputShouldBeRecognizedTimeout = inputShouldBeRecognizedTimeout, - numberOfRetries = numberOfRetries + hierarchy = hierarchy, + numberOfRetries = numberOfRetries, ) }