I am using Watir-Webdriver with Firefox and the method recommended on the watirwebdriver.com site to automate file downloads. This involves setting FireFox about:config parameters to disable the download dialog in FireFox for specific file types. This works well but now I am trying to figure out how best to determine when the file download has completed (some take a few seconds, some take minutes) so I can logout of the site and move on to the next test. It seems since there are no visual clues left in the browser I may have to monitor the file in the download directory. Any options would be appreciated.
There is no built-in to selenium way to wait for the download to be completed. The general idea here would be to wait until a file would appear in your "Downloads" directory. Hope this helps!!
Watir is an open-source web application testing framework that is designed to make writing Selenium tests simple and efficient. Built on Selenium's Ruby language bindings, Watir is able to drive the browser in the same way humans do.
Once the download is completed, we can verify it with the help of the os. path. isfile method. The path of the downloaded file is passed as a parameter to that method.
I had a similar task where I wanted to extract the contents of a downloaded PDF file. I used to following solution:
t = ''
(0..19).each do
sleep 5
t = `pdftotext -raw some_directory/*.pdf -`
break if $?.success?
end
It does 20 attempts at extracting the text using the shell command pdftotext and will break out of the block if the shell command was successful. The advantage of doing it this way is that if the file doesn't exist or if the file is only partially downloaded it will produce an error and then try again. If your file is not a PDF or you don't care about the contents then you use another shell command instead of pdftotext, so long as it produces an error if the file is incomplete.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With