Skip to content

Commit 4e26558

Browse files
committed
implement fireEvent
1 parent 7e5aa6e commit 4e26558

File tree

9 files changed

+157
-89
lines changed

9 files changed

+157
-89
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Central** <img src="https://search.maven.org/favicon.ico" width="16" height="16"
1515
Then, copy the declaration for your build tool. E.g.:_
1616

1717
```kotlin
18-
implementation("com.luissoares:selenium-testing-library:3.7.2")
18+
implementation("com.luissoares:selenium-testing-library:3.7.4")
1919
```
2020

2121
---

lib/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ java {
2929
withJavadocJar()
3030
}
3131

32-
val projectVersion = "3.7.2"
32+
val projectVersion = "3.7.4"
3333
version = projectVersion
3434
val groupId = "com.luissoares"
3535
group = groupId

lib/src/main/kotlin/seleniumtestinglib/WebElement.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.openqa.selenium.WebElement
55
import org.openqa.selenium.remote.RemoteWebDriver
66
import org.openqa.selenium.remote.RemoteWebElement
77
import org.openqa.selenium.support.ui.Select
8+
import seleniumtestinglib.queries.executeTLQuery
89

910
val WebElement.value: Any?
1011
get() {
@@ -21,7 +22,7 @@ val WebElement.displayValue: Any?
2122
Select(this).let {
2223
when {
2324
it.isMultiple -> it.allSelectedOptions.map(WebElement::getText)
24-
else -> it.firstSelectedOption.text
25+
else -> it.firstSelectedOption.text
2526
}
2627
} else getAttribute("value")
2728

@@ -37,14 +38,14 @@ val WebElement.formValues: Map<String, Any?>
3738
"input" -> when (elements.first().getAttribute("type")) {
3839
"checkbox" -> when {
3940
elements.size > 1 -> elements.filter { it.isChecked }.map { it.getAttribute("value") }
40-
else -> elements.first().isChecked
41+
else -> elements.first().isChecked
4142
}
4243

43-
"radio" -> elements.firstOrNull { it.isChecked }?.value
44-
else -> elements.first().value
44+
"radio" -> elements.firstOrNull { it.isChecked }?.value
45+
else -> elements.first().value
4546
}
4647

47-
else -> elements.first().value
48+
else -> elements.first().value
4849
}
4950
}
5051
}
@@ -82,7 +83,7 @@ val WebElement.isRequired
8283
val WebElement.isValid
8384
get() = when (tagName) {
8485
"form" -> wrappedDriver.executeScript("return arguments[0].checkValidity()", this) as Boolean
85-
else -> wrappedDriver.executeScript(
86+
else -> wrappedDriver.executeScript(
8687
"return arguments[0].getAttribute('aria-invalid')",
8788
this
8889
) in setOf(null, "false")
@@ -137,3 +138,22 @@ val WebElement.errorMessage: String?
137138
}
138139

139140
internal val WebElement.wrappedDriver get() = (this as RemoteWebElement).wrappedDriver as RemoteWebDriver
141+
142+
143+
fun WebElement.fireEvent(eventName: Event, eventProperties: Map<String, Map<String, Any?>> = emptyMap()): Any? {
144+
wrappedDriver.ensureScript("testing-library.js", "fireEvent.change")
145+
return wrappedDriver.executeScript("fireEvent.$eventName(arguments[0], arguments[1])", this, eventProperties)
146+
}
147+
148+
@Suppress("unused")
149+
enum class Event {
150+
copy, cut, paste, compositionEnd, compositionStart, compositionUpdate, keyDown, keyPress, keyUp, focus, blur,
151+
focusIn, focusOut, change, input, invalid, submit, reset, click, contextMenu, dblClick, drag, dragEnd, dragEnter,
152+
dragExit, dragLeave, dragOver, dragStart, drop, mouseDown, mouseEnter, mouseLeave, mouseMove, mouseOut, mouseOver,
153+
mouseUp, select, touchCancel, touchEnd, touchMove, touchStart, resize, scroll, wheel, abort, canPlay,
154+
canPlayThrough, durationChange, emptied, encrypted, ended, loadedData, loadedMetadata, loadStart, pause, play,
155+
playing, progress, rateChange, seeked, seeking, stalled, suspend, timeUpdate, volumeChange, waiting, load, error,
156+
animationStart, animationEnd, animationIteration, transitionCancel, transitionEnd, transitionRun, transitionStart,
157+
pointerOver, pointerEnter, pointerDown, pointerMove, pointerUp, pointerCancel, pointerOut, pointerLeave,
158+
gotPointerCapture, lostPointerCapture, popState, offline, online, doubleClick
159+
}

lib/src/main/resources/testing-library.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/main/resources/user-event.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package seleniumtestinglib
2+
3+
import org.junit.jupiter.api.extension.ExtendWith
4+
import org.openqa.selenium.remote.RemoteWebDriver
5+
import seleniumtestinglib.Event.change
6+
import seleniumtestinglib.locators.ByRole
7+
import seleniumtestinglib.locators.Role.TextBox
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
11+
@ExtendWith(DriverLifeCycle::class)
12+
class FireEventTest(private val driver: RemoteWebDriver) {
13+
14+
@Test
15+
fun `fire event`() {
16+
driver.render("""<input />""")
17+
val input = driver.findElement(ByRole(TextBox))
18+
19+
input.fireEvent(change, mapOf("target" to mapOf("value" to "2020-05-24")))
20+
21+
assertEquals("2020-05-24", input.value)
22+
}
23+
}

0 commit comments

Comments
 (0)