Introduction
As the IT sector continues to evolve, securing applications becomes increasingly important. Many organizations implement specific proxy settings that necessitate user credentials when accessing their servers in a browser. Consequently, Selenium must be equipped to handle these authentication prompts before server access.
Below are several methods for handling such authentication pop-ups, especially in a corporate environment where proxy settings may be in place.
1. Via the URL
you can include the username and password directly in the URL. This method is straightforward but may not work in all scenarios, especially if the authentication pop-up is more complex.
Syntax: http://username:password@URL
username=xyz
password=abc@xyz
String URL = “http://” + xyz+ ”:” + abc@xyz+ “@” +www.AutomationEnigma.com;
driver.get(URL);
Alert alert = driver.switchTo().alert();
alert.accept();
Example Selenium Code
// Launch browser and navigate to the URL
driver.get("http://yourwebsite.com");
// Execute AutoIT script to handle the pop-up
Runtime.getRuntime().exec("C:\\path\\to\\auth_script.exe");
// Continue with test execution
2. Using Selenium with AutoIT (Windows only)
You can use AutoIT, a third-party tool for system-level pop-ups (Windows only) for non-basic authentication pop-ups.
Steps:
Download and install AutoIT.
Write an AutoIT script to handle the authentication pop-up.
Compile the script into an executable (.exe).
Call the executable from your Selenium code.
Sample AutoIT Script
WinWaitActive("Authentication Required") ; The title of the authentication pop-up
Send("your_username") ; Send username
Send("{TAB}") ; Press Tab to move to the password field
Send("your_password") ; Send password
Send("{ENTER}") ; Press Enter to submit
3. Using ChromeOptions (Chrome only)
For Chrome, you can handle basic authentication pop-ups using ChromeOptions by sending credentials through browser settings.
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.HashMap;
import java.util.Map;
public class AuthenticationPopupHandling {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Create ChromeOptions instance
ChromeOptions options = new ChromeOptions();
// Add credentials to Chrome's profile for authentication
Map<String, Object> prefs = new HashMap<>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
// Initialize WebDriver with ChromeOptions
WebDriver driver = new ChromeDriver(options);
// Open the URL (basic authentication example)
driver.get("http://username:password@AutomationEnigma.com");
}
}
4. Using FirefoxProfile (Firefox only)
you can handle authentication by setting FirefoxProfile preferences.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
public class AuthenticationPopupHandling {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
// Create Firefox profile and set preferences
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "yourwebsite.com");
profile.setPreference("network.negotiate-auth.delegation-uris", "yourwebsite.com");
profile.setPreference("network.negotiate-auth.trusted-uris", "yourwebsite.com");
// Initialize FirefoxOptions
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
// Launch the browser with the custom profile
WebDriver driver = new FirefoxDriver(options);
// Open the URL (basic authentication example)
driver.get("http://AutomationEnigma.com");
}
}
5. Handling Authentication Pop-ups in Headless Browsers
In case you're running tests in headless browsers like Chrome Headless or Firefox Headless, the basic authentication credentials should be passed directly through the URL, similar to the first method.
options.addArguments("--headless");
driver.get("http://username:password@AutomationEnigma.com");
6. Handling Authentication Alerts with Robot Class
If the authentication pop-up is a standard Java dialog, you can use the Robot class to send keystrokes to the dialog. This method is less reliable but can work in certain cases.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.awt.*;
import java.awt.event.KeyEvent;
public class RobotAuthExample {
public static void main(String[] args) throws AWTException {
String username = "yourUsername";
String password = "yourPassword";
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://yourwebsite.com"); // Trigger the authentication pop-up
Robot robot = new Robot();
// Enter username
for (char c : username.toCharArray()) {
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(c));
robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(c));
}
// Press Tab to go to the password field
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
// Enter password
for (char c : password.toCharArray()) {
robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(c));
robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(c));
}
// Press Enter to log in
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
// Your further automation code here...
// Close the browser
driver.quit();
}
}
7. Handling Alerts Using Alert Interface
If your application uses JavaScript alerts for authentication, you can handle them using Selenium's Alert interface.
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AlertAuthExample {
public static void main(String[] args) {
String username = "yourUsername";
String password = "yourPassword";
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Navigate to the URL that triggers the alert
driver.get("https://yourwebsite.com");
// Handle the alert
Alert alert = driver.switchTo().alert();
alert.sendKeys(username + Keys.TAB + password); // Send username and password
alert.accept(); // Click OK
// Your further automation code here...
// Close the browser
driver.quit();
}
}
Conclusion
Basic Authentication: The easiest method is embedding credentials directly into the URL.
Non-Basic Authentication: You can use external tools like AutoIT.
Browser-Specific Approaches: Both Chrome and Firefox offer native ways to manage authentication pop-ups through browser profiles and options.
Comments
Post a Comment