Steps to handle Proxy Authentication using Selenium Webdriver
Prerequisites:
Example 1: Manual Proxy Authentication Handling
We have cretated a PHP file (selenium_auth.php) that accepts a proxy authentication request of user name and password:
PHP
// Program for Manual Autentication <?php $php_variable = "Hello from w3wiki!" ; echo '<html> <head> <title>Selenium Authentication</title> </head> <body> <head> </head> <div style= "margin-left:20px;" > <script> var username = prompt( "Enter username-" , "" ); var password = prompt( "Enter Password" , "" ); if (username == "gfg" && password == "gfg" ) { document.write( "Proxy Authentication Passed." ); } if (!username || !password){ document.write( "Not successful" ); } if (username !== "gfg" && password !== "gfg" && username !== "" && password !== "" ) { alert( "Wrong User Name and Passowrd" ); } </script> <?php // PHP code echo <p>Hello from w3wiki!</p> </div> </body> </html>'; ?> |
Once we load it into any server such as the localhost, we can see that it is providing the following output:
Output:
As we can see if we are manually handling the proxy authentication once we provide the username and password as “gfg” it says “proxy authentication passed“, Now let’s learn how to do that using the selenium webdriver automatically once a proxy authentication request has been prompted on our screen.
Example 2: Selenium Webdriver Proxy Authentication
Step 1: Use any JDK-enabled Java editor and load the Selenium JAR files. We will be using the IntelliJ editor to execute the code.
Note: To handle proxy authentication without third-party apps in Selenium, use built-in alerts. Switch to the HTTP proxy authentication alert and pass credentials using “sendKeys” method.
Step 2: After this, we will need to write the Java code that will enter the credentials automatically. We can use the Example 1 authentication file (selenium_auth.php) for demonstration.
Java
// Java program for handling alerts // using Selenium WebDriver. package gfg; import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class AlertsDemo { public static void main(String args[]) throws IOException { // Setting the system property to the location of // the ChromeDriver executable System.setProperty( "webdriver.chrome.driver" , "C:\\Users\\kisha\\Downloads\\chromedriver_win32\\chromedriver.exe" ); // Creating a new instance of the ChromeDriver WebDriver driver = new ChromeDriver(); // Opening a web page using the WebDriver driver.get( "http://localhost/selenium-auth/selenium_auth.php" ); // Handling the username alert: // Entering the username "gfg" into the alert and // accepting it driver.switchTo().alert().sendKeys( "gfg" ); driver.switchTo().alert().accept(); // Handling the Password alert: // Entering the password "gfg" into the alert and // accepting it driver.switchTo().alert().sendKeys( "gfg" ); driver.switchTo().alert().accept(); } } |
In this above code, we will only have to make the necessary changes on the driver.get the URL, and the System.set Property so have a look at the URL on your computer and set the path to where you have downloaded the Chromium webdriver.
Step 3: Now when we run will run the Java program in any compiler. This will send two alert keys “gfg” and “gfg“, which will be fetched into the browser and the browser will successfully redirect us to the page where we will see the notification that we successfully managed to do the proxy authentication.
Step 4: We can now see in the output below we have successfully used the alert function provided by the Selenium web driver to handle proxy authentication:
Output:
How does Selenium Webdriver Handle Proxy Authentication?
Selenium is an open-source tool that provides us with the libraries that are used for browser automation the selenium browser allows us to test different functionalities in our website, thus the Selenium web driver comes very handy in day life if we are regularly testing web applications or doing automation tasks.
Contact Us