Skip to content

Commit 5082bcd

Browse files
authored
Ensure recordings and screenshots create relative directories when specified (mobile-dev-inc#2863)
1 parent e575a83 commit 5082bcd

File tree

4 files changed

+83
-10
lines changed

4 files changed

+83
-10
lines changed

maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import maestro.utils.NoopInsights
5353
import maestro.utils.StringUtils.toRegexSafe
5454
import okhttp3.OkHttpClient
5555
import okio.Buffer
56+
import okio.BufferedSink
5657
import okio.Sink
5758
import okio.buffer
5859
import okio.sink
@@ -940,21 +941,15 @@ class Orchestra(
940941

941942
private fun takeScreenshotCommand(command: TakeScreenshotCommand): Boolean {
942943
val pathStr = command.path + ".png"
943-
val file = screenshotsDir
944-
?.let { it.resolve(pathStr).toFile() }
945-
?: File(pathStr)
946-
947-
maestro.takeScreenshot(file, false)
948-
944+
val fileSink = getFileSink(screenshotsDir, pathStr)
945+
maestro.takeScreenshot(fileSink, false)
949946
return false
950947
}
951948

952949
private fun startRecordingCommand(command: StartRecordingCommand): Boolean {
953950
val pathStr = command.path + ".mp4"
954-
val file = screenshotsDir
955-
?.let { it.resolve(pathStr).toFile() }
956-
?: File(pathStr)
957-
screenRecording = maestro.startScreenRecording(file.sink().buffer())
951+
val fileSink = getFileSink(screenshotsDir, pathStr)
952+
screenRecording = maestro.startScreenRecording(fileSink)
958953
return false
959954
}
960955

@@ -1473,6 +1468,22 @@ class Orchestra(
14731468
return true
14741469
}
14751470

1471+
private fun getFileSink(parentPath: Path?, filePathStr: String): BufferedSink {
1472+
// Work out relative v absolute input
1473+
val resolvedFile = parentPath?.resolve(filePathStr)?.toFile() ?: File(filePathStr)
1474+
val absoluteFile = resolvedFile.absoluteFile
1475+
1476+
if(absoluteFile.parentFile.exists() || absoluteFile.parentFile.mkdirs()) {
1477+
return resolvedFile
1478+
.sink()
1479+
.buffer()
1480+
} else {
1481+
throw MaestroException.DestinationIsNotWritable(
1482+
"Unable to create directory for file: ${absoluteFile.parentFile.absolutePath}"
1483+
)
1484+
}
1485+
}
1486+
14761487
private suspend fun executeDefineVariablesCommands(commands: List<MaestroCommand>, config: MaestroConfig?) {
14771488
commands.filter { it.asCommand() is DefineVariablesCommand }.takeIf { it.isNotEmpty() }?.let {
14781489
executeCommands(

maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class IntegrationTest {
5858
@AfterEach
5959
internal fun tearDown() {
6060
File("041_take_screenshot_with_filename.png").delete()
61+
File("134_screenshots/filename.png").delete()
62+
File("134_screenshots").delete()
63+
File("135_recordings/filename.mp4").delete()
64+
File("135_recordings").delete()
6165
File("099_screen_recording.mp4").delete()
6266
File("028_env.mp4").delete()
6367
}
@@ -4148,6 +4152,57 @@ class IntegrationTest {
41484152
)
41494153
}
41504154

4155+
@Test
4156+
fun `Case 134 - Take screenshot with path`() {
4157+
// Given
4158+
val commands = readCommands("134_take_screenshot_with_path")
4159+
4160+
val driver = driver {
4161+
}
4162+
4163+
// When
4164+
Maestro(driver).use {
4165+
runBlocking {
4166+
orchestra(it).runFlow(commands)
4167+
}
4168+
}
4169+
4170+
// Then
4171+
// No test failure
4172+
driver.assertEvents(
4173+
listOf(
4174+
Event.TakeScreenshot,
4175+
)
4176+
)
4177+
assert(File("134_screenshots/filename.png").exists())
4178+
}
4179+
4180+
@Test
4181+
fun `Case 135 - Screen recording with path`() {
4182+
// Given
4183+
val commands = readCommands("135_screen_recording_with_path")
4184+
4185+
val driver = driver {
4186+
}
4187+
4188+
// When
4189+
Maestro(driver).use {
4190+
runBlocking {
4191+
orchestra(it).runFlow(commands)
4192+
}
4193+
}
4194+
4195+
// Then
4196+
// No test failure
4197+
driver.assertEvents(
4198+
listOf(
4199+
Event.StartRecording,
4200+
Event.StopRecording,
4201+
)
4202+
)
4203+
assert(File("135_recordings/filename.mp4").exists())
4204+
}
4205+
41514206
private fun orchestra(
41524207
maestro: Maestro,
41534208
) = Orchestra(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
appId: com.example.app
2+
---
3+
- takeScreenshot: '134_screenshots/filename'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
appId: com.other.app
2+
---
3+
- startRecording: '135_recordings/filename'
4+
- stopRecording

0 commit comments

Comments
 (0)