How to automate basic authentication chrome alert

Viewed 1847

Chrome authentication popup

Have tried several approached to handle it but none worked.

  1. https://user:password@pageaddress.com - doesn't work, chrome launches controlled bu automates test software and authentication pop-up appears anyway.

  2. Adding --disable-blink-features=BlockCredentialedSubresources to Chrome arg and repeat 1'st point - doesn't work, reason the same as in 1'st point.

  3. driver.switchTo().alert.authenticateUsing(new UserAndPassword(user, password)) - here seems like driver doesn't see an alert, have impelented method that checks it and returns false:

    private Alert alert; public boolean isAlertPresent() { try { waitForTimeout(10, TimeUnit.SECONDS); alert = driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; }}

  4. Triggering keyboard or mouse event via selenium Action() doesn't work too.

    chromedriver 2.31 Google Chrome Version 61

Any other ideas ? Maybe some js script ?

2 Answers

Finally - I was able to do it by Robot Framework which is not a solution that I am proud of, but didn't have any other idea.

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

private Robot robot;

public void login(String login, String password) {
    copyValueFromClipboardToInput(login);
    getRobot().keyPress(KeyEvent.VK_TAB);
    getRobot().keyRelease(KeyEvent.VK_TAB);
    copyValueFromClipboardToInput(password);
    getRobot().keyPress(KeyEvent.VK_ENTER);
    getRobot().keyRelease(KeyEvent.VK_ENTER);
}

private void copyValueFromClipboardToInput(String value) {
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(value), null);
    getRobot().keyPress(KeyEvent.VK_CONTROL);
    getRobot().keyPress(KeyEvent.VK_V);
    getRobot().keyRelease(KeyEvent.VK_V);
    getRobot().keyRelease(KeyEvent.VK_CONTROL);
}

private Robot getRobot() {
    if (robot == null) {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
    return robot;
}
Related