可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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...
回答1:
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.
回答2:
U can disable chrome cookies as below:
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:
For Chrome try the following:
DesiredCapabilities capabilities = DesiredCapabilities.chrome() capabilities.setCapability("chrome.switches", Arrays.asList("--disable-local-storage")) driver = new ChromeDriver(capabilities);
回答4:
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);
回答5:
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 prefs = new HashMap(); Map profile = new HashMap(); Map contentSettings = new HashMap(); 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);