11

On a website I am testing we have various links that download PDF files to the users computer.

Our selenium suite runs nightly on bamboo, if we were to download the various files overnight it would cause the hard disk to fill up in short order. The Selenium job itself is sandboxed and can't access the file system to delete the file.

What is best practice for testing file downloads with this setup?

A few potential scenarios;

  1. Download the file and perform some kind of cron cleanup serverside (this might not be viable).
  2. Check the URL of the file location is as expected but don't download the file
  3. Click the link and accept the presence of a dialogue box as sufficient coverage (without downloading the file)
2
  • 1
    Duplicate:sqa.stackexchange.com/questions/2197/… Top-voted answer there should give you a lot of information!
    – user16096
    Jan 17, 2016 at 16:19
  • This is a different question, its not about the actual download, more about best practises when testing file downloads. Do you want to compare the actual file with a previous one or is just checking the link enough. Mar 21, 2016 at 14:59

5 Answers 5

6

Using chrome I automate downloading the file, and then open the chrome://downloads page to retrieve the downloaded files list from shadow DOM like this:

const docs = document
  .querySelector('downloads-manager')
  .shadowRoot.querySelector('#downloads-list')
  .getElementsByTagName('downloads-item');

This solution is restrained to chrome, the data also contains information like file path and download date.

Not sure about data cleanup, I'm currently doing it in Bamboo without problems.

I also use a timeout polling the download results to give the download time to complete.

If testing downloads through the browser is not necessary, then a program to test the website for broken links would do.

3

I agree with KirbyScope I wouldn't test this with Selenium at all.

I would use an http client to make the necessary requests to authorise and then GET the PDF and then assert on the response and content return to ensure it was a valid PDF.

You could do this either programmatically or with a tool like JMeter (and have your JMeter script run via command line)

2

@ Adurbe

  • load the page with the download link, find the element on the page, click on it.
  • driver.findElement(By.id(“foo”)).sendKeys(“AbsoluteFileLocation”);
  • < input type="file">
  • DELETE the downloaded file
  • File downloadedFile = new File(<fileLocation>); downloadedFile.delete();
1
  • While I didn't use your exact solution, the notion of using delete has a great build-in assumption of the file existing in order to be deleted. Upvote!
    – QualiT
    May 20, 2020 at 5:24
0

The best way to handle download functionality and pop-ups, is to use AutoIT with Selenium for testing download/upload files functionality (hope this is what you are asking). This link will provide you step-by-step guidelines over how easily you can use AutoIT with Selenium for such functionalities.

Actually, downloading and uploading files is related to windows/desktop functionality and this is where territory of Selenium shows issues, but with the help of AutoIT (Desktop testing tool) you can handle these windows pop-ups and manage your downloads too.

You will find some information here and over SO, on how you can handle download functionality with Selenium itself (but unfortunately, this didn't work for me, because of my machine issue and so I switched to AutoIT).

1
  • 2
    The bamboo box this runs on is linux based while we have windows desktops. Although a decent idea, I don't think it will be viable in our setup.
    – ECiurleo
    Oct 29, 2015 at 15:17
0

What is best practice for testing file downloads? Assert the (static link) URL on click is correct?

Selenium can't interact with anything outside of the browser, unless you incorporate AutoIT like @Dhiman pointed out.

What are you trying to assert here? Is the link working? You could extract the url and hit it, then make sure the response is 200 (OK). Or, just make sure the link is what you expected.

1
  • 1
    thats actually part of the basis of the question. What is the best practice/compromise to test this scenario?
    – ECiurleo
    Oct 30, 2015 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.