Webdriver - HTTP 身份验证对话框

Webdriver - HTTP authentication dialog

我有一个非常简单的 selenium-webdriver 脚本。我想使用 webdriver 进行 HTTP 身份验证。

脚本:

WebDriver driver = new FirefoxDriver();  
driver.get("http://www.httpwatch.com/httpgallery/authentication/");
driver.findElement(By.id("displayImage")).click();
Thread.sleep(2000);
driver.switchTo().alert().sendKeys("httpwatch");

问题:

driver.switchTo().alert().sendKeys("httpwatch");

投掷

org.openqa.selenium.NoAlertPresentException:没有警报

问题:

编辑

警报有以下方法,似乎尚未实现。

driver.switchTo().alert().authenticateUsing(new UsernameAndPassword("username","password"))

问题是 这不是 javascript 弹出窗口 因此您不能通过 selenium 的 alert().

来操作它

如果 AutoIt 和提交凭据都在 URL(最简单的选项 - 只需打开 the url and click "Display Image") are not options for you, another approach could be to use AutoAuth firefox addon 即可自动提交之前保存的凭据:

AutoAuth automatically submits HTTP authentication dialogs when you’ve chosen to have the browser save your login information. (If you’ve already told the browser what your username and password are, and you’ve told it to remember that username and password, why not just have it automatically submit it instead of asking you each time?)

遵循 主题中建议的答案:

  • Install AutoAuth Firefox plugin;
  • Visit the site where the authentication is needed. Enter your username and password and make sure to choose to save the credentials;
  • Save AutoAuth installation file at your hard drive: at the plugin page, right click at “Add to Firefox” and “Save link as”;
  • Instantiate Firefox webdriver as following:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi");
firefoxProfile.addExtension(pluginAutoAuth);
driver = new FirefoxDriver(firefoxProfile);

此外,以类似于 AutoIt 选项的方式 - 您可以使用 sikuli 屏幕识别和自动化工具在弹出窗口中提交凭据。


另请参阅其他想法和选项:

Basic/NTLM 身份验证 pop-up 是一个浏览器对话框 window。 WebDriver (Selenium 2.0) 无法与此类对话框交互 windows。这样做的原因是因为 WebDriver 仅旨在模仿用户与网站的交互,而浏览器对话框 windows 目前不在该范围内。 JavaScript 对话框 windows 是网站的一部分,因此 WebDriver 可以处理这些。在 Selenium 1.0 中,可以进行基本身份验证。

那么如何解决这个问题呢? 1) 通过 URL http://username:password@website.com 进行身份验证 2) 使用将处理 Basic/NTLM 身份验证的浏览器插件 3) 使用将修改请求 header 并传递username/password 和 4) 使用机器人,例如 AutoIt,或一些 Java 库。

选项 1:最简单,对 system/test 的影响最小。选项 2:作为您的加载插件,对浏览器的影响很大。此外,每个浏览器都使用自己的插件,并且有可能某个浏览器所需的插件不可用。选项 3:适用于 HTTP,但 HTTPS 需要自定义证书,因此并不总是一个选项。对系统和测试都没有太大影响。选项 4:模仿键盘按键,这是一种可行的解决方案,但容易出错。仅当对话框 window 具有焦点时才有效,但情况可能并非总是如此。

我遇到了同样的问题,并使用机器人 class 得到了一些具体的解决方案。它的解决方法或解决方案,让我们看看,但它有效。

public class DialogWindow implements Runnable {

@Override
public void run() {
    try {
        entercredentials();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void entercredentials()  InterruptedException {
    Thread.sleep(5000);
    try {
        enterText("username");
        enterSpecialChar(KeyEvent.VK_TAB);
        enterText("password");
        enterSpecialChar(KeyEvent.VK_ENTER);

    } catch (AWTException e) {

    }
}

private void enterText(String text) throws AWTException {
    Robot robot = new Robot();
    byte[] bytes = text.getBytes();

    for (byte b : bytes) {
        int bytecode = b;
        // keycode only handles [A-Z] (which is ASCII decimal [65-90])
        if (bytecode> 96 && bytecode< 123)
            bytecode = bytecode - 32;
        robot.delay(40);
        robot.keyPress(bytecode);
        robot.keyRelease(bytecode);
    }
}

private void enterSpecialChar(int s) throws AWTException {
    Robot robot = new Robot();
    robot.delay(40);
    robot.keyPress(s);
    robot.keyRelease(s);
}

}

怎么称呼

WebDriver driver= new FirefoxDriver()// or any other driver with capabilities and other required stuff

(new Thread(new DialogWindow())).start();

driver.get(url);