Pass Website Basic Authentication Use Selenium WebDriver

Authentication is normally a technology which can make your application more secure. It validate user credentials at the server side first then user can continue to use the application. Basic authentication is used in web applications. It use a browser window to collect user credentials. This can make your website more secure. And block unauthorized users from using your website. This can reduce invalid website traffic also.

basic-authentication-popup-window

 

If a website use basic authentication, then when you browse a webpage, a window will prompt to gather your username and password. This window is a Browser dialog rather than a JavaScript popup. So it is difficult to use selenium’s sendKeys method  to input credentials.

Solution

The solution is also simple, you can just build your page url like following.

http://username:password@url.

Below is java code example

/* First build the page url with the username and password for the authentication.*/
String pageUrl="http://admin:[email protected]";
/* Then use webdriver to get above url. */
ffDriver.get(pageUrl);

Code example

For Chrome Use Selenium Webdriver

 @Test
 public void testChromeBasicAuth() {
 /* First set the Chrome executable file path. */
 System.setProperty("webdriver.chrome.driver", "C:/WorkSpace/dev2qa.com/Lib/chromedriver_win32/chromedriver.exe");
 
 /* Then create the ChromeDriver object. */
 WebDriver chromeDriver = new ChromeDriver();
 
 /* Maximize the Chrome browser window. */
 chromeDriver.manage().window().maximize();
 
 /* Construct page url to pass the website basic authentication. */
 String pageUrl = "http://admin:[email protected]";
 
 /* Browse the page url above. */
 chromeDriver.get(pageUrl);
 
 /* Check whether pass the authentication or not. */
 String text = chromeDriver.findElement(By.className("home")).getText();
 
 /* If not pass then throw an assertion. */
 Assert.assertTrue("Basic Authentication for this web site failed" , text.contains("yahoo home"));
 
 chromeDriver.quit();
 }

For Firefox Use Selenium Webdriver

@Test
 public void testFirefoxBasicAuth() {

/* Create Firefox driver object and maximize browser window. */
 WebDriver ffDriver = new FirefoxDriver();
 ffDriver.manage().window().maximize();
 
 /* Browse the basic authentication web page url. */
 ffDriver.get("http://admin:[email protected]");
 
 /* Check whether pass the basic authentication or not. */
 String text = ffDriver.findElement(By.className("home")).getText();
 
 /* Throw assertion when fail. */
 Assert.assertTrue("Basic Authentication for this web site failed" , text.contains("yahoo home"));

ffDriver.quit();
 }

For Internet Explorer Use Selenium Webdriver

First you need use another automation test tool “AutoIt” to implement input username and password in the basic authentication popup window. Save following AutoIt script in a file passBasicAuthentication.au3 and compile it to generate the executable file as C:/passBasicAuthentication.exe

; Activate the popup window
WinWaitActive("Windows Security") 
; Input admin in the username text field.
Send("admin") 
; Click Tab key to move to password text field.
Send("{TAB}") 
;Input admin in the password text field
Send("admin") 
;Click Enter key to submit
Send("{ENTER}")

Below is the java code for the selenium testing script that will use Internet Explorer to run the test. In the code, it will call the AutoIt executable file to pass the website basic authentication.

@Test
 public void testIEBasicAuth() {
 
 /* Create the internet explorer DesiredCapabilities object. */
 DesiredCapabilities itCaps = DesiredCapabilities.internetExplorer();
 
 /* Set internet explorer default page to an empty page. */
 itCaps.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");

/* Set the internet explorer executeable file path. */
 System.setProperty("webdriver.ie.driver", "C:/WorkSpace/dev2qa.com/Lib/IEDriverServer.exe");
 
 /* Create the internet explorer driver object and maximize the browser window. */
 WebDriver ieDriver=new InternetExplorerDriver(itCaps); 
 ieDriver.manage().window().maximize();
 
 /* Browse the yahoo home page. */
 ieDriver.get("http://yahoo.com");
 try {
 /* Call the AutoIt test script to pass the web site basic authentication. */
 Runtime.getRuntime().exec("C:/passBasicAuthentication.exe");
 } catch (Exception ex) {
 ex.printStackTrace();
 }
 }

1 thought on “Pass Website Basic Authentication Use Selenium WebDriver”

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.