From f1bd2322ac3e4d8da3aa1aa557494b3c3fd7ab65 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sun, 9 Jun 2024 18:46:53 +0200 Subject: [PATCH] Add overloads of clickOnElement and dispatchMouseClick with Duration Resolves: https://github.com/joffrey-bion/chrome-devtools-kotlin/issues/399 Resolves: https://github.com/joffrey-bion/chrome-devtools-kotlin/issues/398 --- .../devtools/domains/input/InputExtensions.kt | 31 +++++++++++++++++-- .../extensions/CrossDomainExtensions.kt | 29 +++++++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/commonMain/kotlin/org/hildan/chrome/devtools/domains/input/InputExtensions.kt b/src/commonMain/kotlin/org/hildan/chrome/devtools/domains/input/InputExtensions.kt index e31a6ec7..a358c5ec 100644 --- a/src/commonMain/kotlin/org/hildan/chrome/devtools/domains/input/InputExtensions.kt +++ b/src/commonMain/kotlin/org/hildan/chrome/devtools/domains/input/InputExtensions.kt @@ -1,6 +1,8 @@ package org.hildan.chrome.devtools.domains.input import kotlinx.coroutines.delay +import kotlin.time.* +import kotlin.time.Duration.Companion.milliseconds /** * Simulates a mouse click on the given [x] and [y] coordinates. @@ -12,8 +14,8 @@ import kotlinx.coroutines.delay suspend fun InputDomain.dispatchMouseClick( x: Double, y: Double, - clickDurationMillis: Long = 100, - button: MouseButton = MouseButton.left + clickDuration: Duration = 100.milliseconds, + button: MouseButton = MouseButton.left, ) { dispatchMouseEvent( type = MouseEventType.mousePressed, @@ -23,7 +25,7 @@ suspend fun InputDomain.dispatchMouseClick( this.button = button this.clickCount = 1 } - delay(clickDurationMillis) + delay(clickDuration) dispatchMouseEvent( type = MouseEventType.mouseReleased, x = x, @@ -32,3 +34,26 @@ suspend fun InputDomain.dispatchMouseClick( this.button = button } } + +/** + * Simulates a mouse click on the given [x] and [y] coordinates. + * This uses a `mousePressed` and `mouseReleased` event in quick succession. + * + * The current tab doesn't need to be focused. + * If this click opens a new tab, that new tab may become focused, but this session still targets the old tab. + */ +@Deprecated( + message = "Use dispatchMouseClick with a Duration type for clickDuration.", + replaceWith = ReplaceWith( + expression = "this.dispatchMouseClick(x, y, clickDurationMillis.milliseconds, button)", + imports = ["kotlin.time.Duration.Companion.milliseconds"], + ), +) +suspend fun InputDomain.dispatchMouseClick( + x: Double, + y: Double, + clickDurationMillis: Long = 100, + button: MouseButton = MouseButton.left, +) { + dispatchMouseClick(x, y, clickDurationMillis.milliseconds, button) +} diff --git a/src/commonMain/kotlin/org/hildan/chrome/devtools/extensions/CrossDomainExtensions.kt b/src/commonMain/kotlin/org/hildan/chrome/devtools/extensions/CrossDomainExtensions.kt index 6bcb8249..a5f7df1c 100644 --- a/src/commonMain/kotlin/org/hildan/chrome/devtools/extensions/CrossDomainExtensions.kt +++ b/src/commonMain/kotlin/org/hildan/chrome/devtools/extensions/CrossDomainExtensions.kt @@ -6,6 +6,8 @@ import org.hildan.chrome.devtools.domains.input.MouseButton import org.hildan.chrome.devtools.domains.input.dispatchMouseClick import org.hildan.chrome.devtools.domains.utils.center import org.hildan.chrome.devtools.sessions.PageSession +import kotlin.time.* +import kotlin.time.Duration.Companion.milliseconds /** * Finds a DOM element via the given [selector], and simulates a click event on it based on its padding box. @@ -15,8 +17,8 @@ import org.hildan.chrome.devtools.sessions.PageSession */ suspend fun PageSession.clickOnElement( selector: CssSelector, - clickDurationMillis: Long = 100, - mouseButton: MouseButton = MouseButton.left + clickDuration: Duration = 100.milliseconds, + mouseButton: MouseButton = MouseButton.left, ) { val box = dom.getBoxModel(selector) ?: error("Cannot click on element, no node found using selector '$selector'") val elementCenter = box.content.center @@ -24,7 +26,28 @@ suspend fun PageSession.clickOnElement( input.dispatchMouseClick( x = elementCenter.x, y = elementCenter.y, - clickDurationMillis = clickDurationMillis, + clickDuration = clickDuration, button = mouseButton, ) } + +/** + * Finds a DOM element via the given [selector], and simulates a click event on it based on its padding box. + * The current tab doesn't need to be focused. + * + * If this click opens a new tab, that new tab may become focused, but this session still targets the old tab. + */ +@Deprecated( + message = "Use clickOnElement with a Duration type for clickDuration.", + replaceWith = ReplaceWith( + expression = "this.clickOnElement(selector, clickDurationMillis.milliseconds, mouseButton)", + imports = ["kotlin.time.Duration.Companion.milliseconds"], + ), +) +suspend fun PageSession.clickOnElement( + selector: CssSelector, + clickDurationMillis: Long = 100, + mouseButton: MouseButton = MouseButton.left, +) { + clickOnElement(selector, clickDurationMillis.milliseconds, mouseButton) +}