Handle authentication during WebDriver testing

Sometimes authentication is necessary before a test case can be executed. While HtmlUnit based tests can easily enter and confirm authentication requests, most browser based tests, cannot workaround the dialog. This is a browser security measure to prevent automated data capture and/or data entering. WebDriver for Firefox delivers a solution for that problem, but IE and Chrome rely on a manual interaction with the browser before the test automation can run.

The following steps describe a solution for the authentication problem and how to run a script test case as WebDriver based test. The key to this solution is the usage of Sikuli, an image based testing tool that directly interacts with the screen to find the right elements by using the screen.

Or in other words, the test automation controls the browser, not the operating system, while Sikuli controls the display of the operating system and uses the keyboard and mouse to control any program.

  • Download Sikuli tool for your platform (http://sikuli.org)
  • Extract the zip file
  • Add the path to Sikuli lib directory to your environment settings
  • Add sikuli-script.jar to your project
  • Implement a Java class as shown below
  • Take the necessary screenshots of the elements to use

Do not forget to make the appropriate screenshots of the buttons and input fields (see code) and of course, adjust the path and login data. Have fun and watch the magic happening. For more information how Sikuli works, please visit sikuli.org.

2 thoughts on “Handle authentication during WebDriver testing”

  1. Hi i tried the above code to implement in my selenium webdriver with java Eclipse,i added jar file, but its throwing me an error please help me!!

    code:
    import org.junit.Test;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    //import com.xceptance.xlt.api.engine.scripting.AbstractWebDriverScriptTestCase;
    //import com.xceptance.xlt.engine.scripting.XlteniumScriptInterpreter;
    import org.sikuli.script.*;

    import com.xceptance.xlt.engine.scripting.XlteniumScriptInterpreter;

    public class BrowserAuthenticationSUK {

    public static void main(String[] args) throws FindFailed, InterruptedException {

    WebDriver driver = new InternetExplorerDriver();

    // create Sikuli screen object

    Thread.sleep(5000L);
    driver.get(“http://admin.localdomain.com/”);
    Thread.sleep(5000L);

    // wait 5 seconds for the image with the username input field

    Screen s = new Screen();
    s.wait(“D://img//dailog.png”,5);

    // type username “demo” into the input field

    s.type(“temp\t”);
    s.type(“pass\t”);

    // type password into the password field
    // s.type(“D:/img/ps.png”,”demoomed!”);

    // click ok button
    s.click(“D://img//button.png”);

    }
    }

    Error which i got:

    [info] Windows utilities loaded.
    [info] Sikuli vision engine loaded.
    Exception in thread “main” FindFailed: can not find D://img//dailog.png on the screen.
    Line ?, in File ?
    at org.sikuli.script.Region.handleFindFailed(Region.java:420)
    at org.sikuli.script.Region.wait(Region.java:511)
    at BrowserAuthenticationSUK.main(BrowserAuthenticationSUK.java:27)

    Thank you.

  2. Hi Chandra, the error message indicates that there is no image with the name dailog.png in d:\img available. Please check that you placed the image there and named it properly.

Leave a Reply

Your email address will not be published. Required fields are marked *