Webdriver - HTTP authentication dialog

痞子三分冷 提交于 2020-01-20 02:26:08

问题


I have a very simple selenium-webdriver script. I would like to do HTTP authentication using webdriver.

Script:

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");

Issue:

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

throws

org.openqa.selenium.NoAlertPresentException: No alert is present

Question:

  • Does Webdriver find only an alert dialog as alert?
  • What are my options to automate this without using AutoIt OR http:// username:password @somesite

EDIT

Alert has below method and does not seem to have been implemented yet.

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

回答1:


The problem is that this is not a javascript popup hence you cannot manipulate it via selenium's alert().

If both AutoIt and submitting credentials in the URL (the easiest option - just open up the url and click "Display Image") are not options for you, another approach could be to use AutoAuth firefox addon to automatically submit the previously saved credentials:

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?)

Following the answer suggested in HTTP Basic Auth via URL in Firefox does not work? thread:

  • 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);

Also, in a way similar to AutoIt option - you can use sikuli screen recognition and automation tool to submit the credentials in the popup.


Also see other ideas and options:

  • Support BASIC and Digest HTTP authentication
  • Handling browser level authentication using Selenium



回答2:


The Basic/NTLM authentication pop-up is a browser dialog window. WebDriver (Selenium 2.0) cannot interact with such dialog windows. The reason for this is because WebDriver aims solely at mimicking user interaction with websites, and browser dialog windows are currently not in that scope. JavaScript dialog windows, are part of the website, so WebDriver can handle those. In Selenium 1.0 it is possible to do basic authentication.

So how to solve this issue? 1) Authentication via URL http://username:password@website.com 2) Use a browser plugin that will handle the Basic/NTLM autentication 3) Use a local proxy that will modify the request header and pass along the username/password and 4) Make use of a robot, like AutoIt, or some Java library.

Option 1: is the easiest and has the least impact on the system/test. Option 2: has a high browser impact as your loading plugins. Also every browser uses its own plugin and it's possible that the required plugin for a certain browser is not available. Option 3: Works well with HTTP, but HTTPS requires custom certicates thus not always an option. Not much impact on both system and test. Option 4: Mimics keyboard presses, its a go to solution but prone to errors. Only works when the dialog window has focus and it is possible that this is not always the case.




回答3:


I faced same issue, and got some concrete solution using robot class. Its workaround or solution, Let see , but it works.

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

}

How to call it

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

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

driver.get(url);


来源:https://stackoverflow.com/questions/27878208/webdriver-http-authentication-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!