我想在测试环境上进行一些Java/Selenium4 4测试自动化,该环境由一个身份验证弹出保护。这不是警报,而是Chrome弹出窗口:屏幕截图。
在我们的框架中,我们有两个选择:
这个设置一直运行良好。然而,这个弹出式窗口证明是一个相当大的挑战。我为选项1找到了一个解决方案:
((HasAuthentication)driver).register(UsernameAndPassword.of("username","password"));
这不适用于选项2,因为显然RemoteWebDriver类没有实现HasAuthentication接口:
java.lang.ClassCastException: class org.openqa.selenium.remote.RemoteWebDriver cannot be cast to class org.openqa.selenium.HasAuthentication (org.openqa.selenium.remote.RemoteWebDriver and org.openqa.selenium.HasAuthentication are in unnamed module of loader 'app')
这意味着这个解决方案不会在我们的CI设置上运行,现在我被卡住了。我看到人们使用自定义Chrome扩展作为解决办法,但我希望有一个更容易/更好的解决方案.
如何使用RemoteWebDriver Chrome驱动程序类与浏览器弹出、Selenium4 4/Java进行交互?
发布于 2022-07-19 17:54:09
public class ChromeDevToolsAugmenterAuthentication {
public static void main(String[] args) throws MalformedURLException,InterruptedException {
ChromeOptions chromeOptions = new ChromeOptions();
WebDriver driver = RemoteWebDriver.builder()
.oneOf(chromeOptions)
.address("http://localhost:4444")
.build();
DevTools devTools = ((HasDevTools) driver).getDevTools();
devTools.createSession();
Augmenter augmenter = new Augmenter();
driver = augmenter
.addDriverAugmentation("chrome",
HasAuthentication.class,
(caps, exec) -> (whenThisMatches, useTheseCredentials) ->
devTools.getDomains()
.network()
.addAuthHandler(whenThisMatches, useTheseCredentials))
.augment(driver);
((HasAuthentication) driver).register(UsernameAndPassword.of("admin", "admin"));
driver.get("https://the-internet.herokuapp.com/basic_auth");
System.out.println(driver.findElement(By.tagName("p")).getText());
driver.quit();
}}
https://stackoverflow.com/questions/66441362
复制相似问题