Skip to content

Commit

Permalink
update compile and target sdk, uiautomator dependency and improve inp…
Browse files Browse the repository at this point in the history
…utText logic
  • Loading branch information
BenedictP committed Dec 15, 2023
1 parent 6f85929 commit 39a37f3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 62 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions uiTestGlaze/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -34,8 +32,7 @@ internal class InputTextHelper(
if (foundUiElement.text == text) {
return
}

when (uiElementIdentifier) {
isEnteringTextSuccessfully = when (uiElementIdentifier) {
is UiElementIdentifier.PositionInHierarchy ->
enterText(
uiElementIdentifier.inputIndicatorText,
Expand All @@ -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")
}
}

Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -103,7 +120,7 @@ data class UiTestGlaze(
longPress,
offsetX,
offsetY,
device
device,
)
}

Expand Down Expand Up @@ -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,
Expand All @@ -224,8 +239,8 @@ data class UiTestGlaze(
text = text,
uiElementIdentifier = uiElementIdentifier,
device = device,
inputShouldBeRecognizedTimeout = inputShouldBeRecognizedTimeout,
numberOfRetries = numberOfRetries
hierarchy = hierarchy,
numberOfRetries = numberOfRetries,
)
}

Expand Down

0 comments on commit 39a37f3

Please sign in to comment.