Skip to content

Commit 6e6e01c

Browse files
titusfortnerdiemol
andauthored
Downloads (#286)
* update Selenium version * add demo for downloading a file on Sauce * Hardcoding Selenium version again. Plugin was not working. * Not using unsplash anymore --------- Co-authored-by: Diego Molina <[email protected]> Co-authored-by: Diego Molina <[email protected]>
1 parent c8a43f4 commit 6e6e01c

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

selenium-junit5-examples/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
<properties>
3030
<surefire.parallel>10</surefire.parallel>
31+
<selenium.version>4.32.0</selenium.version>
3132
<maven.compiler.source>11</maven.compiler.source>
3233
<maven.compiler.target>11</maven.compiler.target>
3334
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -37,7 +38,7 @@
3738
<dependency>
3839
<groupId>org.seleniumhq.selenium</groupId>
3940
<artifactId>selenium-java</artifactId>
40-
<version>4.32.0</version>
41+
<version>${selenium.version}</version>
4142
<scope>test</scope>
4243
</dependency>
4344
<dependency>

selenium-junit5-examples/src/test/java/com/saucedemo/selenium/TestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected Map<String, Object> defaultSauceOptions(TestInfo testInfo) {
7575
options.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
7676
options.put("name", testInfo.getDisplayName());
7777
options.put("build", System.getProperty("build.name"));
78-
options.put("seleniumVersion", "4.22.0");
78+
options.put("seleniumVersion", "4.32.0");
7979
return options;
8080
}
8181

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.saucedemo.selenium.sauce_features;
2+
3+
import com.saucedemo.selenium.TestBase;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.time.Duration;
8+
import java.util.ArrayList;
9+
import java.util.Comparator;
10+
import java.util.List;
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.api.TestInfo;
16+
import org.openqa.selenium.By;
17+
import org.openqa.selenium.HasDownloads;
18+
import org.openqa.selenium.chrome.ChromeOptions;
19+
import org.openqa.selenium.support.ui.WebDriverWait;
20+
21+
public class DownloadTest extends TestBase {
22+
23+
@BeforeEach
24+
public void setup(TestInfo testInfo) {
25+
ChromeOptions options = new ChromeOptions();
26+
options.setEnableDownloads(true);
27+
startSession(testInfo, options);
28+
}
29+
30+
@DisplayName("Download File from Sauce")
31+
@Test
32+
public void downloadTest() throws IOException {
33+
List<String> fileNames = new ArrayList<>();
34+
fileNames.add("file_1.txt");
35+
fileNames.add("file_2.jpg");
36+
driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");
37+
driver.findElement(By.id("file-1")).click();
38+
driver.findElement(By.id("file-2")).click();
39+
new WebDriverWait(driver, Duration.ofSeconds(5))
40+
.until(d -> ((HasDownloads) d).getDownloadableFiles().contains("file_2.jpg"));
41+
42+
List<String> files = ((HasDownloads) driver).getDownloadableFiles();
43+
44+
// Sorting them to avoid differences when comparing the files
45+
fileNames.sort(Comparator.naturalOrder());
46+
files.sort(Comparator.naturalOrder());
47+
48+
Assertions.assertEquals(fileNames, files);
49+
String downloadableFile = files.get(0);
50+
Path targetDirectory = Files.createTempDirectory("download");
51+
52+
((HasDownloads) driver).downloadFile(downloadableFile, targetDirectory);
53+
54+
String fileContent =
55+
String.join("", Files.readAllLines(targetDirectory.resolve(downloadableFile)));
56+
Assertions.assertEquals("Hello, World!", fileContent);
57+
}
58+
}

selenium-junit5-examples/src/test/java/com/saucedemo/selenium/selenium_features/DevToolsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ void replaceImage() throws IOException {
352352
Path path = Paths.get("src/test/resources/cat-and-dog.jpg");
353353
byte[] sauceBotImage = Files.readAllBytes(path);
354354
Routable replaceImage =
355-
Route.matching(req -> req.getUri().contains("unsplash.com"))
355+
Route.matching(req -> req.getUri().contains("picsum.photos"))
356356
.to(
357357
() ->
358358
req ->

0 commit comments

Comments
 (0)