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

Add filename to env #1945

Merged
merged 11 commits into from
Sep 2, 2024
5 changes: 3 additions & 2 deletions maestro-cli/src/main/java/maestro/cli/runner/TestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory
import java.io.File
import java.nio.file.Path
import kotlin.concurrent.thread
import maestro.orchestra.util.Env.withDefaultEnvVars

/**
* Knows how to run a single Maestro flow (either one-shot or continuously).
Expand Down Expand Up @@ -52,7 +53,7 @@ object TestRunner {

val result = runCatching(resultView, maestro) {
val commands = YamlCommandReader.readCommands(flowFile.toPath())
.withEnv(env)
.withEnv(env.withDefaultEnvVars(flowFile.nameWithoutExtension))

YamlCommandReader.getConfig(commands)?.name?.let {
aiOutput = aiOutput.copy(flowName = it)
Expand Down Expand Up @@ -108,7 +109,7 @@ object TestRunner {

val commands = YamlCommandReader
.readCommands(flowFile.toPath())
.withEnv(env)
.withEnv(env.withDefaultEnvVars(flowFile.nameWithoutExtension))

// Restart the flow if anything has changed
if (commands != previousCommands) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import java.io.File
import java.nio.file.Path
import kotlin.system.measureTimeMillis
import kotlin.time.Duration.Companion.seconds
import maestro.orchestra.util.Env.withDefaultEnvVars

/**
* Similar to [TestRunner], but:
Expand Down Expand Up @@ -191,7 +192,7 @@ class TestSuiteInteractor(
try {
val commands = YamlCommandReader
.readCommands(flowFile.toPath())
.withEnv(env)
.withEnv(env.withDefaultEnvVars(flowFile.nameWithoutExtension))

YamlCommandReader.getConfig(commands)?.name?.let { flowName = it }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ object Env {

return mutable
}

fun Map<String, String>.withDefaultEnvVars(fileName: String? = null) =
fileName?.let { this + mapOf("MAESTRO_FILENAME" to fileName) } ?: this
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import java.nio.file.Path
import kotlin.io.path.absolute
import kotlin.io.path.absolutePathString
import kotlin.io.path.isDirectory
import kotlin.io.path.nameWithoutExtension
import kotlin.io.path.readText
import maestro.orchestra.ApplyConfigurationCommand
import maestro.orchestra.MaestroCommand
import maestro.orchestra.MaestroConfig
import maestro.orchestra.WorkspaceConfig
import maestro.orchestra.error.SyntaxError
import maestro.orchestra.util.Env.withDefaultEnvVars
import maestro.orchestra.util.Env.withEnv

object YamlCommandReader {
Expand Down Expand Up @@ -114,7 +116,7 @@ object YamlCommandReader {
val commands = parser.readValueAs<List<YamlFluentCommand>>(
object : TypeReference<List<YamlFluentCommand>>() {}
)
return config to commands
return config.copy(env = config.env) to commands
}

private fun <T> mapParsingErrors(path: Path, block: () -> T): T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ data class YamlConfig(
val env: Map<String, String> = emptyMap(),
val onFlowStart: YamlOnFlowStart?,
val onFlowComplete: YamlOnFlowComplete?,
private val ext: MutableMap<String, Any?> = mutableMapOf<String, Any?>()
) {

private val ext = mutableMapOf<String, Any?>()

@JsonAnySetter
fun setOtherField(key: String, other: Any?) {
ext[key] = other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import maestro.orchestra.ClearStateCommand
import maestro.orchestra.Command
import maestro.orchestra.Condition
import maestro.orchestra.CopyTextFromCommand
import maestro.orchestra.DefineVariablesCommand
import maestro.orchestra.ElementSelector
import maestro.orchestra.EraseTextCommand
import maestro.orchestra.EvalScriptCommand
Expand Down
82 changes: 46 additions & 36 deletions maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import maestro.MaestroException
import maestro.Point
import maestro.SwipeDirection
import maestro.orchestra.ApplyConfigurationCommand
import maestro.orchestra.DefineVariablesCommand
import maestro.orchestra.LaunchAppCommand
import maestro.orchestra.MaestroCommand
import maestro.orchestra.MaestroConfig
Expand All @@ -29,6 +30,7 @@ import java.io.File
import java.nio.file.Paths
import maestro.orchestra.error.SyntaxError
import kotlin.system.measureTimeMillis
import maestro.orchestra.util.Env.withDefaultEnvVars

class IntegrationTest {

Expand All @@ -41,8 +43,9 @@ class IntegrationTest {

@AfterEach
internal fun tearDown() {
File("screenshot.png").delete()
File("recording.mp4").delete()
File("041_take_screenshot_with_filename.png").delete()
File("099_screen_recording.mp4").delete()
File("028_env.mp4").delete()
}

@Test
Expand Down Expand Up @@ -551,6 +554,11 @@ class IntegrationTest {
// Then
assertThat(commands).isEqualTo(
listOf(
MaestroCommand(
DefineVariablesCommand(
env = mapOf("MAESTRO_FILENAME" to "020_parse_config")
)
),
MaestroCommand(
ApplyConfigurationCommand(
config = MaestroConfig(
Expand Down Expand Up @@ -733,20 +741,19 @@ class IntegrationTest {
@Test
fun `Case 028 - Env`() {
// Given
val commands = readCommands("028_env")
.withEnv(
mapOf(
"APP_ID" to "com.example.app",
"BUTTON_ID" to "button_id",
"BUTTON_TEXT" to "button_text",
"PASSWORD" to "testPassword",
"NON_EXISTENT_TEXT" to "nonExistentText",
"NON_EXISTENT_ID" to "nonExistentId",
"URL" to "secretUrl",
"LAT" to "37.82778",
"LNG" to "-122.48167",
)
val commands = readCommands("028_env") {
mapOf(
"APP_ID" to "com.example.app",
"BUTTON_ID" to "button_id",
"BUTTON_TEXT" to "button_text",
"PASSWORD" to "testPassword",
"NON_EXISTENT_TEXT" to "nonExistentText",
"NON_EXISTENT_ID" to "nonExistentId",
"URL" to "secretUrl",
"LAT" to "37.82778",
"LNG" to "-122.48167",
)
}

val driver = driver {

Expand Down Expand Up @@ -774,8 +781,10 @@ class IntegrationTest {
Event.InputText("\${PASSWORD} is testPassword"),
Event.OpenLink("https://example.com/secretUrl"),
Event.SetLocation(latitude = 37.82778, longitude = -122.48167),
Event.StartRecording,
)
)
assert(File("028_env.mp4").exists())
}

@Test
Expand Down Expand Up @@ -1112,6 +1121,7 @@ class IntegrationTest {
Event.TakeScreenshot,
)
)
assert(File("041_take_screenshot_with_filename.png").exists())
}

@Test
Expand Down Expand Up @@ -1322,12 +1332,11 @@ class IntegrationTest {
@Test
fun `Case 049 - Run flow conditionally`() {
// Given
val commands = readCommands("049_run_flow_conditionally")
.withEnv(
mapOf(
"NOT_CLICKED" to "Not Clicked"
)
val commands = readCommands("049_run_flow_conditionally") {
mapOf(
"NOT_CLICKED" to "Not Clicked"
)
}

val driver = driver {
val indicator = element {
Expand Down Expand Up @@ -1546,12 +1555,11 @@ class IntegrationTest {
@Test
fun `Case 057 - Pass inner env variables to runFlow`() {
// Given
val commands = readCommands("057_runFlow_env")
.withEnv(
mapOf(
"OUTER_ENV" to "Outer Parameter"
)
val commands = readCommands("057_runFlow_env") {
mapOf(
"OUTER_ENV" to "Outer Parameter"
)
}

val driver = driver {
}
Expand Down Expand Up @@ -1605,12 +1613,11 @@ class IntegrationTest {
@Test
fun `Case 060 - Pass env param to an env param`() {
// given
val commands = readCommands("060_pass_env_to_env")
.withEnv(
mapOf(
"PARAM" to "Value"
)
val commands = readCommands("060_pass_env_to_env") {
mapOf(
"PARAM" to "Value"
)
}
val driver = driver { }

// when
Expand Down Expand Up @@ -1710,8 +1717,10 @@ class IntegrationTest {
Event.InputText("Sub"),
Event.InputText("Main"),
Event.InputText("Sub"),
Event.InputText("064_js_files"),
Event.InputText("Hello, Input Parameter!"),
Event.InputText("Hello, Evaluated Parameter!"),
Event.InputText("064_js_files"),
)
)
}
Expand Down Expand Up @@ -2036,12 +2045,11 @@ class IntegrationTest {
@Test
fun `Case 077 - Env special characters`() {
// Given
val commands = readCommands("077_env_special_characters")
.withEnv(
mapOf(
"OUTER" to "!@#\$&*()_+{}|:\"<>?[]\\\\;',./"
)
val commands = readCommands("077_env_special_characters") {
mapOf(
"OUTER" to "!@#\$&*()_+{}|:\"<>?[]\\\\;',./"
)
}

val driver = driver {
// No elements
Expand Down Expand Up @@ -2662,6 +2670,7 @@ class IntegrationTest {
Event.StopRecording,
)
)
assert(File("099_screen_recording.mp4").exists())
}

@Test
Expand Down Expand Up @@ -3124,10 +3133,11 @@ class IntegrationTest {
return driver
}

private fun readCommands(caseName: String): List<MaestroCommand> {
private fun readCommands(caseName: String, withEnv: () -> Map<String, String> = { emptyMap() }): List<MaestroCommand> {
val resource = javaClass.classLoader.getResource("$caseName.yaml")
?: throw IllegalArgumentException("File $caseName.yaml not found")
return YamlCommandReader.readCommands(Paths.get(resource.toURI()))
.withEnv(withEnv().withDefaultEnvVars(caseName))
}

}
1 change: 1 addition & 0 deletions maestro-test/src/test/resources/028_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ appId: com.example.app
- setLocation:
latitude: ${LAT}
longitude: ${LNG}
- startRecording: ${MAESTRO_FILENAME}
2 changes: 1 addition & 1 deletion maestro-test/src/test/resources/041_take_screenshot.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
appId: com.example.app
---
- takeScreenshot: screenshot
- takeScreenshot: ${MAESTRO_FILENAME}_with_filename
4 changes: 3 additions & 1 deletion maestro-test/src/test/resources/064_js_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ appId: com.example.app
- inputText: ${output.sharedResult}
- inputText: ${output.mainFlow.result}
- inputText: ${output.subFlow.result}
- inputText: ${output.subFlowFileName}
- runScript:
file: 064_script_with_args.js
env:
Expand All @@ -15,4 +16,5 @@ appId: com.example.app
file: 064_script_with_args.js
env:
parameter: ${'Evaluated Parameter'}
- inputText: ${output.resultWithParameters}
- inputText: ${output.resultWithParameters}
- inputText: ${output.fileName}
1 change: 1 addition & 0 deletions maestro-test/src/test/resources/064_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ output.sharedResult = 'Main'
output.mainFlow = {
result: 'Main'
}
output.fileName = MAESTRO_FILENAME
1 change: 1 addition & 0 deletions maestro-test/src/test/resources/064_script_alt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ output.sharedResult = 'Sub'
output.subFlow = {
result: 'Sub'
}
output.subFlowFileName = MAESTRO_FILENAME
2 changes: 1 addition & 1 deletion maestro-test/src/test/resources/099_screen_recording.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
appId: com.other.app
---
- startRecording: recording
- startRecording: ${MAESTRO_FILENAME}
- stopRecording
Loading