Skip to content

Support for downloads

nico78 edited this page Dec 14, 2014 · 5 revisions

If what you want to test is not on the page but actually something that can be downloaded (such as a CSV file representation of some data obtained when a user clicks "Download as CSV") then you can use the assertDownload method, which takes:

  • a Finder for the url to download the file from
  • A factory which can take the downloaded file and parse it into some object
  • A matcher for that object

After completion the file will be deleted.

e.g., downloading a csv file from a button parsing the data as a table and using the table matchers:

browser.assertDownload(
            downloadLinkFromJavascriptOnClick(button("Download as CSV")),    //finder for a url, from a button
            parsedAsCSVTable(),   //factory that takes a file and parses as table
            hasDataRows(          //table matcher
                rowIncluding(
                    valueInColumn("Stock Name", "Vodafone"),
                    valueInColumn("Quantity", "10"),
                    valueInColumn("Change", "0.30"),
                    valueInColumn("Value", "10.00"))));

Here, downloadLinkFromJavascriptOnClick is a SFinder<String, SearchContext> that looks for the String "onclick" attribute value within the WebElement found by another finder - in this case the csv download button.