Skip to content

Commit a79c7a2

Browse files
committed
feat: input get value
1 parent 97408bc commit a79c7a2

File tree

7 files changed

+48
-3
lines changed

7 files changed

+48
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ To use kdriver, add the following to your `build.gradle.kts`:
4747

4848
```kotlin
4949
dependencies {
50-
implementation("dev.kdriver:core:0.3.0")
50+
implementation("dev.kdriver:core:0.3.1")
5151
}
5252
```
5353

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
allprojects {
88
group = "dev.kdriver"
9-
version = "0.3.0"
9+
version = "0.3.1"
1010
project.ext.set("url", "https://github.com/cdpdriver/kdriver")
1111
project.ext.set("license.name", "Apache 2.0")
1212
project.ext.set("license.url", "https://www.apache.org/licenses/LICENSE-2.0.txt")

core/src/commonMain/kotlin/dev/kdriver/core/dom/DefaultElement.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ open class DefaultElement(
183183
)
184184
}
185185

186+
override suspend fun getInputValue(): String? {
187+
return apply<String>("(el) => el.value")
188+
}
189+
186190
override suspend fun clearInput() {
187191
apply<Unit>("function (element) { element.value = \"\" }")
188192
}

core/src/commonMain/kotlin/dev/kdriver/core/dom/Element.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ interface Element {
159159
*/
160160
suspend fun sendFile(paths: List<Path>)
161161

162+
/**
163+
* Retrieves the value of the element, typically used for input fields.
164+
*
165+
* This method applies a JavaScript function to get the `value` property of the element.
166+
*
167+
* @return The value of the element, or null if it does not have a value.
168+
*/
169+
suspend fun getInputValue(): String?
170+
162171
/**
163172
* Clears the input of the element by setting its value to an empty string.
164173
*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package dev.kdriver.core.dom
2+
3+
import dev.kdriver.core.browser.createBrowser
4+
import dev.kdriver.core.sampleFile
5+
import kotlinx.coroutines.runBlocking
6+
import kotlin.test.Test
7+
import kotlin.test.assertEquals
8+
9+
class ElementTest {
10+
11+
@Test
12+
fun testInput() = runBlocking {
13+
val browser = createBrowser(this, headless = true, sandbox = false)
14+
val tab = browser.get(sampleFile("groceries.html"))
15+
16+
val input = tab.select("#my_input")
17+
18+
assertEquals("", input.getInputValue())
19+
input.insertText("Hello World")
20+
assertEquals("Hello World", input.getInputValue())
21+
input.clearInput()
22+
assertEquals("", input.getInputValue())
23+
input.sendKeys("KDriver")
24+
assertEquals("KDriver", input.getInputValue())
25+
input.clearInputByDeleting()
26+
assertEquals("", input.getInputValue())
27+
28+
browser.stop()
29+
}
30+
31+
}

core/src/jvmTest/resources/groceries.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ <h1>Grocery List</h1>
3737
<li>Grapes</li>
3838
</ul>
3939
<button id="download_file" onclick="downloadGroceryList()">Download List</button>
40+
<input id="my_input" type="text"/>
4041
</body>
4142
</html>

docs/home/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ To install, add the dependency to your `build.gradle.kts`:
1212

1313
```kotlin
1414
dependencies {
15-
implementation("dev.kdriver:core:0.3.0")
15+
implementation("dev.kdriver:core:0.3.1")
1616
}
1717
```
1818

0 commit comments

Comments
 (0)