Chrome 59 和 Selenium/Fluentlenium 的基本身份验证

Chrome 59 and Basic Authentication with Selenium/Fluentlenium

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

我有一个测试正在使用这个功能,但现在已经坏了,所以我试图用一个等待身份验证弹出窗口并填写详细信息的版本替换它。但以下内容不适用于 Chrome(它不会将身份验证弹出窗口视为警报):

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

selenium-only 版本有同样的问题:

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 没有。有没有其他方法?

一个解决方案是 运行 一个透明代理以向 header 注入所需的凭据。

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

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

我确信 Florent B 的解决方案是可行的,但是为了改造旧测试,我发现发布到 的 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/”),并且这种设置凭据的解决方法不会带来同样的风险。

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

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

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)

在此处找到: