Skip to content

Commit

Permalink
Revert "feat: added element bounds to matadata, with interactive comm…
Browse files Browse the repository at this point in the history
…ands (#2166)"

This reverts commit ece1d63.
  • Loading branch information
amanjeetsingh150 committed Jan 9, 2025
1 parent fb99638 commit ca0472a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package maestro.orchestra

import maestro.Bounds
import maestro.js.JsEngine

/**
Expand Down
151 changes: 20 additions & 131 deletions maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import maestro.utils.Insights
import maestro.utils.MaestroTimer
import maestro.utils.NoopInsights
import maestro.utils.StringUtils.toRegexSafe
import maestro_android.deviceInfo
import okhttp3.OkHttpClient
import okio.Buffer
import okio.Sink
Expand Down Expand Up @@ -257,22 +256,21 @@ class Orchestra(
is TapOnElementCommand -> {
tapOnElement(
command = command,
maestroCommand = maestroCommand,
retryIfNoChange = command.retryIfNoChange ?: true,
waitUntilVisible = command.waitUntilVisible ?: false,
config = config,
config = config
)
}

is TapOnPointCommand -> tapOnPoint(command, command.retryIfNoChange ?: true)
is TapOnPointV2Command -> tapOnPointV2Command(command, maestroCommand)
is TapOnPointV2Command -> tapOnPointV2Command(command)
is BackPressCommand -> backPressCommand()
is HideKeyboardCommand -> hideKeyboardCommand()
is ScrollCommand -> scrollVerticalCommand()
is CopyTextFromCommand -> copyTextFromCommand(command, maestroCommand)
is ScrollUntilVisibleCommand -> scrollUntilVisible(command, maestroCommand)
is CopyTextFromCommand -> copyTextFromCommand(command)
is ScrollUntilVisibleCommand -> scrollUntilVisible(command)
is PasteTextCommand -> pasteText()
is SwipeCommand -> swipeCommand(command, maestroCommand)
is SwipeCommand -> swipeCommand(command)
is AssertCommand -> assertCommand(command)
is AssertConditionCommand -> assertConditionCommand(command)
is AssertNoDefectsWithAICommand -> assertNoDefectsWithAICommand(command)
Expand Down Expand Up @@ -504,7 +502,7 @@ class Orchestra(
return true
}

private fun scrollUntilVisible(command: ScrollUntilVisibleCommand, maestroCommand: MaestroCommand): Boolean {
private fun scrollUntilVisible(command: ScrollUntilVisibleCommand): Boolean {
val endTime = System.currentTimeMillis() + command.timeout.toLong()
val direction = command.direction.toSwipeDirection()
val deviceInfo = maestro.deviceInfo()
Expand All @@ -528,21 +526,7 @@ class Orchestra(
}
} catch (ignored: MaestroException.ElementNotFound) {
}
val oldMetadata = getMetadata(maestroCommand);
val metadata = oldMetadata.copy(
action = Action.MultipleSwipePoint(
direction = direction,
points = (oldMetadata.action as? Action.MultipleSwipePoint)?.points?.toMutableList()?.apply {
add(Point(deviceInfo.widthGrid / 2, deviceInfo.heightGrid / 2))
} ?: listOf(Point(deviceInfo.widthGrid / 2, deviceInfo.heightGrid / 2))
),
)
updateMetadata(maestroCommand, metadata);
maestro.swipeFromCenter(
direction,
durationMs = command.scrollDuration.toLong(),
waitToSettleTimeoutMs = command.waitToSettleTimeoutMs
)
maestro.swipeFromCenter(direction, durationMs = command.scrollDuration.toLong(), waitToSettleTimeoutMs = command.waitToSettleTimeoutMs)
} while (System.currentTimeMillis() < endTime)

throw MaestroException.ElementNotFound(
Expand Down Expand Up @@ -900,6 +884,7 @@ class Orchestra(
throw UnicodeNotSupportedError(command.text)
}
}

maestro.inputText(command.text)

return true
Expand All @@ -919,16 +904,11 @@ class Orchestra(

private fun tapOnElement(
command: TapOnElementCommand,
maestroCommand: MaestroCommand,
retryIfNoChange: Boolean,
waitUntilVisible: Boolean,
config: MaestroConfig?,
): Boolean {
val result = findElement(command.selector, optional = command.optional)
val metadata = getMetadata(maestroCommand).copy(
action = Action.TapPoint(Point(x = result.element.bounds.center().x, y = result.element.bounds.center().y)),
)
updateMetadata(maestroCommand, metadata);

maestro.tap(
element = result.element,
Expand All @@ -955,15 +935,14 @@ class Orchestra(
longPress = command.longPress ?: false,
tapRepeat = command.repeat,
)

return true
}

private fun tapOnPointV2Command(
command: TapOnPointV2Command,
maestroCommand: MaestroCommand,
): Boolean {
val point = command.point
val deviceInfo = maestro.deviceInfo()

if (point.contains("%")) {
val (percentX, percentY) = point
Expand All @@ -975,15 +954,6 @@ class Orchestra(
throw MaestroException.InvalidCommand("Invalid point: $point")
}

val metadata = getMetadata(maestroCommand).copy(
action = Action.TapPoint(
Point(
deviceInfo.widthGrid * percentX / 100,
deviceInfo.heightGrid * percentY / 100
)
),
)
updateMetadata(maestroCommand, metadata);
maestro.tapOnRelative(
percentX = percentX,
percentY = percentY,
Expand All @@ -998,10 +968,6 @@ class Orchestra(
it.trim().toInt()
}

val metadata = getMetadata(maestroCommand).copy(
action = Action.TapPoint(Point(x, y)),
)
updateMetadata(maestroCommand, metadata);
maestro.tap(
x = x,
y = y,
Expand Down Expand Up @@ -1209,8 +1175,7 @@ class Orchestra(
)
}

private fun swipeCommand(command: SwipeCommand, maestroCommand: MaestroCommand): Boolean {
val deviceInfo = maestro.deviceInfo()
private fun swipeCommand(command: SwipeCommand): Boolean {
val elementSelector = command.elementSelector
val direction = command.direction
val startRelative = command.startRelative
Expand All @@ -1220,70 +1185,20 @@ class Orchestra(
when {
elementSelector != null && direction != null -> {
val uiElement = findElement(elementSelector, optional = command.optional)
val metadata = getMetadata(maestroCommand).copy(
action = Action.SwipePoint.WithDirection(
direction = direction,
startPoint = Point(uiElement.element.bounds.center().x, uiElement.element.bounds.center().y),
),
)
updateMetadata(maestroCommand, metadata);
maestro.swipe(
direction,
uiElement.element,
command.duration,
waitToSettleTimeoutMs = command.waitToSettleTimeoutMs
)
maestro.swipe(direction, uiElement.element, command.duration, waitToSettleTimeoutMs = command.waitToSettleTimeoutMs)
}

startRelative != null && endRelative != null -> {
val startPoints = startRelative.replace("%", "")
.split(",").map { it.trim().toInt() }
val endPoint = endRelative.replace("%", "")
.split(",").map { it.trim().toInt() }
val metadata = getMetadata(maestroCommand).copy(
action = Action.SwipePoint.WithEndPoint(
startPoint = Point(
startPoints[0] * deviceInfo.widthGrid / 100,
startPoints[1] * deviceInfo.widthGrid / 100
),
endPoint = Point(
endPoint[0] * deviceInfo.widthGrid / 100,
endPoint[1] * deviceInfo.widthGrid / 100
),
),
)
updateMetadata(maestroCommand, metadata);
maestro.swipe(
startRelative = startRelative,
endRelative = endRelative,
duration = command.duration,
waitToSettleTimeoutMs = command.waitToSettleTimeoutMs
)
maestro.swipe(startRelative = startRelative, endRelative = endRelative, duration = command.duration, waitToSettleTimeoutMs = command.waitToSettleTimeoutMs)
}

direction != null -> {
val metadata = getMetadata(maestroCommand).copy(
action = Action.SwipePoint.WithDirection(
direction = direction,
startPoint = Point(deviceInfo.widthGrid / 2, deviceInfo.heightGrid / 2),
),
)
updateMetadata(maestroCommand, metadata);
maestro.swipe(
swipeDirection = direction,
duration = command.duration,
waitToSettleTimeoutMs = command.waitToSettleTimeoutMs
)
}

start != null && end != null -> {
maestro.swipe(
startPoint = start,
endPoint = end,
duration = command.duration,
waitToSettleTimeoutMs = command.waitToSettleTimeoutMs
)
}
direction != null -> maestro.swipe(swipeDirection = direction, duration = command.duration, waitToSettleTimeoutMs = command.waitToSettleTimeoutMs)
start != null && end != null -> maestro.swipe(
startPoint = start,
endPoint = end,
duration = command.duration,
waitToSettleTimeoutMs = command.waitToSettleTimeoutMs
)

else -> error("Illegal arguments for swiping")
}
Expand All @@ -1295,12 +1210,8 @@ class Orchestra(
timeMs - (System.currentTimeMillis() - timeMsOfLastInteraction),
)

private fun copyTextFromCommand(command: CopyTextFromCommand, maestroCommand: MaestroCommand): Boolean {
private fun copyTextFromCommand(command: CopyTextFromCommand): Boolean {
val result = findElement(command.selector, optional = command.optional)
val metadata = getMetadata(maestroCommand).copy(
action = Action.TapPoint(Point(result.element.bounds.center().x, result.element.bounds.center().y)),
)
updateMetadata(maestroCommand, metadata);
copiedText = resolveText(result.element.treeNode.attributes)
?: throw MaestroException.UnableToCopyTextFromElement("Element does not contain text to copy: ${result.element}")

Expand Down Expand Up @@ -1338,33 +1249,11 @@ class Orchestra(

class CommandWarned(override val message: String) : Exception(message)

sealed class Action {
data class TapPoint(val point: maestro.Point) : Action();

sealed class SwipePoint : Action() {
data class WithDirection(
val direction: SwipeDirection,
val startPoint: maestro.Point
) : SwipePoint()

data class WithEndPoint(
val startPoint: maestro.Point,
val endPoint: maestro.Point
) : SwipePoint()
}

data class MultipleSwipePoint(
val direction: SwipeDirection,
val points: List<maestro.Point>
) : Action()
}

data class CommandMetadata(
val numberOfRuns: Int? = null,
val evaluatedCommand: MaestroCommand? = null,
val logMessages: List<String> = emptyList(),
val insight: Insight = Insight("", Insight.Level.NONE),
val action: Action? = null,
)

enum class ErrorResolution {
Expand Down
70 changes: 5 additions & 65 deletions maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3121,10 +3121,8 @@ class IntegrationTest {
var timeout = "0"
Maestro(driver).use {
orchestra(it, onCommandMetadataUpdate = { _, metaData ->
if(metaData.evaluatedCommand?.scrollUntilVisible != null) {
scrollDuration = metaData.evaluatedCommand?.scrollUntilVisible?.scrollDuration.toString()
timeout = metaData.evaluatedCommand?.scrollUntilVisible?.timeout.toString()
}
scrollDuration = metaData.evaluatedCommand?.scrollUntilVisible?.scrollDuration.toString()
timeout = metaData.evaluatedCommand?.scrollUntilVisible?.timeout.toString()
}).runFlow(commands)
}

Expand Down Expand Up @@ -3159,10 +3157,8 @@ class IntegrationTest {
var timeout = "0"
Maestro(driver).use {
orchestra(it, onCommandMetadataUpdate = { _, metaData ->
if(metaData.evaluatedCommand?.scrollUntilVisible != null) {
scrollDuration = metaData.evaluatedCommand?.scrollUntilVisible?.scrollDuration.toString()
timeout = metaData.evaluatedCommand?.scrollUntilVisible?.timeout.toString()
}
scrollDuration = metaData.evaluatedCommand?.scrollUntilVisible?.scrollDuration.toString()
timeout = metaData.evaluatedCommand?.scrollUntilVisible?.timeout.toString()
}).runFlow(commands)
}

Expand Down Expand Up @@ -3222,63 +3218,6 @@ class IntegrationTest {
)
}

fun `Case 120 - Scroll until view is Returning the bounds`() {
// Given
val commands = readCommands("120_scroll_until_visible_return_action")
val info = driver { }.deviceInfo()

val elementBounds = Bounds(0, 0 + info.heightGrid, 100, 100 + info.heightGrid)
val driver = driver {
element {
id = "maestro"
bounds = elementBounds
}
}

// When
var action: Orchestra.Action.MultipleSwipePoint? = null;
val targetAction = Orchestra.Action.MultipleSwipePoint(SwipeDirection.UP, listOf(Point(270,480)));

Maestro(driver).use {
orchestra(it, onCommandMetadataUpdate = { _, metaData ->
if(metaData.action != null) {
action = metaData.action as Orchestra.Action.MultipleSwipePoint;
}
}).runFlow(commands)
}

assertThat(action).isEqualTo(targetAction)
}

@Test
fun `Case 121 - Tap returns action`() {
// Given
val commands = readCommands("121_tap_returns_action")

val driver = driver {
element {
mutatingText = {
"Static Element"
}
bounds = Bounds(123, 142, 151, 500)
}
}

// When
var action: Orchestra.Action.TapPoint? = null;
val targetAction = Orchestra.Action.TapPoint(Point(137,321));

Maestro(driver).use {
orchestra(it, onCommandMetadataUpdate = { _, metaData ->
if(metaData.action != null) {
action = metaData.action as Orchestra.Action.TapPoint;
}
}).runFlow(commands)
}

assertThat(action).isEqualTo(targetAction)
}

private fun orchestra(
maestro: Maestro,
) = Orchestra(
Expand Down Expand Up @@ -3321,4 +3260,5 @@ class IntegrationTest {
return YamlCommandReader.readCommands(flowPath)
.withEnv(withEnv().withDefaultEnvVars(flowPath.toFile()))
}

}

This file was deleted.

Loading

0 comments on commit ca0472a

Please sign in to comment.