5

I learned how to use Firefox 4 with watir and webdriver (on Win7 x64), setting profile items. Example:

profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.useDownloadDir"] = true
profile["browser.download.dir"] = 'D:\\FirefoxDownloads'
profile["browser.helperApps.neverAsk.saveToDisk"] = "application/csv"
driver = Selenium::WebDriver.for :firefox, :profile => profile
browser = Watir::Browser.new(driver)

What I try to do with the example below, is setting CSV files to be always downloaded to a specific directory, never opened. The code above succeeds in setting all the files automatically downloaded to the specified directory, but setting browser.helperApps.neverAsk.saveToDisk has no effect: I still get the open/save question. After the script runs, the Firefox window is still open, and I enter the URL about:config. I can see that browser.helperApps.neverAsk.saveToDisk was correctly set to application.csv , but in firefox/options/options/applications I don't see the entry for CSV files. It seems that the menu setting, that is really effective, is not really bound with the about:config setting. What am I doing wrong?

Alister Scott
  • 3,675
  • 24
  • 41
carlo.borreo
  • 1,344
  • 2
  • 18
  • 35
  • Someone with the rep to do it should probably add the firewatir tag to this question also. – Chuck van der Linden Mar 29 '11 at 16:41
  • 1
    Well, this is not related to firewatir gem, it can not drive Firefox 4 and it does not use webdriver. – Željko Filipin Mar 30 '11 at 09:44
  • ahhh ok sorry, for some reason I thought webdriver was used with firewatir. Thanks for setting me straight on that. – Chuck van der Linden Mar 30 '11 at 15:43
  • No. Firewatir is one gem, watir-webdriver another one. One does not need the other one. Both can drive Firefox, but firewatir can not drive Firefox 4. – Željko Filipin Mar 31 '11 at 08:46
  • I've just asked the guru (Jari) at #seconf and he suggested your content/type may be wrong, therefore not triggering that particular rule... Can you double check the content type "application/csv" – Tim Koopmans Apr 05 '11 at 23:12
  • I tried with both application/csv and application.csv When I tell a "normal" Firefox to remember my choice, the about:config item becomes application.csv , and it works. But on a Watir-webdriver created Firefox it does not work What is #seconf and how do I reach it? – carlo.borreo Apr 06 '11 at 08:30
  • CORRECTION: I tried with both application/csv and application.csv When I tell a "normal" Firefox to remember my choice, the about:config item `browser.helperApps.neverAsk.saveToDisk` stays empty, but it works. But on a Watir-webdriver created Firefox it does not work What is #seconf and how do I reach it? – carlo.borreo Apr 06 '11 at 09:21
  • 2
    #seconf was the selenium conference we were all just at ... What I meant was, the content type returned by the server may not be application/csv ... it might be text/csv for example ... it would be worth double checking in firebug->net->response headers what exactly the content type is being returned by the server ... – Tim Koopmans Apr 08 '11 at 03:07
  • Can you please help me in this? I have the web page with the link to the CSV file, I have firebug open, I go on the net panel, enable it, and then? How do I see the response headers? – carlo.borreo Apr 08 '11 at 09:06
  • Open firebug and go to the Net panel, enable it, then watch the traffic as you download the file. It will probably be some type of GET or POST. Expand the [+] for the relevant http method and look at the headers tab. In here you will see Response Headers. If you're looking at the right one, you should see fields like Date, Expires, Cache-Control, Content-Type, Content-Encoding as so forth. Note, we're interested in the Content-Type field of the response (not the request). – Tim Koopmans Apr 08 '11 at 16:08
  • I was searching for not only how to auto-save CSV downloads, overwriting without prompting, but to be able to specify the filename in advance from my watir-webdriver ruby script. Will keep researching.. – Marcos Jan 14 '12 at 18:56

2 Answers2

12

I've done some testing of this for you, unfortunately there doesn't seem to be a standard content-type for CSV files. You can try passing a comma separated list of content-types, hopefully one of those work for you. For me it was application/octet-stream that did the trick...

require 'watir-webdriver'
require 'selenium-webdriver'

profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.useDownloadDir"] = true
profile["browser.download.dir"] = '/tmp'
profile["browser.helperApps.neverAsk.saveToDisk"] = "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream"
driver = Selenium::WebDriver.for :firefox, :profile => profile
browser = Watir::Browser.new(driver)

browser.goto "http://altentee.com/test/test.csv"
Tim Koopmans
  • 788
  • 4
  • 9
4

In Firefox 6+, I couldn't get this to work without specifically setting the 'browser.download.folderList' value:

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/csv"
b = Watir::Browser.new :firefox, :profile => profile

See: http://watirwebdriver.com/browser-downloads/

Alister Scott
  • 3,675
  • 24
  • 41