Skip to content

Commit

Permalink
fix: make waitForUploads poll for upload processing until completion …
Browse files Browse the repository at this point in the history
…or timeout (#6904) (#6914)

Co-authored-by: Marco Collovati <marco@vaadin.com>
Co-authored-by: Sergey Vinogradov <mr.vursen@gmail.com>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent 54504e2 commit 9f88070
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@
import static org.junit.Assert.assertThat;

import java.io.File;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.v127.network.Network;
import org.openqa.selenium.logging.LogEntry;

import com.vaadin.flow.component.upload.testbench.UploadElement;
Expand Down Expand Up @@ -161,6 +167,32 @@ public void uploadFileAndNoErrorThrown() throws Exception {
logList2.size(), CoreMatchers.is(0));
}

@Test
public void slowUpload_waitForUpload_pollsUntilUploadFinishes()
throws Exception {
Assume.assumeTrue("Current driver does not support Dev Tools",
driver instanceof HasDevTools);

// Slow down upload to 100 kb/sec
DevTools devTools = ((HasDevTools) driver).getDevTools();
devTools.createSessionIfThereIsNotOne();
devTools.send(Network.enable(Optional.empty(), Optional.empty(),
Optional.empty()));
devTools.send(Network.emulateNetworkConditions(false, 40, 900000,
100000, Optional.empty(), Optional.empty(), Optional.empty(),
Optional.empty()));

File tempFile = createTempFile("txt");
Files.write(tempFile.toPath(), new byte[1024 * 300]);

getUpload().upload(tempFile, 10);

$("button").id("print-file-count").click();

Assert.assertEquals("File list should contain one file", 1,
getFileCount());
}

private int getFileCount() {
return Integer.parseInt($("div").id("file-count").getText());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Timeouts;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WrapsDriver;
import org.openqa.selenium.WrapsElement;
Expand Down Expand Up @@ -147,17 +145,10 @@ public void removeFile(int index) {
* the number of seconds to wait for the upload to finish
*/
private void waitForUploads(int maxSeconds) {
Timeouts timeouts = getDriver().manage().timeouts();
timeouts.setScriptTimeout(maxSeconds, TimeUnit.SECONDS);

String script = "var callback = arguments[arguments.length - 1];"
+ "var upload = arguments[0];"
+ "window.setTimeout(function() {"
+ " var inProgress = upload.files.filter(function(file) { return file.uploading;}).length >0;"
+ " if (!inProgress) callback();" //
+ "}, 500);";
getCommandExecutor().getDriver().executeAsyncScript(script, this);
String script = "return arguments[0].files.every((file) => !file.uploading);";

waitUntil(driver -> (Boolean) executeScript(script, UploadElement.this),
maxSeconds);
}

private void startUpload() {
Expand Down

0 comments on commit 9f88070

Please sign in to comment.