2013-08-07 18 views
10

Tôi muốn khởi động trình duyệt (FF, CHROME) cho thử nghiệm với cookie tàn tật, tôi đã cố gắng này:làm thế nào để vô hiệu hóa cookie sử dụng webdriver dành cho Chrome và 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); 

nhưng nó không hoạt động ...

Trả lời

8

Tôi vừa nhận được giải pháp cho Firefox:

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

nhưng tôi không biết cách quản lý nó bằng Chrome.

1

Đối với Chrome hãy thử như sau:

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

U có thể tắt cookie chrome như sau:

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

Đối với IE sau works-

để vô hiệu hóa 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); 

để cho phép nấu ăn tức là:

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

Bạn có thể sử dụng đoạn mã bên dưới để tắt cookie trong trình duyệt Chrome và Firefox. Nếu bạn muốn bật cookie, chỉ cần xóa khả năng.

Safari không hỗ trợ bất kỳ khả năng nào để đạt được điều này.

Đối với 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); 

Đối với Firefox:

FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference("network.cookie.cookieBehavior",2); 
caps.setCapability(FirefoxDriver.PROFILE,profile); 
Các vấn đề liên quan