Skip to content

Commit

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

---------

Co-authored-by: Muhammed Furkan Boran <furkan.boran@trendyol.com>
  • Loading branch information
boranfrkn and Muhammed Furkan Boran authored Sep 1, 2024
1 parent 3ee902e commit 3ec7cac
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ data class SwipeCommand(
data class ScrollUntilVisibleCommand(
val selector: ElementSelector,
val direction: ScrollDirection,
val scrollDuration: Long,
val scrollDuration: String = DEFAULT_SCROLL_DURATION,
val visibilityPercentage: Int,
val timeout: Long = DEFAULT_TIMEOUT_IN_MILLIS,
val centerElement: Boolean,
Expand All @@ -104,19 +104,24 @@ data class ScrollUntilVisibleCommand(

val visibilityPercentageNormalized = (visibilityPercentage / 100).toDouble()

private fun String.speedToDuration(): String {
return ((1000 * (100 - this.toLong()).toDouble() / 100).toLong() + 1).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()
)
}

companion object {
const val DEFAULT_TIMEOUT_IN_MILLIS = 20 * 1000L
const val DEFAULT_SCROLL_DURATION = 40
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 @@ -500,7 +500,7 @@ class Orchestra(
}
} catch (ignored: MaestroException.ElementNotFound) {
}
maestro.swipeFromCenter(direction, durationMs = command.scrollDuration)
maestro.swipeFromCenter(direction, durationMs = command.scrollDuration.toLong())
} while (System.currentTimeMillis() < endTime)

throw MaestroException.ElementNotFound(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ data class YamlFluentCommand(
selector = toElementSelector(yaml.element),
direction = yaml.direction,
timeout = timeout,
scrollDuration = yaml.speedToDuration(),
scrollDuration = yaml.speed,
visibilityPercentage = visibility,
centerElement = yaml.centerElement,
label = yaml.label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ data class YamlScrollUntilVisible(
val direction: ScrollDirection = ScrollDirection.DOWN,
val element: YamlElementSelectorUnion,
val timeout: Long = ScrollUntilVisibleCommand.DEFAULT_TIMEOUT_IN_MILLIS,
val speed: Int = ScrollUntilVisibleCommand.DEFAULT_SCROLL_DURATION,
val speed: String = ScrollUntilVisibleCommand.DEFAULT_SCROLL_DURATION,
val visibilityPercentage: Int = ScrollUntilVisibleCommand.DEFAULT_ELEMENT_VISIBILITY_PERCENTAGE,
val centerElement: Boolean = ScrollUntilVisibleCommand.DEFAULT_CENTER_ELEMENT,
val label: String? = null
) {
fun speedToDuration(): Long {
return (1000 * (100 - speed).toDouble() / 100).toLong() + 1
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ internal class YamlCommandReaderTest {
selector = ElementSelector(textRegex = "Footer"),
direction = ScrollDirection.DOWN,
timeout = 20000,
scrollDuration = 601,
scrollDuration = "40",
visibilityPercentage = 100,
label = "Scroll to the bottom",
centerElement = false
Expand Down
58 changes: 46 additions & 12 deletions maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2537,15 +2537,17 @@ class IntegrationTest {
}

// Then
driver.assertHasEvent(Event.LaunchApp(
appId = "com.example.app",
launchArguments = mapOf(
"argumentA" to true,
"argumentB" to 4,
"argumentC" to 4.0,
"argumentD" to "Hello String Value true"
driver.assertHasEvent(
Event.LaunchApp(
appId = "com.example.app",
launchArguments = mapOf(
"argumentA" to true,
"argumentB" to 4,
"argumentC" to 4.0,
"argumentD" to "Hello String Value true"
)
)
))
)
}

@Test
Expand Down Expand Up @@ -2931,7 +2933,7 @@ class IntegrationTest {
fun `Case 110 - addMedia command emits add media event with correct path`() {
// given
val commands = readCommands("110_add_media_device")
val driver = driver {}
val driver = driver {}

// when
Maestro(driver).use {
Expand All @@ -2946,7 +2948,7 @@ class IntegrationTest {
fun `Case 111 - addMedia command allows adding multiple media`() {
// given
val commands = readCommands("111_add_multiple_media")
val driver = driver { }
val driver = driver { }

// when
Maestro(driver).use {
Expand Down Expand Up @@ -3061,15 +3063,15 @@ class IntegrationTest {
}

@Test
fun `Case 115 - airplane mode`() {
fun `Case 115 - airplane mode`() {
val commands = readCommands("115_airplane_mode")
val driver = driver { }

Maestro(driver).use {
orchestra(it).runFlow(commands)
}
}

@Test
fun `Case 116 - Kill app`() {
// Given
Expand All @@ -3089,6 +3091,38 @@ class IntegrationTest {
driver.assertHasEvent(Event.KillApp("another.app"))
}

@Test
fun `Case 117 - Scroll until view is visible - with speed evaluate`() {
// Given
val commands = readCommands("117_scroll_until_visible_speed")
val expectedDuration = "601"
val info = driver { }.deviceInfo()

val elementBounds = Bounds(0, 0 + info.heightGrid, 100, 100 + info.heightGrid)
val driver = driver {
element {
id = "maestro"
bounds = elementBounds
}
}

// When
var scrollDuration = "0"
Maestro(driver).use {
orchestra(it, onCommandMetadataUpdate = { _, metaData ->
scrollDuration = metaData.evaluatedCommand?.scrollUntilVisible?.scrollDuration.toString()
}).runFlow(commands)
}

// Then
assertThat(scrollDuration).isEqualTo(expectedDuration)
driver.assertEvents(
listOf(
Event.SwipeElementWithDirection(Point(270, 480), SwipeDirection.UP, expectedDuration.toLong()),
)
)
}

private fun orchestra(
maestro: Maestro,
) = Orchestra(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
output.speed = {
slow: 40
}
output.element = {
id: "maestro"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
appId: com.example.app
---
- runScript: 117_scroll_until_visible_speed.js
- scrollUntilVisible:
element:
id: ${output.element.id}
direction: DOWN
speed: ${output.speed.slow}

0 comments on commit 3ec7cac

Please sign in to comment.