From c0458557ea3c3b246e18ac897bb0ff0cfacbbf3b Mon Sep 17 00:00:00 2001 From: Muhammed Furkan Boran <52128580+boranfrkn@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:36:55 +0300 Subject: [PATCH] Fix evaluate script bug (#2023) * Fix evaluate script bug * Fix test --------- Co-authored-by: Muhammed Furkan Boran --- .../src/main/java/maestro/orchestra/Commands.kt | 12 +++++++++--- .../src/main/java/maestro/orchestra/Orchestra.kt | 2 +- .../java/maestro/orchestra/yaml/YamlFluentCommand.kt | 7 +------ .../maestro/orchestra/yaml/YamlScrollUntilVisible.kt | 2 +- .../maestro/orchestra/yaml/YamlCommandReaderTest.kt | 2 +- .../src/test/kotlin/maestro/test/IntegrationTest.kt | 6 +++++- .../test/resources/117_scroll_until_visible_speed.js | 5 +++++ .../resources/117_scroll_until_visible_speed.yaml | 3 ++- 8 files changed, 25 insertions(+), 14 deletions(-) diff --git a/maestro-orchestra-models/src/main/java/maestro/orchestra/Commands.kt b/maestro-orchestra-models/src/main/java/maestro/orchestra/Commands.kt index 0d721b82f6..eed7c3d390 100644 --- a/maestro-orchestra-models/src/main/java/maestro/orchestra/Commands.kt +++ b/maestro-orchestra-models/src/main/java/maestro/orchestra/Commands.kt @@ -101,7 +101,7 @@ data class ScrollUntilVisibleCommand( val direction: ScrollDirection, val scrollDuration: String = DEFAULT_SCROLL_DURATION, val visibilityPercentage: Int, - val timeout: Long = DEFAULT_TIMEOUT_IN_MILLIS, + val timeout: String = DEFAULT_TIMEOUT_IN_MILLIS, val centerElement: Boolean, override val label: String? = null, override val optional: Boolean = false, @@ -113,6 +113,11 @@ data class ScrollUntilVisibleCommand( return ((1000 * (100 - this.toLong()).toDouble() / 100).toLong() + 1).toString() } + private fun String.timeoutToMillis(): String { + val timeout = if (this.toLong() < 0) { DEFAULT_TIMEOUT_IN_MILLIS.toLong() * 1000L } else this.toLong() * 1000L + return timeout.toString() + } + override fun description(): String { return label ?: "Scrolling $direction until ${selector.description()} is visible." } @@ -120,12 +125,13 @@ data class ScrollUntilVisibleCommand( override fun evaluateScripts(jsEngine: JsEngine): ScrollUntilVisibleCommand { return copy( selector = selector.evaluateScripts(jsEngine), - scrollDuration = scrollDuration.evaluateScripts(jsEngine).speedToDuration() + scrollDuration = scrollDuration.evaluateScripts(jsEngine).speedToDuration(), + timeout = timeout.evaluateScripts(jsEngine).timeoutToMillis(), ) } companion object { - const val DEFAULT_TIMEOUT_IN_MILLIS = 20 * 1000L + const val DEFAULT_TIMEOUT_IN_MILLIS = "20" const val DEFAULT_SCROLL_DURATION = "40" const val DEFAULT_ELEMENT_VISIBILITY_PERCENTAGE = 100 const val DEFAULT_CENTER_ELEMENT = false diff --git a/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt b/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt index 266ff2014d..fc1d716fad 100644 --- a/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt +++ b/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt @@ -486,7 +486,7 @@ class Orchestra( } private fun scrollUntilVisible(command: ScrollUntilVisibleCommand): Boolean { - val endTime = System.currentTimeMillis() + command.timeout + val endTime = System.currentTimeMillis() + command.timeout.toLong() val direction = command.direction.toSwipeDirection() val deviceInfo = maestro.deviceInfo() diff --git a/maestro-orchestra/src/main/java/maestro/orchestra/yaml/YamlFluentCommand.kt b/maestro-orchestra/src/main/java/maestro/orchestra/yaml/YamlFluentCommand.kt index 952481649d..b658744bb0 100644 --- a/maestro-orchestra/src/main/java/maestro/orchestra/yaml/YamlFluentCommand.kt +++ b/maestro-orchestra/src/main/java/maestro/orchestra/yaml/YamlFluentCommand.kt @@ -614,17 +614,12 @@ data class YamlFluentCommand( } private fun scrollUntilVisibleCommand(yaml: YamlScrollUntilVisible): MaestroCommand { - val timeout = - if (yaml.timeout < 0) { - ScrollUntilVisibleCommand.DEFAULT_TIMEOUT_IN_MILLIS - } else yaml.timeout - val visibility = if (yaml.visibilityPercentage < 0) 0 else if (yaml.visibilityPercentage > 100) 100 else yaml.visibilityPercentage return MaestroCommand( ScrollUntilVisibleCommand( selector = toElementSelector(yaml.element), direction = yaml.direction, - timeout = timeout, + timeout = yaml.timeout, scrollDuration = yaml.speed, visibilityPercentage = visibility, centerElement = yaml.centerElement, diff --git a/maestro-orchestra/src/main/java/maestro/orchestra/yaml/YamlScrollUntilVisible.kt b/maestro-orchestra/src/main/java/maestro/orchestra/yaml/YamlScrollUntilVisible.kt index 8ed384709e..c2e4054b00 100644 --- a/maestro-orchestra/src/main/java/maestro/orchestra/yaml/YamlScrollUntilVisible.kt +++ b/maestro-orchestra/src/main/java/maestro/orchestra/yaml/YamlScrollUntilVisible.kt @@ -8,7 +8,7 @@ data class YamlScrollUntilVisible( @JsonFormat(with = [JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES]) val direction: ScrollDirection = ScrollDirection.DOWN, val element: YamlElementSelectorUnion, - val timeout: Long = ScrollUntilVisibleCommand.DEFAULT_TIMEOUT_IN_MILLIS, + val timeout: String = ScrollUntilVisibleCommand.DEFAULT_TIMEOUT_IN_MILLIS, val speed: String = ScrollUntilVisibleCommand.DEFAULT_SCROLL_DURATION, val visibilityPercentage: Int = ScrollUntilVisibleCommand.DEFAULT_ELEMENT_VISIBILITY_PERCENTAGE, val centerElement: Boolean = ScrollUntilVisibleCommand.DEFAULT_CENTER_ELEMENT, diff --git a/maestro-orchestra/src/test/java/maestro/orchestra/yaml/YamlCommandReaderTest.kt b/maestro-orchestra/src/test/java/maestro/orchestra/yaml/YamlCommandReaderTest.kt index e31b4cec90..19db099240 100644 --- a/maestro-orchestra/src/test/java/maestro/orchestra/yaml/YamlCommandReaderTest.kt +++ b/maestro-orchestra/src/test/java/maestro/orchestra/yaml/YamlCommandReaderTest.kt @@ -433,7 +433,7 @@ internal class YamlCommandReaderTest { ScrollUntilVisibleCommand( selector = ElementSelector(textRegex = "Footer"), direction = ScrollDirection.DOWN, - timeout = 20000, + timeout = "20", scrollDuration = "40", visibilityPercentage = 100, label = "Scroll to the bottom", diff --git a/maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt b/maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt index 1acf1e7dca..9c95ca4d68 100644 --- a/maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt +++ b/maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt @@ -3100,10 +3100,11 @@ class IntegrationTest { } @Test - fun `Case 117 - Scroll until view is visible - with speed evaluate`() { + fun `Case 117 - Scroll until view is visible - with speed and timeout evaluate`() { // Given val commands = readCommands("117_scroll_until_visible_speed") val expectedDuration = "601" + val expectedTimeout = "20000" val info = driver { }.deviceInfo() val elementBounds = Bounds(0, 0 + info.heightGrid, 100, 100 + info.heightGrid) @@ -3116,14 +3117,17 @@ class IntegrationTest { // When var scrollDuration = "0" + var timeout = "0" Maestro(driver).use { orchestra(it, onCommandMetadataUpdate = { _, metaData -> scrollDuration = metaData.evaluatedCommand?.scrollUntilVisible?.scrollDuration.toString() + timeout = metaData.evaluatedCommand?.scrollUntilVisible?.timeout.toString() }).runFlow(commands) } // Then assertThat(scrollDuration).isEqualTo(expectedDuration) + assertThat(timeout).isEqualTo(expectedTimeout) driver.assertEvents( listOf( Event.SwipeElementWithDirection(Point(270, 480), SwipeDirection.UP, expectedDuration.toLong()), diff --git a/maestro-test/src/test/resources/117_scroll_until_visible_speed.js b/maestro-test/src/test/resources/117_scroll_until_visible_speed.js index ad33541120..cca0b994ac 100644 --- a/maestro-test/src/test/resources/117_scroll_until_visible_speed.js +++ b/maestro-test/src/test/resources/117_scroll_until_visible_speed.js @@ -1,6 +1,11 @@ output.speed = { slow: 40 } + +output.timeout = { + slow: 20 +} + output.element = { id: "maestro" } \ No newline at end of file diff --git a/maestro-test/src/test/resources/117_scroll_until_visible_speed.yaml b/maestro-test/src/test/resources/117_scroll_until_visible_speed.yaml index 656d554465..1424096f15 100644 --- a/maestro-test/src/test/resources/117_scroll_until_visible_speed.yaml +++ b/maestro-test/src/test/resources/117_scroll_until_visible_speed.yaml @@ -5,4 +5,5 @@ appId: com.example.app element: id: ${output.element.id} direction: DOWN - speed: ${output.speed.slow} \ No newline at end of file + speed: ${output.speed.slow} + timeout: ${output.timeout.slow} \ No newline at end of file