Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow env values in setLocation command #1769

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -545,10 +545,10 @@ class Maestro(private val driver: Driver) : AutoCloseable {
}
}

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 @@ -667,8 +667,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 @@ -677,7 +677,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 @@ -808,17 +811,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 @@ -834,7 +843,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 @@ -300,8 +300,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"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a test using negative coordinates ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Alaksion done. Please review.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!


// 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 @@ -481,8 +481,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 @@ -502,10 +502,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 @@ -646,4 +646,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
Loading