Skip to content

Commit

Permalink
Add overloads of clickOnElement and dispatchMouseClick with Duration
Browse files Browse the repository at this point in the history
Resolves:
#399
Resolves:
#398
  • Loading branch information
joffrey-bion committed Jun 9, 2024
1 parent 0582e40 commit f1bd232
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
Expand All @@ -23,7 +25,7 @@ suspend fun InputDomain.dispatchMouseClick(
this.button = button
this.clickCount = 1
}
delay(clickDurationMillis)
delay(clickDuration)
dispatchMouseEvent(
type = MouseEventType.mouseReleased,
x = x,
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,16 +17,37 @@ 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

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)
}

0 comments on commit f1bd232

Please sign in to comment.