Reputation: 1912
I want launch browsers(FF, CHROME) for test with disabled cookies, I tried this:
service =
new ChromeDriverService.Builder()
.usingDriverExecutable(new File("src/test/resources/chromedriver"))
.usingAnyFreePort().build();
try {
service.start();
} catch (IOException e1) {
e1.printStackTrace();
}
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("disable-restore-session-state", true);
driver = new ChromeDriver(service, capabilities);
but it's not work...
Upvotes: 15
Views: 40656
Reputation: 1912
I've just get solution for Firefox:
FirefoxProfile profile = new ProfilesIni().getProfile("default");
profile.setPreference("network.cookie.cookieBehavior", 2);
driver = new FirefoxDriver(profile);
for Chrome (block all cookies)
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.cookies", 2);
for Chrome (block only third party cookies)
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.cookies", 1);
options.AddUserProfilePreference("profile.cookie_controls_mode", 1);
Upvotes: 13
Reputation: 1334
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.
Upvotes: 1
Reputation: 99
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);
Upvotes: 4
Reputation: 1057
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);
Upvotes: 2
Reputation: 99
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);
Upvotes: 1
Reputation: 7441
For Chrome try the following:
DesiredCapabilities capabilities = DesiredCapabilities.chrome()
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-local-storage"))
driver = new ChromeDriver(capabilities);
Upvotes: 1