I want launch browsers(FF, CHROME) for test with disabled cookies, I tried this:
service =
new ChromeDriverService.Builder()
You can use the below code snippets to disable the cookies in the Chrome and Firefox browsers. If you wish to enable cookies, just remove the capability.
Safari doesn't support any capability to achieve this.
For Chrome:
DesiredCapabilities caps = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
Map<String, Object> profile = new HashMap<String, Object>();
Map<String, Object> contentSettings = new HashMap<String, Object>();
contentSettings.put("cookies",2);
profile.put("managed_default_content_settings",contentSettings);
prefs.put("profile",profile);
options.setExperimentalOption("prefs",prefs);
caps.setCapability(ChromeOptions.CAPABILITY,options);
WebDriver driver = new ChromeDriver(caps);
For Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.cookie.cookieBehavior",2);
caps.setCapability(FirefoxDriver.PROFILE,profile);
There is a slight change in the prefs for current chrome version.
I am currently using Chrome version 70.
In Java 11.
var chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--incognito", "start-maximized"); // incognito and maximized.
var prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.cookies", 2);
chromeOptions.setExperimentalOption("prefs", prefs);
var webDriver = new ChromeDriver(chromeOptions);
According to the official doc,
DesiredCapabilities
is deprecated for java, so just use ChromeOptions
.
Quoted from the doc.
prefs : dictionary
A dictionary with each entry consisting of the name of the preference and its value. These preferences are only applied to the user profile in use. See the 'Preferences' file in Chrome's user data directory for examples.
Preferences file is C:\Users\user name\AppData\Local\Google\Chrome\User Data\Default\Preferences
I just played with settings in my chrome and saw what was changing in the file.
For Chrome try the following:
DesiredCapabilities capabilities = DesiredCapabilities.chrome()
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-local-storage"))
driver = new ChromeDriver(capabilities);
I've just get solution for Firefox:
FirefoxProfile profile = new ProfilesIni().getProfile("default");
profile.setPreference("network.cookie.cookieBehavior", 2);
driver = new FirefoxDriver(profile);
but I don't know how to manage it with Chrome.
You can disable Chrome cookies as below:
Map prefs = new HashMap();
prefs.put("profile.default_content_settings.cookies", 2);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOptions("prefs", prefs);
driver = new ChromeDriver(options);
For IE following works-
to disable cookie:
String command = "REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ \" /v 1A10 /t REG_DWORD /d 0X3 /f";
Runtime.getRuntime().exec(command);
to enable cookie:
String command = "REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ \" /v 1A10 /t REG_DWORD /d 0X1 /f";
Runtime.getRuntime().exec(command);