【问题标题】:Chrome 59 and Basic Authentication with Selenium/FluentleniumChrome 59 和使用 Selenium/Fluentlenium 的基本身份验证
【发布时间】:2017-11-16 11:39:42
【问题描述】:

Chrome 59 有removed support for https://user:password@example.com URLs

我有一个使用此功能的测试,该功能现已损坏,因此我尝试将其替换为等待身份验证弹出窗口并填写详细信息的版本。但是以下内容在 Chrome 上不起作用(它不会将身份验证弹出窗口视为警报):

alert().authenticateUsing(new UserAndPassword("test", "test"));

只有 selenium 的版本有同样的问题:

WebDriverWait wait = new WebDriverWait(getDriver(), 10);      
Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
alert.authenticateUsing(new UserAndPassword("test", "test"));

(基于此处给出的答案:How to handle authentication popup with Selenium WebDriver using Java

我可以看到在 FireFox 中处理此问题的几种解决方法,但对于 Chrome 则没有。有什么替代方法吗?

【问题讨论】:

    标签: java google-chrome selenium fluentlenium


    【解决方案1】:

    我确信 Florent B 的解决方案是可行的,但是为了改造旧测试,我发现发布到 this duplicate question 的 zoonabar 的解决方案更容易实现,需要的代码要少得多,并且不需要对测试进行特殊准备盒子。对于查看代码的新开发人员来说,似乎也更容易理解。

    简而言之:在访问被测 URL(没有凭据)之前访问任何带有凭据的 URL 都会导致浏览器记住凭据。

    goTo("http://user:password@localhost"); // Caches auth, but page itself is blocked
    goTo("http://localhost"); // Uses cached auth, page renders fine
    // Continue test as normal
    

    这可能感觉像是浏览器中的一个漏洞,将被修补,但我认为这不太可能;该限制已被施加以避免网络钓鱼风险(选择的用户名看起来像一个域,例如“http://google.com:long-token-here-which-makes-the-real-domain-disappear@example.com/”),并且这种设置凭据的解决方法不会带来相同的风险。

    See zoonabar's answer

    【讨论】:

    • 我的浏览器两次请求凭据。它将通过您的解决方案传递第一个请求,但我如何填写第二个请求?发送 goTo("user:password@localhost"); 两次都没有用...
    【解决方案2】:

    一种解决方案是运行一个透明代理来注入带有所需凭据的标头。

    但另一个更简单的解决方案是创建一个小型扩展来自动设置凭据:

    https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46

    【讨论】:

      【解决方案3】:

      https://bugs.chromium.org/p/chromium/issues/detail?id=435547#c33 中,您可以看到一个 mkwst 说存在关于基本身份验证凭据的错误,并且同源站点使其稳定。

      如果您使用“--disable-blink-features=BlockCredentialedSubresources”或转至 Chrome Canary 版本,您可能会发现您看到的原始问题不再发生...

      【讨论】:

        【解决方案4】:

        Florent B. 在 chrome 扩展的帮助下找到了一个解决方案,该扩展是在 selenium 测试中动态添加的。如果需要,扩展处理基本身份验证凭据:

        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File("C:/path_to/credentials_extension.zip"));
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), options);
        

        Chrome 扩展代码: https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46
        (只需修改 background.js 中的用户名和密码,然后将文件 background.js 和 manifest.json 压缩到 credentials_extension.zip)

        在这里找到:Selenium - Basic Authentication via url

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-16
          • 1970-01-01
          • 1970-01-01
          • 2011-01-28
          • 1970-01-01
          • 2011-03-22
          • 1970-01-01
          • 2014-09-15
          相关资源
          最近更新 更多