2013-08-07 10 views
10

chcę uruchomić przeglądarki (FF, Chrome) dla testu z niepełnosprawnym ciasteczek, próbowałem to:Jak wyłączyć ciasteczka wykorzystujące webdriver dla Chrome i FireFox JAVA

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

ale to nie działa ...

Odpowiedz

8

właśnie dostać rozwiązanie dla Firefox:

FirefoxProfile profile = new ProfilesIni().getProfile("default"); 
profile.setPreference("network.cookie.cookieBehavior", 2); 
driver = new FirefoxDriver(profile); 

ale nie wiem, jak zarządzać go z Chrome.

1

Dla Chrome, spróbuj wykonać następujące czynności:

DesiredCapabilities capabilities = DesiredCapabilities.chrome() 
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-local-storage")) 
driver = new ChromeDriver(capabilities); 
4

U można wyłączyć chromowane ciasteczka jak poniżej:

ChromeOptions options = new ChromeOptions(); 
Map prefs = new HashMap(); 
prefs.put("profile.default_content_settings.cookies", 2); 
options.setExperimentalOptions("prefs", prefs); driver = new ChromeDriver(options); 
1

dla IE po works-

aby wyłączyć cookies:

String command = "REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ \" /v  1A10 /t REG_DWORD /d 0X3 /f"; 
Runtime.getRuntime().exec(command); 

, aby włączyć gotowanie tj:

String command = "REG ADD \"HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\ \" /v  1A10 /t REG_DWORD /d 0X1 /f"; 
Runtime.getRuntime().exec(command); 
0

Możesz użyć poniższych fragmentów kodu, aby wyłączyć pliki cookie w przeglądarkach Chrome i Firefox. Jeśli chcesz włączyć obsługę plików cookie, po prostu usuń tę możliwość.

Safari nie obsługuje żadnej możliwości osiągnięcia tego.

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

Dla Firefox:

FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference("network.cookie.cookieBehavior",2); 
caps.setCapability(FirefoxDriver.PROFILE,profile);