File Downloads

info Few of the scenarios listed here may seem unrelated to File downloads at first glance, but they are.

Pdf and Word files

question Why do Pdf and Word files get downloaded through Sahi?
answer PDF and Word files will not be opened by Sahi in a browser. This is because Sahi cannot interact with them since Sahi scripts cannot be injected into these pages.

These files will automatically be downloaded through Sahi to an internal temp location.

You may use _lastDownloadedFileName() to check that the downloaded file name is not null. You may use _saveDownloadedAs() to save the file to your chosen location.

Refer to this section for details.

Iterate a list of wsdl urls

question How can I use Sahi to iterate a list of wsdl urls?
answer The page that lists the wsdls does not contain any javascript, so Sahi script is not injected into the wsdls page. Hence, Sahi will not be able to interact with the page.

The better alternative is to download the file and then process the file.

What you can do is
  1. Add the url pattern to download_urls.txt (Dashboard | Configure). Example:
    .*SampleFile_For_XMLResponse.*[.]xml.*
  2. The file will get downloaded to userdata\temp\download folder.
  3. Then use _saveDownloadedAs() to save the file to a chosen location. Refer to this section for details.
  4. Once the file is saved, you can read it through Java APIs from the Sahi script and deal with it.

Saving an image

question An image is opened in a new browser window. We wish to download it by right clicking on the page and selecting the option to save the image. How can we do it through Sahi?
answer With Sahi, you cannot automate the right click to bring up the context menu. Instead force Sahi to download the file and use _sendHTMLResponseAfterFileDownload to close the page. Refer to this section for details.

File download dialog appears with Save & Open options

question While trying to download a .csv file in Sahi Pro, it displays a file download dialog with Save & Open options, which is the same as what we get in a normal browser outside of Sahi.
answer Ideally you should not get the download dialog at all. The reason you are getting this is because the download is being done by the browser. Rather the download should happen through Sahi. Refer to section If you see a file download dialog during playback, do the following: on this page.

Blank page after download

question After a file has been downloaded, I get a blank page. This page does not contain the injected Sahi header code and hence the Sahi script fails to execute further. How can I handle this?
answer Add _sendHTMLResponseAfterFileDownload(true) before the step that causes the download to occur. At the end of the download, an html page will be displayed with the following details - Name of the file that was downloaded and two links - Back and Close Window. This html page will contain the injected Sahi header code. You can add Sahi code in the Sahi script to click on the Close Window link to close the window.

Refer to this section for details.

Blank page appears

question When I access my app through Sahi, a page shows up as blank. What could be the reason?
answer The page has downloadable content that gets downloaded through Sahi. Hence you see the blank page.

Refer above for details on how to deal with this.

File does not get downloaded to Sahi's temp downloads folder

question I want Sahi to download a particular file. I added a matching url pattern to userdata\config\download_urls.txt. But I see that the file is not getting downloaded to userdata\temp\download folder. It shows up in C:/Users/<my_user_name>/Downloads/. Also, _lastDownloadedFileName() returns null.
answer The file is being downloaded by the browser and not by Sahi as can be seen from the download location. This essentially means that the url pattern added to download_urls.txt is incorrect. Since Sahi did not download the file, _lastDownloadedFileName() returns null. Correct the url pattern added in download_urls.txt.

Downloaded file does not get saved

question I can see the file getting downloaded to userdata\temp\download folder but _lastDownloadedFileName() is being returned as null, and _saveDownloadedAs() fails.
answer
  • If the file does not get downloaded to the temp location, refer to question above
  • If the file download is large, put a _wait() after launching the download and before checking the value of _lastDownloadedFileName(). _lastDownloadedFileName() will return the correct value only after the download to the temp location is completed.
  • If you can see that the file has been fully downloaded to the temp location but still _lastDownloadedFileName() returns null or _saveDownloadedAs() has failed, check if the downloaded file comes from a different domain. If it does, add the following statement before launching the download.
    addToSession("<DOMAIN NAME OF THE DOWNLOADED FILE>");
    Example:
    addToSession("http://downloaddomain.com"); // Here the download url is http. Change downloaddomain.com to your domain.
    addToSession("https://downloaddomain.com"); // Here the download url is https. Change downloaddomain.com to your domain.
    danger NOTE: If it is a https url, it is a MUST to prefix the domain name with "https://".
    Refer to this section for details.