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 assertVisual command #1898

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,24 @@ data class AssertConditionCommand(
}
}

data class AssertVisualCommand(
val baseline: String,
val thresholdPercentage: Int, // from 1 to 100
val optional: Boolean = false, /// If true, the command will not fail the flow, but print a warning
val label: String? = null,
) : Command {
override fun description(): String {
return label ?: "Assert visual difference with baseline $baseline (threshold: $thresholdPercentage%)"
}

override fun evaluateScripts(jsEngine: JsEngine): Command {
return copy(
baseline = baseline.evaluateScripts(jsEngine)
// TODO: Allow for evaluating script for more properties
)
}
}

data class InputTextCommand(
val text: String,
val label: String? = null,
Expand Down Expand Up @@ -920,6 +938,7 @@ data class ToggleAirplaneModeCommand(
}
}


internal fun tapOnDescription(isLongPress: Boolean?, repeat: TapRepeat?): String {
return if (isLongPress == true) "Long press"
else if (repeat != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ data class MaestroCommand(
val backPressCommand: BackPressCommand? = null,
@Deprecated("Use assertConditionCommand") val assertCommand: AssertCommand? = null,
val assertConditionCommand: AssertConditionCommand? = null,
val assertVisualCommand: AssertVisualCommand? = null,
val inputTextCommand: InputTextCommand? = null,
val inputRandomTextCommand: InputRandomCommand? = null,
val launchAppCommand: LaunchAppCommand? = null,
Expand Down Expand Up @@ -75,6 +76,7 @@ data class MaestroCommand(
swipeCommand = command as? SwipeCommand,
backPressCommand = command as? BackPressCommand,
assertCommand = command as? AssertCommand,
assertVisualCommand = command as? AssertVisualCommand,
assertConditionCommand = command as? AssertConditionCommand,
inputTextCommand = command as? InputTextCommand,
inputRandomTextCommand = command as? InputRandomCommand,
Expand Down Expand Up @@ -116,6 +118,7 @@ data class MaestroCommand(
backPressCommand != null -> backPressCommand
assertCommand != null -> assertCommand
assertConditionCommand != null -> assertConditionCommand
assertVisualCommand != null -> assertVisualCommand
inputTextCommand != null -> inputTextCommand
inputRandomTextCommand != null -> inputRandomTextCommand
launchAppCommand != null -> launchAppCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import maestro.orchestra.MaestroCommand
object Env {

fun String.evaluateScripts(jsEngine: JsEngine): String {
// Look for strings starting with ${ and ending with }
val result = "(?<!\\\\)\\\$\\{([^\$]*)}".toRegex()
.replace(this) { match ->
val script = match.groups[1]?.value ?: ""
Expand Down
30 changes: 30 additions & 0 deletions maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package maestro.orchestra

import com.github.romankh3.image.comparison.ImageComparison
import maestro.*
import maestro.Filters.asFilter
import maestro.js.GraalJsEngine
Expand Down Expand Up @@ -256,6 +257,7 @@ class Orchestra(
is SwipeCommand -> swipeCommand(command)
is AssertCommand -> assertCommand(command)
is AssertConditionCommand -> assertConditionCommand(command)
is AssertVisualCommand -> assertVisualCommand(command)
is InputTextCommand -> inputTextCommand(command)
is InputRandomCommand -> inputTextRandomCommand(command)
is LaunchAppCommand -> launchAppCommand(command)
Expand Down Expand Up @@ -334,6 +336,34 @@ class Orchestra(
return false
}

private fun assertVisualCommand(command: AssertVisualCommand): Boolean {
val baseline = command.baseline
val thresholdPercentage = command.thresholdPercentage

val file = screenshotsDir
?.let { File(it, baseline) }
?: File("actual_screenshots")

val actual = maestro.takeScreenshot()

val imageDiff = ImageComparison(
startScreenshot,
endScreenshot
).compareImages().differencePercent

maestro.takeScreenshot(file, false)

if (!actual.matches(expected)) {
throw MaestroException.VisualAssertionFailure(
"Visual assertion failed: ${command.selector.description()}",
expected,
actual,
)
}

return false
}

private fun isOptional(condition: Condition): Boolean {
return condition.visible?.optional == true
|| condition.notVisible?.optional == true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package maestro.orchestra.yaml

import com.fasterxml.jackson.annotation.JsonCreator

private const val DEFAULT_DIFF_THRESHOLD = 95

data class YamlAssertVisual(
val baseline: String,
val thresholdPercentage: Int = DEFAULT_DIFF_THRESHOLD,
val optional: Boolean = false,
val label: String? = null,
) {

companion object {

// TODO(bartek): This might be needed if single value is passed in YAML
// @JvmStatic
// @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
// fun parse(appId: String): YamlLaunchApp {
// return YamlLaunchApp(
// appId = appId,
// clearState = null,
// clearKeychain = null,
// stopApp = null,
// permissions = null,
// arguments = null,
// )
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ data class YamlFluentCommand(
val assertVisible: YamlElementSelectorUnion? = null,
val assertNotVisible: YamlElementSelectorUnion? = null,
val assertTrue: YamlAssertTrue? = null,
val assertVisual: YamlAssertVisual? = null,
val back: YamlActionBack? = null,
val clearKeychain: YamlActionClearKeychain? = null,
val hideKeyboard: YamlActionHideKeyboard? = null,
Expand Down Expand Up @@ -115,6 +116,16 @@ data class YamlFluentCommand(
)
)
)
assertVisual != null -> listOf(
MaestroCommand(
AssertVisualCommand(
baseline = assertVisual.baseline,
thresholdPercentage = assertVisual.thresholdPercentage,
optional = assertVisual.optional,
label = assertVisual.label
)
)
)
addMedia != null -> listOf(
MaestroCommand(
addMediaCommand = addMediaCommand(addMedia, flowPath)
Expand Down
Loading