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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import org.junit.Test; 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.*; public class TfoobarIE extends AbstractWebDriverScriptTestCase { /** * Constructor starts internet explorer */ public TfoobarIE() { super(new InternetExplorerDriver(), "http://xlt.xceptance.com"); } /** * Main test method solves authentication problem and executes xlt script developer script */ @Test public void test() throws Throwable { // create Sikuli screen object Screen s = new Screen(); // open page with xlt open("/demo/"); // wait 5 seconds for the image with the username input field s.wait("./img/username_input_field.jpg",5); // type username "demo" into the input field s.type("demo"); // type password into the password field s.type("./img/password_input_field.jpg","demoomed!"); // click ok button s.click("./img/ok_button.jpg"); // execute xlt script developer script XlteniumScriptInterpreter interpreter = new XlteniumScriptInterpreter(getWebDriver()); interpreter.executeScript("Tfoobar"); } } |
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.
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.
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.