Reputation: 41
I was trying to handle authetication popup and is working fine in local (when we create webdriver instance with chromeDriver). But if we try with RemoteWebDriver, we are getting below mentioned exception.
Our problem is to handle authentication pop up in ci environment using RemoteWebDriver.
Request you to guide for the same.
Error:
INFO: Using OpenTelemetry for tracing
Aug 05, 2022 3:44:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected upstream dialect: W3C
Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.HasAuthentication
at domain.TestWithRemoteDriver.main(TestWithRemoteDriver.java:47)
public class TestWithRemoteDriver {
public static void main(String[] args) throws MalformedURLException {
Boolean isGridEnable = true;
WebDriver webDriver;
if (isGridEnable) {
ChromeOptions chromeOptions = getChromeDesiredCapabilities();
String seleniumRemoteUrl = "http://localhost:4444/wd/hub";
webDriver = new RemoteWebDriver(new URL(seleniumRemoteUrl), chromeOptions);
} else {
WebDriverManager.getInstance(DriverManagerType.CHROME).setup();
webDriver = new ChromeDriver();
}
((HasAuthentication) webDriver).register(UsernameAndPassword.of("admin", "admin"));
webDriver.get("https://the-internet.herokuapp.com/basic_auth");
String success = webDriver.findElement(By.xpath("//*[@id=\"content\"]/div/p")).getText();
webDriver.quit();
}
private static ChromeOptions getChromeDesiredCapabilities() {
ChromeOptions chromeOptions = new ChromeOptions();
HashMap<String, Object> chromePrefs = new HashMap<>();
chromeOptions.addArguments("--start-fullscreen");
chromeOptions.addArguments("--window-size=1920,1080");
chromeOptions.addArguments("--start-maximized");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.setExperimentalOption("prefs", chromePrefs);
return chromeOptions;
}
}
Upvotes: 3
Views: 1280
Reputation: 430
Please try Selenium 4 bi directional api:
((HasAuthentication) webDriver) .register(() -> new UsernameAndPassword("admin", "admin"));
https://saucelabs.com/resources/articles/bidirectional-apis
Upvotes: 1