How to handle authentication popup in Chrome with Selenium WebDriver using Java - java

I am trying to handle an authentication pop-up in one of my new Webdriver scripts. I have a working solution for IE, but I am struggling with Chrome. IE was as simple as following the advice on [this page]:How to handle authentication popup with Selenium WebDriver using Java. That thread doesn't show a great solution for Chrome, although several commentors point out, that the solution does not work for Chrome. The problem is, when you try to do the below code on Chrome, the login popup isn't an Alert.
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(**username**, **password**));
It's not a windows level () authentication pop-up, the web page is simply password protected. I know there are several other instances of this question on Stack Overflow, but I don't see any more recently than 2 years old. I am hoping there is a better solution now in 2017. Thanks in advance.

May be helpful for others to solve this problem in chrome with the help of chrome extension. Thanks to #SubjectiveReality who gave me this idea.
Sending username and password as part of url like https://username:password#www.mytestsite.com may be helpful if same server performs both authentication and hosts the application. However most corporate applications have firmwide authentications and app server may reroute the request to authentication servers. In such cases, passing credentials in URL wont work.
Here is the solution:
#Step1: Create chrome extension#
Create a folder named 'extension'
Create a file named 'manifest.json' inside 'extension' folder. Copy below code into the file and save it.
{
"name":"Webrequest API",
"version":"1.0",
"description":"Extension to handle Authentication window",
"permissions":["<all_urls>","webRequest","webRequestBlocking"],
"background": {
"scripts" : ["webrequest.js"]
},
"manifest_version": 2
}
Create a file named 'webrequest.js' inside 'extension' folder and copy paste below code into the file and save it.
chrome.webRequest.onAuthRequired.addListener(
function handler(details){
return {'authCredentials': {username: "yourusername", password: "yourpassword"}};
},
{urls:["<all_urls>"]},
['blocking']);
Open chrome browser, go to chrome://extensions and turn on developer mode
Click 'Pack Extension', select root directory as 'extension' and pack extension. It should create a file with extension '.crx'
#Step2: Add extension into your test automation framework #
Copy the .crx file into your framework
Configure your webdriver creation to load the extension like
options.addExtensions(new File("path/to/extension.crx"));
options.addArguments("--no-sandbox");
Invoke your webdriver and application URL
You wont see the authentication popup appearing as its handled by above extension
Happy Testing!
References:
http://www.adambarth.com/experimental/crx/docs/webRequest.html#apiReference
https://developer.chrome.com/extensions/webRequest#event-onAuthRequired
chrome.webRequest.onAuthRequired Listener
https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46

*edit Chrome no longer supports this.
Isn't that a "restricted" pop-up that can be handled by prepending the address with username and password?
Instead of driver.get("http://www.example.com/"); go for driver.get("http://username:password#www.example.com");.

Updated answer of the solution provided by #Bhuvanesh Mani
manifest.json
{
"name": "Webrequest API",
"version": "1.0",
"description": "Extension to handle Authentication window",
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"background": {
"scripts": [
"webrequest.js"
]
},
"manifest_version": 2
}
webrequest.js
chrome.webRequest.onAuthRequired.addListener(function(details){
console.log("chrome.webRequest.onAuthRequired event has fired");
return {
authCredentials: {username: "yourusername", password: "yourpassword"}
};
},
{urls:["<all_urls>"]},
['blocking']);
Not certain why the '--no sandbox' option is required, but for me this is not required.
I did need to perform a registry edit to allow adding extensions since this is disabled with a corporate AD policy:
Open regedit.exe as admin
navigate to
Computer\HKEY_USERS\
Change the REG_SZ value that is now set to * to for example -.
restart the chrome instance in which you want add extensions to.
The only difference with the original answer is the url's that also need to be present in the permissions.
extra's
if you want to see console output, turn on developer mode and click on the Inspect views background page for your extension in the extensions screen in Chrome

I know your situation is Java, but this might be helpful. I was able to get past the alert using this approach in C#:
// I am using a static instance of the driver for this.
Driver.Instance.Navigate().GoToUrl(url);
var alert = Driver.Instance.SwitchTo().Alert();
alert.SendKeys($"{Driver.user_name}" + Keys.Tab + $"{Driver.user_password}" + Keys.Tab);
alert.Accept();
Driver.Instance.SwitchTo().DefaultContent();
I then utilized a test user account as the values of the user_name and password.

Selenium 4 supports authenticating using Basic and Digest auth . It's using the CDP and currently only supports chromium-derived browsers
Java Example :
Webdriver driver = new ChromeDriver();
((HasAuthentication) driver).register(UsernameAndPassword.of("username", "pass"));
driver.get("http://sitewithauth");
Note : In Alpha-7 there is bug where it send username for both user/password. Need to wait for next release of selenium version as fix is available in trunk https://github.com/SeleniumHQ/selenium/commit/4917444886ba16a033a81a2a9676c9267c472894

Related

Selenium in production Spring boot

I'm writing a web page in Spring Boot and using Selenium to get data from another website based on user's action, which means Selenium will get data on the fly. The website will require login to get the data. So the website will first need to initiate the webDriver and log in (I'm using user-data attribute but sometimes the login will expire so need to check every time). I'm trying to find the best approach for using Selenium to get data on the fly. From what I have experienced, if I use something like this to initialize Selenium driver:
try {
webDriver.get(url);
WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(TIME_OUT));
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("button")));
logger.info("Already logged in");
} catch (TimeoutException e) {
logger.error("Not logged in yet");
login(webDriver);
}
The "button" here will only be present if it is logged in. I added a log above and after this line and found out it will take about 8 seconds to finish this part, which is too long. I initially added this when user triggers the action, but this will obviously be too long.
My thought to overcome this is: for each connection, once user hits the website main page (does not need Selenium at main page) then start creating a new instance of webDriver and the login process and hold the webDriver for each of the connections. But how should I approach in such a way? Should I use Spring boot HttpSession and do something like:
session.setAttribute("driver", webDriver);
And then later if user actually makes the request that needs Selenium, then it can retrieve the webDriver using
session.getAttribute("driver");
Is this something that is reliable to use?
Or could anyone please enlighten me on how should I optimize this?
Thanks a lot!

How to switch to window authentication popup and enter credentials? [duplicate]

This question already has answers here:
Selenium - Basic Authentication via url
(6 answers)
Closed 4 years ago.
After opening application URL, User is redirected to Sign-In page where there is a Sign-In button.
driver.get("abc.com")
Now when user click on Sign-In button, URL is changed in the same window say it becomes xyz.com and shows authentication popup window for login purpose similar to image shown below.
To enter username and password in authentication window I tried the below code
shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("username")
time.sleep(1)
shell.Sendkeys("{TAB}")
time.sleep(1)
shell.Sendkeys("password")
time.sleep(1)
shell.Sendkeys("{ENTER}")
time.sleep(1)
It didn't work. Then I tried to open windows authentication popup directly(by copying URL after click of Sign-In button) with the above code, it worked
driver.get("xyz.com")//instead of abc.com my application URL
I am bit confused. If I open my app URL abc.com, click on Sign-In button, use autoit, it didn't enter credentials. But if I directly send window authentication URL xyz.com instead of app URL abc.com and use autoit, it work.
Can anyone suggest what I am missing here? I also tried to switch window after click on Sign-In button thinking its new URL and then autoit command, but it still it didn't work.
driver.switch_to_window(driver.window_handles[1])
Any idea on this?
Note: I noticed that click on Sign-In button, windows is loading infinitely & cursor is active on username text-field of windows authentication poup. Also once windows authentication window appears, none of selenium command is working and neither autoit command.
The dialog is treated as an alert by selenium. In c#, the code to enter in credentials for a Firefox test looks like:
// Inputs id with permissions into the Windows Authentication box
var alert = driver.SwitchTo().Alert();
alert.SendKeys( #"username" + Keys.Tab +
#"password" + Keys.Tab);
alert.Accept();
The first line is telling the driver to check for open alerts (the dialog box).
The second line sends the username and password to the alert
The third line then sends those credentials and, assuming they are valid, the test will continue.
Assuming you are testing using Firefox, there is no requirement to use extra frameworks to deal with this authentication box.
It looks like you just have to open the URL with basic auth credentials.
Can you try this first?
driver.get('http://username:password#abc.com')
If you're still getting the popup, try this
driver.get('http://username:password#xyz.com') #assuming this is the site that handles authentication
driver.get('abc.com')
It should stop the popup
you can automate the keyboard:
import keyboard
keyboard.write("username")
keyboard.press_and_release("tab")
keyboard.write("password")
keyboard.press_and_release("enter")
Here is an example of loading a login page with selenium, then entering login credentials with keyboard:
from selenium.webdriver import Firefox
import keyboard
driver = Firefox()
driver.get('https://somelink')
keyboard.press_and_release('tab')
keyboard.press_and_release('shift + tab')
keyboard.write('user', delay=1)
keyboard.press_and_release('tab')
keyboard.write('pass', delay=1)
keyboard.press_and_release('enter')
Note: keyboard may require root permission on Linux.
You need to switch to the alert, which is different than a window. Once the alert is present, you switch the alert handle, then use the .authenticate method to give it the username and password
alert = driver.switch_to.alert
alert.authenticate(username, password)
You may want to wait on the EC.alert_is_present expected condition to be sure the alert is present.
Try this (with page_title being the title of the popup window and assuming you are on a windows machine) :
from win32com.client import Dispatch
autoit = Dispatch("AutoItX3.Control")
def _window_movement_windows(page_title):
autoit.WinSetOnTop(page_title, "", 1)
autoit.WinActivate(page_title, "")
autoit.WinWaitActive(page_title)
An example how to setup AutoIt with python can be found here : Calling AutoIt Functions in Python
With Firefox, it's possible to avoid the authentication popup by automatically providing the credentials when they are requested.
It requires to inject and run some code at a browser level to detect the authentication attempt and set the credentials.
Here's a working example:
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
def add_credentials_moz(driver, target, username, password):
driver.execute("SET_CONTEXT", {"context": "chrome"})
driver.execute_script("""
let [target, username, password] = [...arguments];
let WebRequest = Cu.import('resource://gre/modules/WebRequest.jsm', {});
WebRequest.onAuthRequired.addListener(function listener(){
WebRequest.onAuthRequired.removeListener(listener);
return Promise.resolve({authCredentials: {username: username, password: password}});
}, {urls: new MatchPatternSet([target])}, ['blocking']);
""", target, username, password)
driver.execute("SET_CONTEXT", {"context": "content"})
WebDriver.add_credentials_moz = add_credentials_moz
driver = webdriver.Firefox()
driver.add_credentials_moz("https://httpbin.org/*", username="user", password="passwd")
driver.get("https://httpbin.org/")
driver.find_element_by_css_selector("[href='/basic-auth/user/passwd']")\
.click()

Using Selenium on https web application that runs over vpn

First of all, I need to state that I am total beginner with Selenium.
I am trying to test an application in firefox browser using Selenium. Due to security issues the application works only over vpn.
My problem occurs with the following steps; I create the webdriver and navigate to the start (login) page of application. I get an "Authorization request" popup. If I cancel the popup, then I get to a page that states "Connection is not secure" (it's https address). After I get passed that, I lose the part where Selenium program should populate username and password and it just stays on the login page.
My question, is there a way to start Selenium testing on an application so that it is already opened and prepared (ie logged in) in browser? I am not happy that username and password are hard-coded in Selenium code.
If that is not possible, how do I skip that authorization popup and problem with non-secure connection? How do I populate username and password in safest way?
Thanks!
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Selenium-java-3.0.1\\geckodriver.exe");
// I tried also with this code bellow in comment, but it did not work, it did not even get to login page
//WebDriverWait wait = new WebDriverWait(driver, 10);
//Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//alert.authenticateUsing(new UserAndPassword("cfadmin", "20lipim18"));
driver.get("https://Application_login_page.com");
driver.findElement(By.xpath(".//*[#id='login']")).click();
driver.findElement(By.xpath("[#id='login']")).sendKeys("Username");
}
if it's possible, is there a way to start Selenium testing on application that is already opened and prepared (logged) in browser?
Try using firefox profile. Since selenium open fresh instance of browser by default. You can use your own firefox frofile.
This is a code to implement a profile, which can be embedded in the selenium code.
ProfilesIni profile = new ProfilesIni();
// this will create an object for the Firefox profile
FirefoxProfile myprofile = profile.getProfile("default");
// this will Initialize the Firefox driver
WebDriver driver = new FirefoxDriver(myprofile)
It will also maintain the session means you are already login to the application in firefox(default profile). Then if you execute the script, you will see that you are already logged in to the application.
There is no way to open already authorised page, you have to have to pass username and password through selenium script.
You may use below code to do the authentication
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(username, password));

Java program for detecting server login prompt?

I'm writing a program in Java to automate a web test of server management console URL's to find which ones present users with a box for login credentials.
I have begun looking into doing this with Selenium WebDriver to automate the actual test function of opening and closing windows, but what I can't figure out is how I should go about detecting whether or not the login box is presented to a user.
Is there a specific response code that the server presents when this happens? I know when a user is unauthenticated/forbidden there will be an HTTP response of 401 or 403, respectively, but I am not sure if this 401 unauthenticated response Code will happen regardless of whether or not a box for login credentials is presented to the user.
The goal is to find which URL's allowed a user the opportunity to type in credentials and then publish those to a .txt file for further evaluation. I already have the java I/O elements working properly to do this, so all I really need is to figure out what condition I'm looking for. Any help would be appreciated!
Given you have an instantiated WebDriver instance (driver)
and a collection of URLs (urls) you want to check,
and given your loginbox contains some element located by ID "username"
then one possible Selenium solution in Java could look like this:
for (String url : urls) {
driver.get(url);
//adjust to your needs: long timeOutInSeconds, long sleepInMillis
WebDriverWait wait = new WebDriverWait(driver, 120L, 1000L);
try {
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
//TODO: Element found within timeOutInSeconds
} catch (WebDriverException e) {
//TODO: Element not present after timeOutInSeconds, write url to txt file
}
}
If you cannot locate the element by id, use other locating mechanisms as described here.

How to handle Login Authentication pop up without using Auto it?

How to handle Login Authentication pop up without using Auto it?
Can any one could help me in doing this...
you can use like below:-
driver.get("http://UserName:Password#Example.com");
Give your username in place of username, same for password and change example.com with your website
OR
WebDriverWait wait = new WebDriverWait(driver, 20);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(**username**, **password**));
Hope it will help you :)
Joe Colantonio has explained - How to Automate Non-Browser Based Functionality in very simple steps. Please go through this link. Please note that he has used AutoIT for the same [which is your requirement], but it will work only for windows platfrom
I guess you are trying to handle basic authentication popup which comes through Java Script. You can handle the java script based authentication by putting the UserId and Password in your URL. Make your URL something like below and them It wont ask to enter the credentials in popup
http://UserID:Password#YourWebsite.com

Categories