Authentication popup goes into the background Selenium, Firefox - java

I have had the problem for 2 weeks that when I create a new Firefox driver in Selenium, the authentication popup for the proxy is immediately pushed into the background. Selenium can't reach it there anymore. Do you have a solution to the problem? I am using Selenium 3.141.5, Java 1.8. and Firefox version 63.0.1.
System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver(options);
try {
Alert alert = driver.switchTo().alert();
alert.sendKeys("Username" + Keys.TAB + "Password");
alert.accept();
driver.switchTo().defaultContent();
}catch (NoAlertPresentException e) {
e.printStackTrace();
}
driver.get("https://www.google.de/");
EDIT: I tested it with Firefox version 62.0.3, everything works there.

Best way is to avoid the pop-up.
hit Win+R, run "firefox -p" and create new profile (let's call it selenium_profile)
run Firefox in selenium_profile, login to the proxy and save your credetials to Firefox
use the customized profile, there is my setup:
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", sec_var.driver_path);
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
With custom browser profile you can use almost any settings modification, imported certificate (to avoid another auth pop-up), use extensions, ...
Basic auth pop-up you can avoid with send credentials in URL:
driver.get("https://username:password#www.example.com");
but it does not work in Chrome.

I have the exact same problem and have been researching everywhere for a workaround.
Here is what I've learned so far:
Using the previously saved custom browser profile that keeps your credentials saved (#pburgr suggestion) does the trick, however it also accumulates a lot more unintended and possibly undesired browser profile informations such as cookies, history and everything.
the auth pop up is not a normal pop up so it cannot be manipulated with selenium switch_to
In addition these type of credential pop ups cannot be inspected nor javascripted.
Another workaround is to use pyAutoGui to alt+tab and fill your credentials. Not a great solution though because you may have trouble into guessing how many alt+tabs until the pop up.
Bottomline: The most promising way would be loading a fresh clean browser profile, updating this profile by adding such credentials (I don't know how nor if feasible) and voilĂ , discarding this profile at the end of the session.

Related

Firefox selenium webdriver gives "Insecure Connection"

I've created a Maven project with 20 tests made with Selenium Webdriver (java). Now, when I want to execute my Maven project, I get sometimes the following error:
Mozilla error
This is due to login every test. So, when I want to run 20 tests, sometimes that error appears and I can't continue my test, so it returns "Failed test" in Selenium Webdriver.
Does anybody know how to fix this problem?
I've tried to put "Thread.sleep(30000);" at the end of every test to give them some time "not to seem a robot", but it doesn't work...
Thanks so much for your help guys!
Here is the Answer to your Question:
The Real Issue:
The URL/Connection with which you are a working if it is Not Secure then whenever you access the URL through Mozilla Firefox 53.0, Firefox will display a lock icon with red strike-through red strikethrough icon in the address bar. Now when URL gets loaded the cursor by default will be positioned on Username field and there will be popup showing a message This connection is not secure. Logins entered here could be compromised. Learn More like this:
Now your script through Selenium enters the username within the Username input field and the Not Secure popup overlays the Password input field.
Next if you try to call the click() or sendKeys() operation in the Password input field the Not Secure popup receives the click and Insecure password warning in Firefox page opens up in the next tab along with Selenium shifting its focus to new tab. Hence test-case starts Failing.
Solution:
In these cases the best solution is:
Create a new Mozilla Firefox Profile. You will find the documentation here. For Example, I have created a Firefox Profile by the name debanjan
Configure the Firefox Profile debanjan to ignore all the UntrustedCertificate issues.
Rerun your Test Script without any issues.
Here is a sample code block to disable insecure_field_warning:
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
testprofile.setAcceptUntrustedCertificates(true);
testprofile.setAssumeUntrustedCertificateIssuer(true);
testprofile.setPreference("security.insecure_field_warning.contextual.enabled", false);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
dc.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(dc);
driver.manage().window().maximize();
driver.navigate().to("http://demosite.center/wordpress/wp-login.php");
Let me know if this Answers your Question.

Starting chromedriver with saved passwords enabled

Everytime that I start the Chrome web browser, using chromedriver, it starts up clean with any custom settings disabled. I have a case where I am logged in on a website and I want to access the account to get some information. However, the newly opened browser is not logged into the account anymore. Even when I open a new browser manually I am still logged in on that same page. Is there a way to enable custom settings? Preferably in Java.
You can achieve this by using ChromeOptions as below :-
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
String chromeDirPath = "provided here a path where you want to custom chrome dir which could be use everty time you launch"
//ensure chromeDirPath exist in dir
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir="+chromeDirPath);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("url");
Now it will maintain your custom browser setting at chromeDirPath
Hope it will help you.

How do I access my existing cookies using Chrome?

I have cookies with Gmail login info, so that chrome automatically opens my Gmail.
I tried the following code, but it didn't work:
System.setProperty("webdriver.chrome.driver","chromedriver\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default"));
//I also tried using: capabilities.setCapability("chrome.switches", Arrays.asList("--user-data-dir = C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default"));
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://gmail.com");
I checked the directory of C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data\\Default it is okay.
What is the problem in here?
There is a Known issues section on the Chrome Driver official wiki page I haven't noticed before:
Known Issues
3 . Cannot specify a custom profile
Now, I don't know whether this is or isn't outdated. I could not find a bug report for this. It's true that you can't specify a custom profile via Capabilities (as of July 2013), as you discovered. But there is a solution...
The Solution
Here's how I managed to make it run:
ChromeOptions opt = new ChromeOptions();
opt.setBinary("E:\\some\\path\\chrome.exe");
opt.addArguments("--user-data-dir=C:\\Users\\Owner\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(opt);
Notice the path to the User data directory - it does not have the \\Default part. And in that case, it works just fine for me, opens up the Chrome profile stored with all the cookies and logins.
I have no idea why the Capabilities solution does not work. It might be worthwile to file a bug as I could not find one on topic.

Using Personal SSL certificates with Webdriver (Selenium 2.0)

I am testing a website which requires personal SSL certificates in order to do certain things, such as sign-in.
I have a Webdriver (Selenium 2.0) test that I have set up with a proxy:
Proxy localhostProxy = new Proxy();
localhostProxy.setProxyType(Proxy.ProxyType.MANUAL);
localhostProxy.setHttpProxy("www-proxyname:port");
FirefoxProfile profile = new FirefoxProfile();
profile.setProxyPreferences(localhostProxy);
driver = new FirefoxDriver(profile);
And this will access the homepage fine. The test then clicks the sign in button, enters in the correct credentials and clicks on submit. At this point the browser then goes into a loading state, and I'm assuming it's because the SSL certificate is missing from my side and therefore cannot connect to the sign in service.
I searched for different proxy solutions, and found this:
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
So I added it into my code, but it doesn't seem to do what I want. I think I'm looking for a way to tell WebDriver that my ssl certificate is in x directory, please use it when accessing this site. Does anyone know how to do this?
My Test code is:
#Test
public void userSignsInAndVerifiesDrawerViews(){
driver.get("www.url.com");
waitFor(5000);
driver.findElement(By.xpath("//a[contains(text(), 'Sign in')]")).click();
waitFor(3000);
String username = "seleniumtest";
String password = "seleniumtest1";
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.xpath("//signin")).click();
waitFor(30000);
String signInLinkText = driver.findElement(By.xpath("//xpath")).getText();
assertEquals(signInLinkText, username);
}
Webdriver has no built in mechanism for adding a personal cert.
If you are using firefox the only way that I have found to do this is to create a firefox profile and add the certificate to it. You can then either reuse the profile when you run your tests OR, and this is my prefered option, take the cert8.db and key3.db files and add them to the profile that webdriver creates at runtime.
I am not sure how yo do this in java, but in ruby I override the layout_on_disk method of FirefoxProfile to add the extra files I required. Java has the same class so you should be able to do this same thing.
No need to overwrite the method layout_on_disk() as suggested.
You can simply load as profile a folder containing the files cert8.db and key3.db.
Selenium will complete the profile for you.
Then you can add the preferences you need to the firefox profile.
The resulting code looks like this:
FirefoxProfile firefoxProfile = new FirefoxProfile(
new File("/folder/location"));
FirefoxOptions options = new FirefoxOptions();
options.setProfile(firefoxProfile);
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
options.toCapabilities());
Tested with selenium 3.5.3.
Webdriver can do this, although Derek is right and it isn't built in.
All you need to do is make a custom Trust Manager that trusts all certs and then also override the "hostname verifier" to allow a non-real domain name.
There is somewhat of an example I found on Google here:
http://grepcode.com/file/repo1.maven.org/maven2/org.seleniumhq.selenium.server/selenium-server-coreless/1.0.3/org/openqa/selenium/server/TrustEverythingSSLTrustManager.java
This is the same method you would use with Apache HC components to override SSL settings without using WebDriver. I've used this method a lot with direct HTTP posts using Apache HT components and it "appears" that from the link above , this concept should also work with WebDriver.

How to perform Basic Authentication for FirefoxDriver, ChromeDriver and IEdriver in Selenium WebDriver?

I am using the Selenium-Firefox-driver and Selenium-Chrome-Driver version 2.0a5 (Web Driver API), and I am trying to test a web app that has BASIC authentication (there is a popup that come up to authenticate the user when I hit whatever page, the popup is not part of the HTML).
Now, I need to a strategy to authenticate the user in Firefox, Chrome and IE (I'm going to import the IE Driver soon).
I was reading in few articles that I can set a Firefox profile for instance..something like:
FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.setPreference("network.http.phishy-userpass-length", 255);
WebDriver driver = new FirefoxDriver(ffProfile);
driver.get("http://username:password#hostname");
but it doesn't seem to work for me. Does anyone have a working solution for those browsers?
I got it to work with Firefox webdriver by the following:
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "google.com");
driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("http://user:pwd#google.com");
True, BASIC HTTP authentication is not currently supported but I got it working now for FF and for Chrome.
The code I wrote in the questions works for those drivers. I just tried using FF3.6 as Firefox default browser (installed in Firefox folder) instead of FF4 (not supported yet). For IE, i may try to disable the authentication through Windows Registry.
This page http://code.google.com/p/selenium/issues/detail?id=34 may help.
For more portability, this can be handled by stub API and using Alert.
Example Java code (sample):
import org.openqa.selenium.Alert;
import org.openqa.selenium.security.Credentials;
public void authenticateUsing(Credentials credentials) {
private final Alert alert;
alert.authenticateUsing(credentials);
}
See also: auth_tests.py
Or by sending keys manually like:
SendKeys("user");
SendKeys("{TAB}");
SendKeys("password");
SendKeys("~"); // Enter
See also the following feature request: #453 Portable BASIC Auth at GitHub
Related:
How to send Basic Authentication headers in Selenium? at QA SE
Add this New Firefox Profile on your code
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("myProjectProfile"); //replace "myProjectProfile" with your profile"
WebDriver driver = new FirefoxDriver(myprofile);
Firefox configuration settings
This works fine without prompting any authentication when you do the following settings..
Type "about:config" on your FF url
Now type "Proxy" in the search field
Make sure "signon.autologin.proxy" is set "true" (By default
it is "false")
Load Default/Custom Chrome Profile to run tests using Selenium
WebDriver
Download chromedriver.exe
Extract the chromedriver_win_26.0.1383.0.zip folder and locate .exe file to C:/ folder
Add this Script on your JAVA code
DesiredCapabilities capability = DesiredCapabilities.chrome();
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
capability.setCapability("chrome.switches", Arrays.asList("–disable-extensions"));
capability.setCapability("chrome.binary", "C:/Users/user_name/AppData/Local/Google/Chrome/Application/chrome.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/Default");
driver = new ChromeDriver(capability);
Note: IE doesn't need profile setup to run tests because they run on Server user while Firefox and Chrome works with binary.
If you want to enable the http auth in Internet explorer, you have to edit the registry and add this (create keys if they are not present):
in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create a DWORD iexplore.exe with a value of 0
in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create a DWORD iexplore.exe with a value of 0
Close and reopen Internet explorer
If you have a x64 IE, the path is a bit different :
HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE
There is a solution for performing authentication with Selenium 1.x by manually setting the HTTP headers at http://mogotest.com/blog/2010/06/23/how-to-perform-basic-auth-in-selenium but I don't think this is transferable to Selenium 2, as you don't have access to the headers.
According to the information here 'Basic Authentication support for Selenium 2' was added in Selenium 2 Beta 2 but looking through the source code I can only see it implemented as a way of securing Remote Selenium Servers against anonymous access.
So I think the answer is that BASIC HTTP authentication is not currently supported.
I was not able to use the basic authentication with Selenium 2 and Chrome (Due a bug with Chrome), so I created an extension for Chrome that sends the basic authentication credentials automatically (See https://chrome.google.com/webstore/detail/basic-authentication-auto/dgpgkkfheijbcgjklcbnokoleebmeokn).
Multipass extension of Firefox made automation engineers life easy. Through this, we can handle the basic authentication pop-up in any browser using any programing language. PFB the steps:
Open the Firefox browser and download the plug-in
-> https://addons.mozilla.org/en-US/firefox/addon/multipass/
Now go to the below location to get the XPI file that is the 'multipass' executable file for firefox
-> C:\Users\Your user name\AppData\Roaming\Mozilla\Firefox\Profiles\oleovwxr.extensionUser\extensions
Copy the file 'multipass#gilles.crettenand.info.xpi' from the above directory and past it to your project directory inside any folder of the resource package.
Now use the below code snippet to configure the Firefox driver.
public WebDriver config() {
System.setProperty("webdriver.gecko.driver", "Path to geco driver");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "firefox");
capabilities.setCapability(CapabilityType.PLATFORM_NAME, "WINDOWS");
capabilities.setCapability("acceptSslCerts", true);
capabilities.setCapability("marionette", true);
FirefoxProfile profile = new FirefoxProfile();
//Give the multipass path
profile.addExtension(new File("c:/your project name/src/main/resources/multipass#gilles.crettenand.info.xpi"));
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile);
firefoxOptions.merge(capabilities);
return new FirefoxDriver(firefoxOptions);
}
Now the main challenge is to get the UUID of the downloaded multipass extension as whenever we run it changes. So we are taking every time when run.
public void setup() throws InterruptedException {
WebDriver driver = config();
driver.get("about:debugging#/runtime/this-firefox");
Thread.sleep(4000);
String uuid = driver.findElement(By.xpath("//span[#title='MultiPass for HTTP basic authentication']/parent::li/section/dl/div/dt[contains(text(),'UUID')]/parent::div/dd")).getText();
System.out.println("My Url:::" + "moz-extension://" + uuid + "/popin.html");
driver.get("moz-extension://" + uuid + "/popin.html");
//change below URL with your URL and username and password
driver.findElement(By.id("url")).sendKeys("http://mywebsite.com");
driver.findElement(By.id("username")).sendKeys("site user name");
driver.findElement(By.id("password")).sendKeys("site password");
driver.findElement(By.xpath("//button[.='Add']")).click();
//Now change below URL with your url, note:: the domain should math with above multipass url
driver.get("http://mywebsite.com/homeLogin.html");
}
I have tested with the below selenium version selenium-java 4.1.1 and selenium-server 3.141.59.

Categories

Resources