Not able to handle HasAuthentication popup with Remote driver

176 views
Skip to first unread message

Pavan Washimkar

unread,
Aug 5, 2022, 12:17:14 PM8/5/22
to Selenium Users
Hi Team,
   
    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)
   
   
    Test Class code:

import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import org.openqa.selenium.By;
import org.openqa.selenium.HasAuthentication;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

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();
            System.out.println("Able to launch the browser :: " + success);

            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;
    }

}
Reply all
Reply to author
Forward
0 new messages