3030import java .util .LinkedList ;
3131import java .util .List ;
3232import java .util .Objects ;
33+ import java .util .Set ;
3334import java .util .concurrent .ExecutorService ;
3435import java .util .concurrent .Executors ;
3536import org .junit .jupiter .api .AfterEach ;
5758
5859class RemoteWebDriverDownloadTest {
5960
61+ private static final Set <String > FILE_EXTENSIONS = Set .of (".txt" , ".jpg" );
62+
6063 private Server <?> server ;
6164 private NettyAppServer appServer ;
6265 private Capabilities capabilities ;
@@ -112,7 +115,15 @@ void canListDownloadedFiles() {
112115 driver .findElement (By .id ("file-2" )).click ();
113116
114117 new WebDriverWait (driver , Duration .ofSeconds (5 ))
115- .until (d -> ((HasDownloads ) d ).getDownloadableFiles ().size () == 2 );
118+ .until (
119+ d ->
120+ ((HasDownloads ) d )
121+ .getDownloadableFiles ().stream ()
122+ // ensure we hit no temporary file created by the browser while
123+ // downloading
124+ .filter ((f ) -> FILE_EXTENSIONS .stream ().anyMatch (f ::endsWith ))
125+ .count ()
126+ == 2 );
116127
117128 List <String > downloadableFiles = ((HasDownloads ) driver ).getDownloadableFiles ();
118129 assertThat (downloadableFiles ).contains ("file_1.txt" , "file_2.jpg" );
@@ -132,7 +143,12 @@ void canDownloadFiles() throws IOException {
132143 driver .findElement (By .id ("file-1" )).click ();
133144
134145 new WebDriverWait (driver , Duration .ofSeconds (5 ))
135- .until (d -> !((HasDownloads ) d ).getDownloadableFiles ().isEmpty ());
146+ .until (
147+ d ->
148+ ((HasDownloads ) d )
149+ .getDownloadableFiles ().stream ()
150+ // ensure we hit no temporary file created by the browser while downloading
151+ .anyMatch ((f ) -> FILE_EXTENSIONS .stream ().anyMatch (f ::endsWith )));
136152
137153 String fileName = ((HasDownloads ) driver ).getDownloadableFiles ().get (0 );
138154
@@ -155,7 +171,12 @@ void testCanDeleteFiles() {
155171 driver .findElement (By .id ("file-1" )).click ();
156172
157173 new WebDriverWait (driver , Duration .ofSeconds (5 ))
158- .until (d -> !((HasDownloads ) d ).getDownloadableFiles ().isEmpty ());
174+ .until (
175+ d ->
176+ ((HasDownloads ) d )
177+ .getDownloadableFiles ().stream ()
178+ // ensure we hit no temporary file created by the browser while downloading
179+ .anyMatch ((f ) -> FILE_EXTENSIONS .stream ().anyMatch (f ::endsWith )));
159180
160181 driver = new Augmenter ().augment (driver );
161182 ((HasDownloads ) driver ).deleteDownloadableFiles ();
0 commit comments