How can I add profile preferences to Chrome for Selenium Grid 2 in C#?

Maverick picture Maverick · Mar 5, 2014 · Viewed 9.6k times · Source

This is how I add profile preferences to Chrome for Local Auto-test runs and TeamCity(CI):

Capabilities = DesiredCapabilities.Chrome();

var chromeOptions = new ChromeOptionsWithPrefs();
chromeOptions.AddUserProfilePreference("download.default_directory", DownloadPath);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");

return new ChromeDriver(chromeDriverPath, chromeOptions);

But when I create new 'RemoteWebDriver', I must send it a hub URL and 'Capabilities', this way I send profile preferences to Firefox (to RemoteWebDriver):

var profile = new FirefoxProfile();

Capabilities = DesiredCapabilities.Firefox();

profile.SetPreference("browser.helperApps.alwaysAsk.force", false); 
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", DownloadPath);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk",
   "application/zip, application/octet-stream");

Capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());

return Capabilities;

Can someone help me, I need to do the same thing to Chrome as I did to Firefox. Basically what I need, is that I could change the default path for downloading files.

Answer

JimEvans picture JimEvans · Mar 5, 2014

You'll want to do something like the following:

var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", DownloadPath);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");

IWebDriver driver = new RemoteWebDriver(new Uri("http://path/to/selenium/server"), chromeOptions.ToCapabilities());