Skip to content

Commit 128371d

Browse files
committed
allow multiple file upload
1 parent 0fef21e commit 128371d

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,20 @@ fun User.type(
208208
/**
209209
* https://testing-library.com/docs/user-event/utility#upload
210210
*/
211-
fun User.upload(input: WebElement, file: File): User {
211+
fun User.upload(input: WebElement, vararg file: File): User {
212+
val fileS = file.mapIndexed { idx, el -> el.toJS(1 + idx * 3) }
213+
.joinToString(
214+
prefix = "[".takeIf { file.size > 1 } ?: "",
215+
postfix = "]".takeIf { file.size > 1 } ?: ""
216+
)
212217
driver.executeScript(
213-
"await user.upload(arguments[0], new File(arguments[1], arguments[2], arguments[3]))",
218+
"await user.upload(arguments[0], $fileS)",
214219
input,
215-
file.bits,
216-
file.name,
217-
file.options
220+
*file.map { listOf(it.bits, it.name, it.options) }.flatten().toTypedArray()
218221
)
219222
return this
220223
}
221224

222-
data class File(val bits: List<String>, val name: String, val options: Map<String, String> = emptyMap())
225+
data class File(val bits: List<String>, val name: String, val options: Map<String, String> = emptyMap()) {
226+
internal fun toJS(i: Int) = "new File(arguments[$i], arguments[${i + 1}], arguments[${i + 2}])"
227+
}

lib/src/test/kotlin/seleniumtestinglib/interactions/UploadTest.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,31 @@ class UploadTest {
3232
assertEquals("hello".length.toLong(), upload["size"])
3333
}
3434

35-
// TODO: multiple files
35+
@Test
36+
fun `upload multiple`() {
37+
driver.render(
38+
"""
39+
<div>
40+
<label for="file-uploader">Upload file:</label>
41+
<input id="file-uploader" type="file" multiple />
42+
</div>
43+
"""
44+
)
45+
val input = driver.findElement(labelText("upload file").exact(false))
46+
47+
driver.user.upload(
48+
input,
49+
File(listOf("hello1"), "hello.png", mapOf("type" to "image/png")),
50+
File(listOf("hello2"), "hello.jpg", mapOf("type" to "image/jpeg")),
51+
)
52+
53+
val upload = input.files
54+
assertEquals(2, input.files.size)
55+
assertEquals("hello.png", upload.first()["name"])
56+
assertEquals("image/png", upload.first()["type"])
57+
assertEquals("hello1".length.toLong(), upload.first()["size"])
58+
assertEquals("hello.jpg", upload.last()["name"])
59+
assertEquals("image/jpeg", upload.last()["type"])
60+
assertEquals("hello2".length.toLong(), upload.last()["size"])
61+
}
3662
}

0 commit comments

Comments
 (0)