Is there way to disable CORS check using RemoteWebDriver for SauceLabs

我只是一个虾纸丫 提交于 2021-02-17 05:54:05

问题


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.


回答1:


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.


References

  • Disable same origin policy in Chrome
  • Disable-web-security in Chrome 48+
  • Run Chrome browser without CORS

Outro

You can find a couple of relevant discussions in:

  • Uncaught DOMException: Blocked a frame with origin “http://localhost:8080” from accessing a cross-origin frame while listing the iframes in page
  • Error: Permission denied to access property “x” due to same/cross origin policy using Selenium?


来源:https://stackoverflow.com/questions/59104516/is-there-way-to-disable-cors-check-using-remotewebdriver-for-saucelabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!