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

feat: paste from clipboard #246

Merged
merged 1 commit into from
Oct 3, 2022
Merged
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
2 changes: 2 additions & 0 deletions maestro-client/src/main/java/maestro/Driver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ interface Driver {

fun hideKeyboard()

fun clipboardPaste()

fun takeScreenshot(out: Sink)

}
7 changes: 7 additions & 0 deletions maestro-client/src/main/java/maestro/Maestro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ class Maestro(private val driver: Driver) : AutoCloseable {
waitForAppToSettle()
}

fun clipboardPaste() {
LOGGER.info("Paste from Clipboard")

driver.clipboardPaste()
waitForAppToSettle()
}

fun swipe(start: Point, end: Point) {
LOGGER.info("Swiping from (${start.x},${start.y}) to (${end.x},${end.y})")

Expand Down
4 changes: 4 additions & 0 deletions maestro-client/src/main/java/maestro/drivers/AndroidDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ class AndroidDriver(
dadb.shell("input keyevent 111")
}

override fun clipboardPaste() {
dadb.shell("input keyevent 279")
}

override fun takeScreenshot(out: Sink) {
val deviceScreenshotPath = "/sdcard/maestro-screenshot.png"

Expand Down
4 changes: 4 additions & 0 deletions maestro-client/src/main/java/maestro/drivers/IOSDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ class IOSDriver(
iosDevice.pressKey(40).expect {}
}

override fun clipboardPaste() {
iosDevice.pressKey(40).expect {}
}

override fun takeScreenshot(out: Sink) {
iosDevice.takeScreenshot(out).expect {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,31 @@ class HideKeyboardCommand : Command {
}
}

class ClipboardPasteCommand : Command {

override fun equals(other: Any?): Boolean {
if (this == other) return true
if (javaClass != other?.javaClass) return false
return true
}

override fun hashCode(): Int {
return javaClass.hashCode()
}

override fun toString(): String {
return "ClipboardPasteCommand()"
}

override fun description(): String {
return "Paste from Clipboard"
}

override fun injectEnv(env: Map<String, String>): ClipboardPasteCommand {
return this
}
}

data class TapOnElementCommand(
val selector: ElementSelector,
val retryIfNoChange: Boolean? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ data class MaestroCommand(
val pressKeyCommand: PressKeyCommand? = null,
val eraseTextCommand: EraseTextCommand? = null,
val hideKeyboardCommand: HideKeyboardCommand? = null,
val clipboardPasteCommand: ClipboardPasteCommand? = null,
val takeScreenshotCommand: TakeScreenshotCommand? = null,
val stopAppCommand: StopAppCommand? = null,
val clearStateCommand: ClearStateCommand? = null,
Expand All @@ -60,6 +61,7 @@ data class MaestroCommand(
pressKeyCommand = command as? PressKeyCommand,
eraseTextCommand = command as? EraseTextCommand,
hideKeyboardCommand = command as? HideKeyboardCommand,
clipboardPasteCommand = command as? ClipboardPasteCommand,
takeScreenshotCommand = command as? TakeScreenshotCommand,
stopAppCommand = command as? StopAppCommand,
clearStateCommand = command as? ClearStateCommand,
Expand All @@ -81,6 +83,7 @@ data class MaestroCommand(
pressKeyCommand != null -> pressKeyCommand
eraseTextCommand != null -> eraseTextCommand
hideKeyboardCommand != null -> hideKeyboardCommand
clipboardPasteCommand != null -> clipboardPasteCommand
takeScreenshotCommand != null -> takeScreenshotCommand
stopAppCommand != null -> stopAppCommand
clearStateCommand != null -> clearStateCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class Orchestra(
is TapOnPointCommand -> tapOnPoint(command, command.retryIfNoChange ?: true)
is BackPressCommand -> maestro.backPress()
is HideKeyboardCommand -> maestro.hideKeyboard()
is ClipboardPasteCommand -> maestro.clipboardPaste()
is ScrollCommand -> maestro.scrollVertical()
is SwipeCommand -> swipeCommand(command)
is AssertCommand -> assertCommand(command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import maestro.orchestra.ElementSelector
import maestro.orchestra.ElementTrait
import maestro.orchestra.EraseTextCommand
import maestro.orchestra.HideKeyboardCommand
import maestro.orchestra.ClipboardPasteCommand
import maestro.orchestra.InputTextCommand
import maestro.orchestra.LaunchAppCommand
import maestro.orchestra.MaestroCommand
Expand Down Expand Up @@ -84,6 +85,7 @@ data class YamlFluentCommand(
when (action) {
"back" -> MaestroCommand(BackPressCommand())
"hide keyboard" -> MaestroCommand(HideKeyboardCommand())
"clipboard paste" -> MaestroCommand(ClipboardPasteCommand())
"scroll" -> MaestroCommand(ScrollCommand())
"clearKeychain" -> MaestroCommand(ClearKeychainCommand())
else -> error("Unknown navigation target: $action")
Expand Down Expand Up @@ -360,6 +362,10 @@ data class YamlFluentCommand(
action = "hide keyboard"
)

"clipboard paste", "clipboardPaste" -> YamlFluentCommand(
action = "clipboard paste"
)

"scroll" -> YamlFluentCommand(
action = "scroll"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ class FakeDriver : Driver {
events += Event.HideKeyboard
}

override fun clipboardPaste() {
ensureOpen()

events += Event.ClipboardPaste
}

override fun takeScreenshot(out: Sink) {
ensureOpen()

Expand Down Expand Up @@ -280,6 +286,8 @@ class FakeDriver : Driver {
object BackPress : Event(), UserInteraction

object HideKeyboard : Event(), UserInteraction

object ClipboardPaste : Event(), UserInteraction

data class InputText(
val text: String
Expand Down
23 changes: 23 additions & 0 deletions maestro-test/src/test/kotlin/maestro/test/IntegrationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,29 @@ class IntegrationTest {
driver.assertEventCount(Event.Tap(Point(50, 50)), 1)
}

@Test
fun `Case 050 - Paste from Clipboard`() {
// Given
val commands = readCommands("050_clipboard_paste")

val driver = driver {
}

// When
Maestro(driver).use {
orchestra(it).runFlow(commands)
}

// Then
// No test failure
driver.assertEvents(
listOf(
Event.ClipboardPaste,
Event.ClipboardPaste,
)
)
}

private fun orchestra(it: Maestro) = Orchestra(it, lookupTimeoutMs = 0L, optionalLookupTimeoutMs = 0L)

private fun driver(builder: FakeLayoutElement.() -> Unit): FakeDriver {
Expand Down
4 changes: 4 additions & 0 deletions maestro-test/src/test/resources/050_clipboard_paste.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
appId: com.example.app
---
- action: clipboard paste
- clipboardPaste