Saturday, November 21, 2015

Handle "Authentication Required" Window using Selenium

    I found several ways on web to handle these window using Selenium WebDriver.
I try to list all here:


A) The Alert Method, authenticateUsing() to skip the Http Basic Authentication box

WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("USERNAME", "PASSWORD"));



B) Pass the credentials in the URL.

driver.get("http://UserName:Password@Example.com"); 
 
C) By using "Robot" class from "java.awt.Robot".

    WebDriver driver = new FirefoxDriver();
    driver.get("http://<your_url>");
    Alert authenticationWindow = driver.switchTo().alert();

    try 
    {
        Robot robot = new Robot();
        authenticationWindow.sendKeys("username");

        robot.keyPress(KeyEvent.VK_TAB);//Jump to password input

        robot.keyPress(KeyEvent.VK_T);
        robot.keyPress(KeyEvent.VK_E);
        robot.keyPress(KeyEvent.VK_S);
        robot.keyPress(KeyEvent.VK_T);

        robot.keyPress(KeyEvent.VK_ENTER);
    } 
    catch (AWTException e) {
      e.printStackTrace();
    }
 
 
 
 

No comments:

Post a Comment