- 1 year ago
How do I force Capybara to download to the directory I specify and not the default download directory in Firefox?
Comment
1 / 1
To force Capybara to download files to a specific directory in Firefox, you can set the browser.download.dir
and browser.download.folderList
preferences in the Firefox profile used by Capybara. Here's how you can do it:
Create a new Firefox profile with the desired download directory:
- Open Firefox and type
about:profiles
in the address bar. - Click on the "Create a New Profile" button and follow the wizard to create a new profile.
- Choose a name for the profile and a location for the profile folder (where Firefox will store its data).
- Once the profile is created, select it from the list of profiles and click on the "Set as Default Profile" button.
- Open Firefox and type
Configure the download preferences for the new profile:
- Close Firefox if it's open.
- Navigate to the profile folder you chose in step 1.
- Create a new file called
user.js
(if it doesn't already exist) in the profile folder. - Open the
user.js
file with a text editor and add the following lines:
Replacescssuser_pref("browser.download.dir", "/path/to/your/directory");
user_pref("browser.download.folderList", 2);
/path/to/your/directory
with the absolute path of the directory where you want the downloads to be saved.
Use the new Firefox profile with Capybara:
- In your Capybara test setup, create a new Firefox profile object and pass it to the Capybara driver options:
Replacerubyprofile = Selenium::WebDriver::Firefox::Profile.new("/path/to/your/profile/folder")
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
end
/path/to/your/profile/folder
with the absolute path to the profile folder you created in step 1.
- In your Capybara test setup, create a new Firefox profile object and pass it to the Capybara driver options:
With these steps, Capybara will use the custom Firefox profile with the specified download preferences, and downloads will be saved to the directory you specified in the user.js
file.