Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable cookies using webdriver for Chrome and FireFox JAVA

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...

like image 861
Roman Iuvshin Avatar asked Aug 07 '13 14:08

Roman Iuvshin


2 Answers

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);
like image 113
Roman Iuvshin Avatar answered Sep 27 '22 21:09

Roman Iuvshin


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);
like image 28
user3090371 Avatar answered Sep 27 '22 23:09

user3090371