2

I am new to Serenity BDD. Usually what I do is to set the firefox preferences.

        FirefoxProfile   profile = new FirefoxProfile();

            //Set Location to store files after downloading.
        profile.setPreference("browser.download.dir", "D:\\WebDriverDownloads");
        profile.setPreference("browser.download.folderList", 2);

        //Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"); 

        profile.setPreference( "browser.download.manager.showWhenStarting", false );
        profile.setPreference( "pdfjs.disabled", true );

But in Serenity where and how can I use these profiles? Is there any way for me to set the preferences in serenity.properties file?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
LoneWarrior
  • 51
  • 2
  • 7
  • 1
    Take a look at the Serenity documentation for firefox profiles: http://www.thucydides.info/docs/serenity/#_serenity_webdriver_support_in_junit. There is a section for configuring the webdriver.firefox.profile and firefox.preferences. – smit9234 Jul 17 '17 at 21:57
  • I have read those. I am actually new to automation and coding. So what I did was using serenity jbehave archtype, added a project. It had stories under src/test/resources folder. Added stories there and later mapped those stories to the java code using jbehave. I am still confused where to add those code for firefox profile. – LoneWarrior Jul 19 '17 at 05:46
  • 1
    Finally done by adding preferences in pom.xml file – LoneWarrior Jul 19 '17 at 11:46
  • Just had to do this the other day. This question was the first hit in Google, but no answer. Find my solution below. ;) – SiKing Aug 06 '20 at 20:37

1 Answers1

0

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();
    }
}
SiKing
  • 10,003
  • 10
  • 39
  • 90