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.
While using ChromeDriver / Chrome combo to disable cors check you can use the --disable-web-security
argument.
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.
You can find a couple of relevant discussions in:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With