2013-08-07 11 views
10

voglio lanciare i browser (FF, CHROME) per il test con i biscotti disabili, ho provato questo:come disabilitare i cookie utilizzando WebDriver per Chrome e 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); 

ma non un lavoro ...

risposta

8

ho appena ottenere soluzione per Firefox:

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

ma non so come gestire con Chrome.

1

Per Chrome provare quanto segue:

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

U può disattivare i cookie Chrome come di seguito:

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

Per IE seguente works-

per disabilitare biscotto:

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

per abilitare la cottura es .:

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

È possibile utilizzare i frammenti di codice seguenti per disabilitare i cookie nei browser Chrome e Firefox. Se si desidera abilitare i cookie, è sufficiente rimuovere la capacità.

Safari non supporta alcuna funzionalità per ottenere ciò.

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

Per Firefox:

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