Handling Authentication Window with WebDriver (In Firefox, Chrome and IE)


When you are working in a test environment, Stage or Pre Production, there are cases where you may need to work with applications which are secured with Authentication (Basic Auth).
When ever you enter the URL, it will prompt you to enter the User name and the password and It will not allow to perform any further operations until you provide username and password. And this Authentication pop-up is not a JavaScript pop-up, it is a Browser dialog window which selenium cannot handle simply using sendKeys method which we do for normal JavaScript pop-ups..


To work with Basic Authentication pop-up (which is a browser dialogue window), you just need to send the user name and password along with the application URL.
Syntax:
driver.get("http://admin:admin@yoururl.com");

Check out the example below to execute in Firefox browser:

@Test
 public void testBasicAuth_Firefox() {

  WebDriver driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("http://admin:admin@yoururl.com");
  //To check if we have landed in the correct place
  String text = driver.findElement(By.className("home")).getText();
  Assert.assertTrue(text.contains("Welcome"), "Basic Authentication failed");

 }

Check out the example below to work with Chrome browser:

@Test
 public void testBasicAuth_Chrome() {

  System.setProperty("webdriver.chrome.driver", "G:/Jars/chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("http://admin:admin@yoururl.com");
  //To check if we have landed in the correct place
  String text = driver.findElement(By.className("home")).getText();
  Assert.assertTrue(text.contains("Welcome"), "Basic Authentication failed");
 }
The best way to make it work with IE is using AutoIt tool. If not, you may need to change the stuff in registry, To change for the current user, you need to edit in 'HKEY_CURRENT_USER...' and if you want to do that for all users, you can set the value of register keys as 'HKEY_LOCAL_MACHINE...' etc.
Once you open the URL in IE it will look like the below screen shot: -
Basic authentication in IE using Selenium

Check out the example to work with IE using AutoIt tool.

First create AutoIt script as below and save it as basicauth.au3
; To pass user name and password
WinWaitActive("Windows Security")
Send("admin")
Send("{TAB}")
Send("admin")
Send("{ENTER}")
After creating the above AutoIT script, compile the script and take the location of the script exe file. Now the selenium code should look like below :
@Test
 public void testBasicAuth_IE() {
  DesiredCapabilities caps = DesiredCapabilities.internetExplorer();   
  caps.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");

  System.setProperty("webdriver.ie.driver", "G:/Jars/IEDriverServer.exe");
        WebDriver driver=new InternetExplorerDriver(caps); 
  driver.manage().window().maximize();
  driver.get("http://yoururl.com");
  try {
   Runtime.getRuntime().exec("G:/basicauth.exe");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

Followers