Skip to content

Commit

Permalink
Fix evaluate script bug (#2023)
Browse files Browse the repository at this point in the history
* Fix evaluate script bug

* Fix test

---------

Co-authored-by: Muhammed Furkan Boran <furkan.boran@trendyol.com>
  • Loading branch information
boranfrkn and Muhammed Furkan Boran authored Sep 6, 2024
1 parent e9b4247 commit c045855
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -113,19 +113,25 @@ 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."
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
output.speed = {
slow: 40
}

output.timeout = {
slow: 20
}

output.element = {
id: "maestro"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ appId: com.example.app
element:
id: ${output.element.id}
direction: DOWN
speed: ${output.speed.slow}
speed: ${output.speed.slow}
timeout: ${output.timeout.slow}

0 comments on commit c045855

Please sign in to comment.