Skip to content

Instantly share code, notes, and snippets.

@alisterscott
Created June 15, 2012 11:46
Show Gist options
  • Save alisterscott/2936040 to your computer and use it in GitHub Desktop.
Save alisterscott/2936040 to your computer and use it in GitHub Desktop.
How to check for downloads to complete and get the file name
require 'watir-webdriver'
file_name = nil
download_directory = "#{Dir.pwd}/downloads"
download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
downloads_before = Dir.entries download_directory
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 # custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"
b = Watir::Browser.new :firefox, :profile => profile
b.goto 'https://dl.dropbox.com/u/18859962/hello.csv'
30.times do
difference = Dir.entries(download_directory) - downloads_before
if difference.size == 1
file_name = difference.first
break
end
sleep 1
end
raise "Could not locate a new file in the directory '#{download_directory}' within 30 seconds" if not file_name
puts file_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment