4

Question says it all, I m trying to execute some selenium tests on SauceLabs, the test loads a webpage that makes a cross domain request. I was thinking is there a way to disable CORS, in platform-independent way through code.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
VishalDevgire
  • 4,232
  • 10
  • 33
  • 59

1 Answers1

12

While using ChromeDriver / Chrome combo to disable check you can use the --disable-web-security argument.

sameoriginpolicy

which is defined in content_switches.cc as:

// Don't enforce the same-origin policy. (Used by people testing their sites.)
const char kDisableWebSecurity[]            = "disable-web-security";

Code samples:

  • Windows:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-web-security"); // don't enforce the same-origin policy
    options.addArguments("--disable-gpu"); // applicable to windows os only
    options.addArguments("--user-data-dir=~/chromeTemp"); // applicable to windows os only
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://google.com");
    
  • OSX:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-web-security"); // don't enforce the same-origin policy
    options.addArguments("--user-data-dir=/tmp/chrome_dev_test");
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://google.com");
    
  • Linux

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-web-security"); // don't enforce the same-origin policy
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://google.com");
    

Note: If you need access to local files for development/testing purposes like AJAX or JSON, you can use -–allow-file-access-from-files flag.

allowfileaccess


References


Outro

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352