Skip to content

Commit

Permalink
[java] Adding a test for multi-file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Nov 18, 2019
1 parent b823a5e commit 8264a3a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion common/src/web/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
onsubmit="onUploadSubmit();">
<div>
Enter a file to upload:
<div><input id="upload" name="upload" type="file"/></div>
<div><input id="upload" name="upload" type="file" multiple/></div>
<div><input id="go" type="submit" value="Go!"/></div>
</div>
<div id="upload_label" style="display:none"></div>
Expand Down
50 changes: 43 additions & 7 deletions java/client/test/org/openqa/selenium/UploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import static org.openqa.selenium.testing.drivers.Browser.HTMLUNIT;
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;

import com.google.common.collect.ImmutableList;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.CapabilityType;
Expand All @@ -45,9 +47,11 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import java.util.List;
import java.util.stream.Collectors;

/**
* Demonstrates how to use WebDriver with a file input element.
Expand All @@ -60,7 +64,7 @@ public class UploadTest extends JUnit4TestBase {
private File testFile;

@Before
public void setUp() throws Exception {
public void setUp() {
testFile = createTmpFile(FILE_HTML);
}

Expand All @@ -86,6 +90,34 @@ public void testFileUploading() {
wait.until(elementTextToEqual(body, LOREM_IPSUM_TEXT));
}

@SwitchToTopAfterTest
@Test
@NotYetImplemented(value = SAFARI, reason = "Returns wrong text of the frame body")
public void testMultipleFileUploading() {
List<String> multiContent = ImmutableList.of(LOREM_IPSUM_TEXT, LOREM_IPSUM_TEXT, LOREM_IPSUM_TEXT);
String fileNames = multiContent.stream()
.map(text -> "<div>" + text + "</div>")
.map(this::createTmpFile)
.map(File::getAbsolutePath)
.collect(Collectors.joining("\n"));
assumeFalse(
"This test as written assumes a file on local disk is accessible to the browser. "
+ "That is not true for browsers on mobile platforms.",
TestUtilities.getEffectivePlatform(driver).is(ANDROID));
driver.get(pages.uploadPage);
driver.findElement(By.id("upload")).sendKeys(fileNames);
driver.findElement(By.id("go")).click();

// Uploading files across a network may take a while, even if they're really small
WebElement label = driver.findElement(By.id("upload_label"));
wait.until(not(visibilityOf(label)));

driver.switchTo().frame("upload_target");

WebElement body = driver.findElement(By.xpath("//body"));
wait.until(elementTextToEqual(body, String.join("\n", multiContent)));
}

@Test
public void testCleanFileInput() {
driver.get(pages.uploadPage);
Expand Down Expand Up @@ -159,10 +191,14 @@ public void testUploadingWithInvisibleFileInputWhenStrictFileInteractabilityIsOn
() -> input.sendKeys(testFile.getAbsolutePath()));
}

private File createTmpFile(String content) throws IOException {
File f = File.createTempFile("webdriver", "tmp");
f.deleteOnExit();
Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8));
return f;
private File createTmpFile(String content) {
try {
File f = File.createTempFile("webdriver", "tmp");
f.deleteOnExit();
Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8));
return f;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,24 @@ protected void doPost(HttpServletRequest request,
response.setStatus(HttpServletResponse.SC_OK);

request.setAttribute(Request.MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);
Part upload = request.getPart("upload");

byte[] buffer = new byte[(int) upload.getSize()];
String content;
try (InputStream in = upload.getInputStream()) {
in.read(buffer, 0, (int) upload.getSize());
content = new String(buffer, StandardCharsets.UTF_8);
StringBuilder content = new StringBuilder();
for (Part part : request.getParts()) {
if (part.getName().equalsIgnoreCase("upload")) {
byte[] buffer = new byte[(int) part.getSize()];
try (InputStream in = part.getInputStream()) {
in.read(buffer, 0, (int) part.getSize());
content.append(new String(buffer, StandardCharsets.UTF_8));
}
}
}

// Slow down the upload so we can verify WebDriver waits.
try {
Thread.sleep(2500);
} catch (InterruptedException ignored) {
}
response.getWriter().write(content);
response.getWriter().write(content.toString());
response.getWriter().write(
"<script>window.top.window.onUploadDone();</script>");
response.setStatus(HttpServletResponse.SC_OK);
Expand Down

0 comments on commit 8264a3a

Please sign in to comment.