Serenity BDD does not have a specific way to download a file. You will have to use driver options, similar to what you have usually been doing.
There is A short guide on how to configure ChromeDriver in Serenity BDD. Firefox and other browsers have a similar approach.
Setting these options in serenity.properties
might not be ideal. First, everything in this file must be static - you cannot have different values based on OS or environment. Second, download paths (for example in Chrome the chrome_preferences.download.default_directory
option) must be global - you might not know what this value will be at run-time. A potential solution is to set these in your pom; something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.plugin.version}</version>
<configuration>
<includes>
<include>features.*.*Story</include>
</includes>
<systemPropertyVariables>
<chrome_preferences.download.prompt_for_download>false</chrome_preferences.download.prompt_for_download>
<chrome_preferences.download.default_directory>${project.build.directory}/downloads</chrome_preferences.download.default_directory>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
The above will always use a known location to download files, regardless if you are running on Window or Linux, or on your local machine or on Jenkins. To further parameterize this, you could use Maven Profiles.
The next problem will be to find the file you just downloaded, so that you can do something with it. I tried the solutions discussed here, but I was not able to get those to work in headless mode.
Eventually I came up with this Chrome-only solution, which should work whether you are running through Maven or just debugging in your IDE. Firefox and other browsers will use different locations - the downloads
location below.
public class DownloadedFile implements Question<File> {
private static final Logger log = LoggerFactory.getLogger(DownloadedFile.class);
private DownloadedFile() { }
@Override
public File answeredBy(Actor actor) {
File downloads;
if (System.getProperty("chrome_preferences.download.default_directory") != null)
downloads = new File(System.getProperty("chrome_preferences.download.default_directory"));
else if (System.getProperty("headless.mode") != null && System.getProperty("headless.mode").contentEquals("true"))
downloads = new File(System.getProperty("user.dir"));
else
downloads = new File(System.getProperty("user.home") + "/Downloads");
log.debug("searching: " + downloads.getPath());
// credit: https://stackoverflow.com/a/286001/3124333
File[] files = downloads.listFiles(File::isFile);
File chosenFile = null;
long lastModifiedTime = Long.MIN_VALUE;
if (files != null) {
for (File file : files) {
if (file.lastModified() > lastModifiedTime) {
chosenFile = file;
lastModifiedTime = file.lastModified();
}
}
}
log.debug("found: " + chosenFile.getAbsoluteFile());
return chosenFile;
}
public static Question<File> filename() {
return new DownloadedFile();
}
}