http authorization with webdriver and popup window - c#

I'm trying to use Selenium WebDriver to automatically login in to a site with a user-name and password. I've done my research and I don't believe this feature is supported by WebDriver, so I need to find another way. The site I'm trying to automate logging into is located here.
When prompted to login a popup window comes up that doesn't seem to be part of the browser. I'm using Firefox and Chrome. It seems Windows API may be required? I already tried passing the credentials in the URL but that didn't work. Also tried sendkeys, but received a Windows exception that the application was not accepting Windows messages. I also tried switching the current handle using driver.windowhandles but the popup doesn't seem to be a new handle.
Does anybody have any ideas? I'm kinda stuck. The preliminary code to get to the popup window is:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.portal.adp.com");
string currentWindow = driver.CurrentWindowHandle;
IWebElement userLogin = driver.FindElement(By.Id("employee"));
userLogin.Click();

The popup you are seeing is prompted by web server and is a authentication prompt. Selenium doesn't support this operation.
One of the way to handle this limitation is to pass user and password in the url like like below:
http://user:password#example.com
More info available here : http://aleetesting.blogspot.in/2011/10/selenium-webdriver-tips.html

I wanted my answer out there because I think I've solved it. This answer does not require passing the credentials through the URL (for those of you that are unable to like me). It also does not require any custom Firefox Profiles or extensions to be installed or included with the solution or installed onto the browser eliminating cross-machine compatibility issues.
The issue with me was that the authentication could not be completed via passing the credentials through the URL because the login was behind a proxy.
So, I turned to windows automation toolkits and found AutoIT. Using AutoIT and Selenium, you can login automatically by sending the username and password to the windows dialog that appears. Here's how (note the steps below are for c#:
1 - Download AutoIT from http://www.autoitscript.com/site/autoit/downloads/
2 - Add the autoit .dll to your project references.
Right click on references, select Add Reference. Next click the browse button and browse to the dll location (most default installations it will be c:\Program Files (x86)\AutoIt3\AutoItX\AutoItX3.dll), and add to project.
3 - use AutoIT and Selenium like this (assuming your web driver is already initialized):
//Initialize AutoIT
var AutoIT = new AutoItX3();
//Set Selenium page load timeout to 2 seconds so it doesn't wait forever
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(2));
//Ingore the error
try
{
Driver.Url = url;
}
catch
{
return;
}
//Wait for the authentication window to appear, then send username and password
AutoIT.WinWait("Authentication Required");
AutoIT.WinActivate("Authentication Required");
AutoIT.Send("username");
AutoIT.Send("{TAB}");
AutoIt.Send("password");
AutoIT.Send("{ENTER}");
//Return Selenium page timeout to infinity again
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(-1));
Anyway, that's it, and it works like a charm :)
Also note that there are some special characters that need to be escaped in AutoIT using the sequence "{x}". For example, if your password is "!tRocks", you'd need to pass it into AutoIT as "{!}tRocks".
Happy automating.

FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.http.phishy-userpass-length", 255);
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", hostname);
Driver = new FirefoxDriver(profile);
hostname is your URL (example.com) then try to
Driver.Navigate().GoToUrl(http://user:password#example.com);

I just got done working on a prototype project that is supposed to handle exactly this kind of situation.
It utilizes BrowserMob, a popular open source proxy, to perform the authentication.
SeleniumBasicAuthWrapper Hope it helps! It is still a work in progress, but hopefully we'll get any kinks or defects ironed out in the near future.

Related

Windows Authentication Pop up Issue in Selinum Automation

I am trying to write UI Automation for a Portal but while Authentication Chrome after entering User name a Pop up comes which can not be handled by selenium c#
I have tried using
https://username:password{siteurl}.com
But it didn't work. Also Auto IT nuget is not working. Any suggestions will be really helpful
PFB the screenshot for the same.
You can't try using a "robot" keyboard which just types in the keyboard. There is no need for the element to be inside the HTML page, as long as you know the prompt is focused:
Install the NuGet InputSimulator:
InputSimulator sim = new InputSimulator();
sim.Keyboard.TextEntry("User123");
sim.Keyboard.KeyPress(VirtualKeyCode.TAB);
sim.Keyboard.TextEntry("Password123");
sim.Keyboard.KeyPress(VirtualKeyCode.RETURN);
This way you just "hit" the keyboard. You code works regardless of the webpage.
If the popping prompt is not part of the HTML page, but a Windows alert, you can try using the keyboard:
new Actions(driver).SendKeys("User_Name").Perform();
new Actions(driver).SendKeys(Keys.Tab).Perform();
new Actions(driver).SendKeys("Password_here").Perform();
new Actions(driver).SendKeys(Keys.Tab).Perform();
new Actions(driver).SendKeys(Keys.Enter).Perform();
Basically you just type the user_name and password and hit the Enter key using Selenium.

I am not able to open my application using FireFox C# selenium webdriver

I want to test my unit cases using firefox. I am using C# selenium webdriver. My application uses windows authentication. The code is
FirefoxOptions d= new FirefoxOptions();
In URL i am passing username and password
Amna - I am looking for a better way to handle Windows Authentication but here is something that will work.
Add NuGet package AutoIT
Then add this to your login:
bool ele1 = AutoItX.WinExists("[CLASS:MozillaDialogClass]")== 1;
if (ele1)
{
AutoItX.WinActivate("[CLASS:MozillaDialogClass]");
AutoItX.Send("Username");
AutoItX.Send("{TAB}");
AutoItX.Send("Password");
AutoItX.Send("{ENTER}");
}
Alert Login was Deprecated as was user/pass in the url string. AutoIT is the only thing that has worked but it is not thread safe so you have to run single thread.

Complete Internet Explorer Authentication Dialog with Selenium

I am using Selenium to simulate a user to automate some legacy software. The software works only with IE6 (I'm using IE11 in compatibility mode) and is a bit crap.
There is a point in the software where the Windows Security dialog appears. This requires credentials before the user/simulator can proceed.
I'm using IAlert.SetAuthenticationCredentials to try and populate the dialog but this doesn't seem to work. To move on from this, I can enter the details manually, but then Selenium seems to thing the main browser window has been closed:
Currently focused window has been closed.
The WindowHandles collection at this point is empty, but the browser window is still open, and has rendered the correct page.
What's going on here?
UPDATE
The answers provided are suggestions on how to handle the dialog. I'm wondering why Selenium thinks the browser window is closed when in fact it is still there.
It is not possible to interract with native windows via selenium. The way to deal with your issue is for example to use analogue of Robot in Java. Since you are using C# there is a simulator here https://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library.
Example code would be like following:
// Simulate (Ctrl + C) shortcut, which is copy for most applications
KeyboardSimulator.SimulateStandardShortcut(StandardShortcut.Copy);
// This does the same as above
KeyboardSimulator.KeyDown(Keys.Control);
KeyboardSimulator.KeyPress(Keys.C);
KeyboardSimulator.KeyUp(Keys.Control);
There are also Mouse simulators, so with this framework it will be possible to enter the required values in window and accept it.
Try to switch to that alert by,
var alert = driver.SwitchTo().Alert();
alert.SetAuthenticationCredentials("Username", "Pwd");
alert.Accept();
I have tested it and it works for IE11, selenium v3.1.0
Ref: https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IAlert_SetAuthenticationCredentials.htm
Suggesstion 1-Go to internet explorer settings->security settings-> user authentication-> select automatic login with current username and password.
Suggesstion 2- if your application has access to it's API, then login via API, get the authentication token and set the auth.token in browser cookie.

Selenium(chrome) crashes on navigate

I am trying to open url with Selenium and Google chrome, however i always end up with chromedriver.has stopped working.
ChromeDriver driver = new ChromeDriver(#"Path\To\The\Driver");
driver.Navigate().GoToUrl("https://www.google.com/");
i tried to sleep between initializing and going to url, however it does nothing.
As stated above, chromedriver version 2.25 will work. The problem with version 2.25 is that it crashes when running. You can see here the update history.
https://sites.google.com/a/chromium.org/chromedriver/download .
I would suggest you use a later version of the driver.
Here is the link to the drivers that was given to me by visual studio. http://chromedriver.storage.googleapis.com
Choose a chrome driver version 2.37 for windows selenium version 3.11.1.
Any news on this topic? I have the same issue. The execution stops at driver.Navigate().GoToUrl(desiredUri); where the login window is shown. It waits for me to enter the username and password and manually it can be done but the execution has stopped there and it does not go to my next line var alert = driver.SwitchTo().Alert(); from where I want to add the username and password.

cross browser testing using codedui?

i have issue in cross browser testing using codedui.
Using below code,
Process.Start("firefox", url);
BrowserWindow.CurrentBrowser = "firefox";
Browser = BrowserWindow.Launch(new System.Uri(url));
Keyboard.SendKeys("^{0}");
all code developed in IE . but now i have to execute code in firefox or chrome.I am going to execute the code in forefox.I am using this code here
Browser = BrowserWindow.Launch(new System.Uri(url));
in this line getting error like "An error occurred while connecting to Firefox".how to resolve this issue?I installed selenium components also. if i remove this line I am getting diffrent error like " Unable to find browser"...Please help.
Out of the box Visual Studio doesn't support cross browser CodedUI testing.
You're going to need to install Selenium components to allow for cross browser testing in Visual Studio.
Details on that can be found here:
http://blogs.msdn.com/b/visualstudioalm/archive/2012/10/30/introducing-cross-browser-testing-with-coded-ui-tests.aspx
Selenium components can be found here:
http://visualstudiogallery.msdn.microsoft.com/11cfc881-f8c9-4f96-b303-a2780156628d
Looks like CodedUI doesn't support playback on very many different browsers http://msdn.microsoft.com/en-us/library/dd380742(VS.100).aspx
There are some other tools out there http://watin.org/ is one, but I can't find anything myself that will really solve your problem.
Try
BrowserWindow.CurrentBrowser = "firefox";
BrowserWindow WebApp;
WebApp.CopyFrom(BrowserWindow.LaunchUrl(new System.Uri(url)));
I set up something similar to the following and it works well (I hand code everything, bypassing the UIMap).
public class WebApp : BrowserWindow
{
private string _url;
public WebApp(string url)
{
//define search properties using this keyword so the web application can be treated as a browser
_url = url;
BrowserWindow.CurrentBrowser = "Chrome";
this.CopyFrom(BrowserWindow.Launch(new Uri(url));
}
}
You can overload the constructor, of course, by adding a parameter for the browser to use or whether to start up the browser or not.
By setting up the web application as a BrowserWindow, you can have one open and ready and the playback engine should find it. I find this helps when working on tests (in IE).
Just a reminder, you do need the Selenium plug-in and that plug-in will only work for playback, not recording.
Cheers!
In our environment, we set up the Launch() method by doing the following:
public void LaunchBrowser(string uri)
{
BrowserWindow.CurrentBrowser = "firefox";
BrowserWindow myBrowser = BrowserWindow.Launch(new System.Uri(uri));
}
One thing to note is that if there is a firefox process already running in the background, the WebDriver will not launch a new instance, so be sure that all instances of firefox are shut down before the LaunchBrowser() is called. I've found that there are Java plugins that can keep it running in the background, so try to disable those that you don't need. Another good place to look, if you check your Task Manager and this is the case, is here.

Categories