From 9b239673e080068708623b146bf3a85f2d3b9c33 Mon Sep 17 00:00:00 2001 From: Amanjeet Singh Date: Mon, 30 Sep 2024 19:26:05 +0530 Subject: [PATCH] fix: optional commands in subFlow (#2071) --- .../main/java/maestro/orchestra/Commands.kt | 6 ++++-- .../main/java/maestro/orchestra/Orchestra.kt | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/maestro-orchestra-models/src/main/java/maestro/orchestra/Commands.kt b/maestro-orchestra-models/src/main/java/maestro/orchestra/Commands.kt index eed7c3d390..1cc4b1f244 100644 --- a/maestro-orchestra-models/src/main/java/maestro/orchestra/Commands.kt +++ b/maestro-orchestra-models/src/main/java/maestro/orchestra/Commands.kt @@ -265,7 +265,8 @@ data class TapOnElementCommand( ) : Command { override fun description(): String { - return label ?: "${tapOnDescription(longPress, repeat)} on ${selector.description()}" + val optional = if (optional) "(Optional) " else "" + return label ?: "${tapOnDescription(longPress, repeat)} on $optional${selector.description()}" } override fun evaluateScripts(jsEngine: JsEngine): TapOnElementCommand { @@ -379,7 +380,8 @@ data class AssertConditionCommand( } override fun description(): String { - return label ?: "Assert that ${condition.description()}" + val optional = if (optional) "(Optional) " else "" + return label ?: "Assert that $optional${condition.description()}" } override fun evaluateScripts(jsEngine: JsEngine): Command { diff --git a/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt b/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt index 33b68efaae..70d233e20e 100644 --- a/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt +++ b/maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt @@ -678,10 +678,21 @@ class Orchestra( updateMetadata(command, metadata) return@mapIndexed try { - executeCommand(evaluatedCommand, config) - .also { - onCommandComplete(index, command) - } + try { + executeCommand(evaluatedCommand, config) + .also { + onCommandComplete(index, command) + } + } catch (exception: MaestroException) { + val isOptional = command.asCommand()?.optional == true + if (isOptional) throw CommandWarned(exception.message) + else throw exception + } + } catch (ignored: CommandWarned) { + // Swallow exception, but add a warning as an insight + Insights.report(Insight(message = ignored.message, level = Insight.Level.WARNING)) + onCommandWarned(index, command) + false } catch (ignored: CommandSkipped) { // Swallow exception onCommandSkipped(index, command)