how to disable cookies using webdriver for Chrome and FireFox JAVA

前端 未结 6 1932
独厮守ぢ
独厮守ぢ 2020-12-01 16:55

I want launch browsers(FF, CHROME) for test with disabled cookies, I tried this:

           service =
                    new ChromeDriverService.Builder()
          


        
6条回答
  •  余生分开走
    2020-12-01 16:57

    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);
    

提交回复
热议问题