【问题标题】:Is there way to disable CORS check using RemoteWebDriver for SauceLabs有没有办法使用 RemoteWebDriver for SauceLabs 禁用 CORS 检查
【发布时间】:2020-03-25 00:50:39
【问题描述】:

问题说明了一切,我正在尝试在 SauceLabs 上执行一些硒测试,该测试加载了一个发出跨域请求的网页。我在想有没有办法通过代码以独立于平台的方式禁用 CORS。

【问题讨论】:

  • 在您控制的服务器上?是 - 设置适当的标题。在您无法控制的服务器上?信任会挫败目的。您可以设置一个代理,这样您就不会发出 Ajax 请求,尽管您可能需要玩一些游戏,但它仍然可能无法正常工作。
  • @DaveNewton 谢谢,周转很快。 ;)
  • 谷歌没有透露太多关于手头的问题
  • 是你控制的服务器吗?

标签: java selenium automation saucelabs


【解决方案1】:

使用 ChromeDriver / Chrome 组合禁用 时,请检查您可以使用 --disable-web-security 参数。

content_switches.cc中定义为:

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

代码示例:

  • 窗户:

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

注意:如果您需要访问本地文件以进行 AJAX 或 JSON 等开发/测试目的,您可以使用 -–allow-file-access-from-files 标志。


参考文献


结尾

您可以在以下位置找到一些相关讨论:

【讨论】:

  • 谢谢,我最终找到了相同的解决方案。
  • 感谢您添加对我帖子的引用
  • @AlexFilatov 您的帖子对解释主题非常有帮助。感谢您分享宝贵的研究成果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-15
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 2020-10-24
相关资源
最近更新 更多