Sunday 10 May 2015

Authentication Window pop up handle with WebDriver API



If you are working in test environment where network admin make the secure the directory with some user name and password, your testing environment like Stage or Production, there are such a cases where you may need to work with an applications which are secured with double Authentication like one server user name password other is application login name and password.
Whenever you enter the URL in browser, it is prompting to you enter the user name and the password. It will block you to perform any further operations until you provide correct username and password and this authentication pop-up is not a JavaScript pop-up, it is a window base browser dialog which selenium webdriver API cannot handle simply using send Keys method which we do for normal JavaScript pop-up box.
While working with basic Authentication pop-up (which is a browser dialogue window), you need to send the user name and password along with the application URL.

Proper Syntax:-

driver.get("http://admin:admin@abcxyz.com");
Example below to execute your website which is secure with windows authenticate user name and password :
@Test
public void basicAuth_Firefoxpopuptest() {
WebDriver driverapi = new FirefoxDriver();
driverapi.manage().window().maximize();
driverapi.get("http://admin:admin@abcxyz.com");
//Check if we have gone to landed in the correct place
String text = driverapi.findElement(By.className("home")).getText();
Assert.assertTrue(text.contains("Welcome"), "Basic Authentication failed");

                }
See the good 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@xyz.com");
                 //To verify if we have reached in the correct place
                 String text = driver.findElement(By.className("home")).getText();
                 Assert.assertTrue(text.contains("Welcome"), "Basic Authentication failed");
         }
 Note:- we can also achieve this with AutoIt tool and Robot class details is in this blog

No comments:

Post a Comment