使用Selenium / Fluentlenium进行Chrome 59和基本身份验证

时间:2017-06-14 10:53:20

标签: java google-chrome selenium fluentlenium

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

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

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

仅限硒的版本具有相同的问题:

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没有。有没有替代方法?

4 个答案:

答案 0 :(得分:10)

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

简而言之:在访问测试中的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

答案 1 :(得分:2)

一种解决方案是运行透明代理以使用所需凭据注入标头。

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

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

答案 2 :(得分:0)

resample中,您可以看到mkwst说有关于基本身份验证凭据的错误,并且相同的原始网站使其稳定。

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

答案 3 :(得分:0)

Florent B.在铬扩展的帮助下找到了一种解决方案,该解决方案是在硒测试中即时添加的。如果需要,扩展将处理基本身份验证凭据:

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文件压缩为凭据_extension.zip)

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

相关问题