Как отключить файлы cookie с помощью webdriver для Chrome и FireFox JAVA
Я хочу запускать браузеры (FF, CHROME) для тестирования с отключенными кукисами, я пробовал это:
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);
но он не работает...
Ответы
Ответ 1
Я только что получил решение для Firefox:
FirefoxProfile profile = new ProfilesIni().getProfile("default");
profile.setPreference("network.cookie.cookieBehavior", 2);
driver = new FirefoxDriver(profile);
но я не знаю, как управлять им с Chrome.
Ответ 2
U может отключить хром-куки, как показано ниже:
ChromeOptions options = new ChromeOptions();
Map prefs = new HashMap();
prefs.put("profile.default_content_settings.cookies", 2);
options.setExperimentalOptions("prefs", prefs); driver = new ChromeDriver(options);
Ответ 3
Для Chrome попробуйте выполнить следующее:
DesiredCapabilities capabilities = DesiredCapabilities.chrome()
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-local-storage"))
driver = new ChromeDriver(capabilities);
Ответ 4
Для IE следующие работы -
отключить файл 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);
чтобы включить файл 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);
Ответ 5
Вы можете использовать приведенные ниже фрагменты кода для отключения файлов cookie в браузерах Chrome и Firefox. Если вы хотите включить файлы cookie, просто удалите эту возможность.
Safari не поддерживает какие-либо возможности для достижения этой цели.
Для 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);
Для Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.cookie.cookieBehavior",2);
caps.setCapability(FirefoxDriver.PROFILE,profile);