Skip to content

Commit

Permalink
Allow env variables in setLocation and travel commands (#1988)
Browse files Browse the repository at this point in the history
* added test for setLocation with negative coordinates

* removed e.printStacktrace() statement

* Update maestro-orchestra/src/test/java/maestro/orchestra/MaestroCommandTest.kt

* Update MaestroCommandTest.kt

* add integration test

Co-authored-by: Prasanta Biswas <[email protected]>
Co-authored-by: Prasanta Biswas <[email protected]>
Co-authored-by: Bartek Pacia <[email protected]>
  • Loading branch information
4 people authored Aug 30, 2024
1 parent 843ae8d commit f9f4cc8
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 29 deletions.
4 changes: 2 additions & 2 deletions maestro-client/src/main/java/maestro/Maestro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,10 @@ class Maestro(
}
}

fun setLocation(latitude: Double, longitude: Double) {
fun setLocation(latitude: String, longitude: String) {
LOGGER.info("Setting location: ($latitude, $longitude)")

driver.setLocation(latitude, longitude)
driver.setLocation(latitude.toDouble(), longitude.toDouble())
}

fun eraseText(charactersToErase: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ data class RunFlowCommand(
}

data class SetLocationCommand(
val latitude: Double,
val longitude: Double,
val latitude: String,
val longitude: String,
val label: String? = null
) : Command {

Expand All @@ -708,7 +708,10 @@ data class SetLocationCommand(
}

override fun evaluateScripts(jsEngine: JsEngine): SetLocationCommand {
return this
return copy(
latitude = latitude.evaluateScripts(jsEngine),
longitude = longitude.evaluateScripts(jsEngine),
)
}
}

Expand Down Expand Up @@ -839,17 +842,23 @@ data class TravelCommand(
) : Command {

data class GeoPoint(
val latitude: Double,
val longitude: Double,
val latitude: String,
val longitude: String,
) {

fun getDistanceInMeters(another: GeoPoint): Double {
val earthRadius = 6371 // in kilometers
val dLat = Math.toRadians(another.latitude - latitude)
val dLon = Math.toRadians(another.longitude - longitude)
val oLat = Math.toRadians(latitude.toDouble())
val oLon = Math.toRadians(longitude.toDouble())

val aLat = Math.toRadians(another.latitude.toDouble())
val aLon = Math.toRadians(another.longitude.toDouble())

val dLat = Math.toRadians(aLat - oLat)
val dLon = Math.toRadians(aLon - oLon)

val a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(another.latitude)) *
Math.cos(Math.toRadians(oLat)) * Math.cos(Math.toRadians(aLat)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)

val c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
Expand All @@ -865,7 +874,9 @@ data class TravelCommand(
}

override fun evaluateScripts(jsEngine: JsEngine): Command {
return this
return copy(
points = points.map { it.copy(latitude = it.latitude.evaluateScripts(jsEngine), longitude = it.longitude.evaluateScripts(jsEngine)) }
)
}

}
Expand Down
16 changes: 11 additions & 5 deletions maestro-orchestra/src/main/java/maestro/orchestra/geo/Traveller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ object Traveller {

val timeToSleep = timeToTravelInMilliseconds / steps

val latitudeStep = (end.latitude - start.latitude) / steps
val longitudeStep = (end.longitude - start.longitude) / steps
val sLat = start.latitude.toDouble()
val sLon = start.longitude.toDouble()

val eLat = end.latitude.toDouble()
val eLon = end.longitude.toDouble()

val latitudeStep = (eLat - sLat) / steps
val longitudeStep = (eLon - sLon) / steps

for (i in 1..steps) {
val latitude = start.latitude + (latitudeStep * i)
val longitude = start.longitude + (longitudeStep * i)
val latitude = sLat + (latitudeStep * i)
val longitude = sLon + (longitudeStep * i)

maestro.setLocation(latitude, longitude)
maestro.setLocation(latitude.toString(), longitude.toString())
Thread.sleep(timeToSleep)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ data class YamlFluentCommand(
val longitude = spitPoint[1].toDoubleOrNull() ?: throw SyntaxError("Invalid travel point longitude: $point")

TravelCommand.GeoPoint(
latitude = latitude,
longitude = longitude,
latitude = latitude.toString(),
longitude = longitude.toString(),
)
},
speedMPS = command.speed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package maestro.orchestra.yaml
import com.fasterxml.jackson.annotation.JsonCreator

data class YamlSetLocation @JsonCreator constructor(
val latitude: Double,
val longitude: Double,
val latitude: String,
val longitude: String,
val label: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class MaestroCommandTest {
@Test
fun `description (with a label)`() {
// given
val maestroCommand = MaestroCommand(SetLocationCommand(12.5266, 78.2150, "Set Location to Test Laboratory"))
val maestroCommand = MaestroCommand(SetLocationCommand("12.5266", "78.2150", "Set Location to Test Laboratory"))

// when
val description = maestroCommand.description()
Expand All @@ -42,4 +42,17 @@ internal class MaestroCommandTest {
assertThat(description)
.isEqualTo("Set Location to Test Laboratory")
}

@Test
fun `description (negative coordinates)`() {
// given
val maestroCommand = MaestroCommand(SetLocationCommand("-12.5266", "-78.2150", "Set location with negative coordinates"))

// when
val description = maestroCommand.description()

// then
assertThat(description)
.isEqualTo("Set location with negative coordinates")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ internal class YamlCommandReaderTest {
centerElement = false
),
SetLocationCommand(
latitude = 12.5266,
longitude = 78.2150,
latitude = "12.5266",
longitude = "78.2150",
label = "Set Location to Test Laboratory"
),
StartRecordingCommand(
Expand All @@ -460,10 +460,10 @@ internal class YamlCommandReaderTest {
),
TravelCommand(
points = listOf(
TravelCommand.GeoPoint(0.0,0.0),
TravelCommand.GeoPoint(0.1,0.0),
TravelCommand.GeoPoint(0.1,0.1),
TravelCommand.GeoPoint(0.0,0.1),
TravelCommand.GeoPoint("0.0","0.0"),
TravelCommand.GeoPoint("0.1","0.0"),
TravelCommand.GeoPoint("0.1","0.1"),
TravelCommand.GeoPoint("0.0","0.1"),
),
speedMPS = 2000.0,
label = "Run around the north pole"
Expand Down Expand Up @@ -604,4 +604,11 @@ internal class YamlCommandReaderTest {

private fun commands(vararg commands: Command): List<MaestroCommand> =
commands.map(::MaestroCommand).toList()

@Test
fun setLocationSyntaxError(
@YamlFile("026_setLocation_syntaxError.yaml") e: SyntaxError,
) {
assertThat(e.message).contains("Cannot deserialize value of type")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
appId: com.example.app
---
setLocation:
latitude: 12.5266
longitude: 78.2150
5 changes: 4 additions & 1 deletion maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ class IntegrationTest {
"NON_EXISTENT_TEXT" to "nonExistentText",
"NON_EXISTENT_ID" to "nonExistentId",
"URL" to "secretUrl",
"LAT" to "37.82778",
"LNG" to "-122.48167",
)
)

Expand Down Expand Up @@ -770,7 +772,8 @@ class IntegrationTest {
Event.Tap(Point(50, 50)),
Event.Tap(Point(50, 50)),
Event.InputText("\${PASSWORD} is testPassword"),
Event.OpenLink("https://example.com/secretUrl")
Event.OpenLink("https://example.com/secretUrl"),
Event.SetLocation(latitude = 37.82778, longitude = -122.48167),
)
)
}
Expand Down
5 changes: 4 additions & 1 deletion maestro-test/src/test/resources/028_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ appId: com.example.app
- assertNotVisible:
id: .*${NON_EXISTENT_ID}.*
- inputText: \${PASSWORD} is ${PASSWORD}
- openLink: https://example.com/${URL}
- openLink: https://example.com/${URL}
- setLocation:
latitude: ${LAT}
longitude: ${LNG}

0 comments on commit f9f4cc8

Please sign in to comment.