Skip to content

Commit

Permalink
[java] make RemoteWebDriverDownloadTest less flaky
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Nov 7, 2024
1 parent 34d3ff8 commit 15b44c0
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -57,6 +58,8 @@

class RemoteWebDriverDownloadTest {

private static final Set<String> FILE_EXTENSIONS = Set.of(".txt", ".jpg");

private Server<?> server;
private NettyAppServer appServer;
private Capabilities capabilities;
Expand Down Expand Up @@ -112,7 +115,15 @@ void canListDownloadedFiles() {
driver.findElement(By.id("file-2")).click();

new WebDriverWait(driver, Duration.ofSeconds(5))
.until(d -> ((HasDownloads) d).getDownloadableFiles().size() == 2);
.until(
d ->
((HasDownloads) d)
.getDownloadableFiles().stream()
// ensure we hit no temporary file created by the browser while
// downloading
.filter((f) -> FILE_EXTENSIONS.stream().anyMatch(f::endsWith))
.count()
== 2);

List<String> downloadableFiles = ((HasDownloads) driver).getDownloadableFiles();
assertThat(downloadableFiles).contains("file_1.txt", "file_2.jpg");
Expand All @@ -132,7 +143,12 @@ void canDownloadFiles() throws IOException {
driver.findElement(By.id("file-1")).click();

new WebDriverWait(driver, Duration.ofSeconds(5))
.until(d -> !((HasDownloads) d).getDownloadableFiles().isEmpty());
.until(
d ->
((HasDownloads) d)
.getDownloadableFiles().stream()
// ensure we hit no temporary file created by the browser while downloading
.anyMatch((f) -> FILE_EXTENSIONS.stream().anyMatch(f::endsWith)));

String fileName = ((HasDownloads) driver).getDownloadableFiles().get(0);

Expand All @@ -155,7 +171,12 @@ void testCanDeleteFiles() {
driver.findElement(By.id("file-1")).click();

new WebDriverWait(driver, Duration.ofSeconds(5))
.until(d -> !((HasDownloads) d).getDownloadableFiles().isEmpty());
.until(
d ->
((HasDownloads) d)
.getDownloadableFiles().stream()
// ensure we hit no temporary file created by the browser while downloading
.anyMatch((f) -> FILE_EXTENSIONS.stream().anyMatch(f::endsWith)));

driver = new Augmenter().augment(driver);
((HasDownloads) driver).deleteDownloadableFiles();
Expand Down

0 comments on commit 15b44c0

Please sign in to comment.