Skip to content

Commit

Permalink
allow multiple file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lsoares committed Mar 8, 2024
1 parent 0fef21e commit c2411d7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
17 changes: 11 additions & 6 deletions lib/src/main/kotlin/seleniumtestinglib/UserEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,20 @@ fun User.type(
/**
* https://testing-library.com/docs/user-event/utility#upload
*/
fun User.upload(input: WebElement, file: File): User {
fun User.upload(input: WebElement, vararg file: File): User {
val fileS = file.mapIndexed { idx, el -> el.toJS(1 + idx * 3) }
.joinToString(
prefix = "[".takeIf { file.size > 1 } ?: "",
postfix = "]".takeIf { file.size > 1 } ?: ""
)
driver.executeScript(
"await user.upload(arguments[0], new File(arguments[1], arguments[2], arguments[3]))",
"await user.upload(arguments[0], $fileS)",
input,
file.bits,
file.name,
file.options
*file.map { listOf(it.bits, it.name, it.options) }.flatten().toTypedArray()
)
return this
}

data class File(val bits: List<String>, val name: String, val options: Map<String, String> = emptyMap())
data class File(val bits: List<String>, val name: String, val options: Map<String, String> = emptyMap()) {
internal fun toJS(i: Int) = "new File(arguments[$i], arguments[${i + 1}], arguments[${i + 2}])"
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,31 @@ class UploadTest {
assertEquals("hello".length.toLong(), upload["size"])
}

// TODO: multiple files
@Test
fun `upload multiple`() {
driver.render(
"""
<div>
<label for="file-uploader">Upload file:</label>
<input id="file-uploader" type="file" multiple />
</div>
"""
)
val input = driver.findElement(labelText("upload file").exact(false))

driver.user.upload(
input,
File(listOf("hello1"), "hello.png", mapOf("type" to "image/png")),
File(listOf("hello2"), "hello.jpg", mapOf("type" to "image/jpeg")),
)

val upload = input.files
assertEquals(2, input.files.size)
assertEquals("hello.png", upload.first()["name"])
assertEquals("image/png", upload.first()["type"])
assertEquals("hello1".length.toLong(), upload.first()["size"])
assertEquals("hello.jpg", upload.last()["name"])
assertEquals("image/jpeg", upload.last()["type"])
assertEquals("hello2".length.toLong(), upload.last()["size"])
}
}

0 comments on commit c2411d7

Please sign in to comment.