Skip to content

Commit ca03a1d

Browse files
committed
Adding support for authentication dialogs in the .NET bindings
This commit includes adding tests for the new authentication functionality.
1 parent 7604d12 commit ca03a1d

8 files changed

+104
-0
lines changed

Diff for: dotnet/src/webdriver/IAlert.cs

+7
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,12 @@ public interface IAlert
4747
/// </summary>
4848
/// <param name="keysToSend">The keystrokes to send.</param>
4949
void SendKeys(string keysToSend);
50+
51+
/// <summary>
52+
/// Sets the user name and password in an alert prompting for credentials.
53+
/// </summary>
54+
/// <param name="userName">The user name to set.</param>
55+
/// <param name="password">The password to set.</param>
56+
void SetAuthenticationCredentials(string userName, string password);
5057
}
5158
}

Diff for: dotnet/src/webdriver/Remote/CommandInfoRepository.cs

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ private void InitializeCommandDictionary()
189189
this.commandDictionary.Add(DriverCommand.AcceptAlert, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/accept_alert"));
190190
this.commandDictionary.Add(DriverCommand.GetAlertText, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/alert_text"));
191191
this.commandDictionary.Add(DriverCommand.SetAlertValue, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/alert_text"));
192+
this.commandDictionary.Add(DriverCommand.SetAlertCredentials, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/alert/credentials"));
192193
this.commandDictionary.Add(DriverCommand.SetTimeout, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/timeouts"));
193194
this.commandDictionary.Add(DriverCommand.ImplicitlyWait, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/timeouts/implicit_wait"));
194195
this.commandDictionary.Add(DriverCommand.SetAsyncScriptTimeout, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/timeouts/async_script"));

Diff for: dotnet/src/webdriver/Remote/DriverCommand.cs

+5
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ public static class DriverCommand
318318
/// </summary>
319319
public static readonly string SetAlertValue = "setAlertValue";
320320

321+
/// <summary>
322+
/// Represents the Authenticate command
323+
/// </summary>
324+
public static readonly string SetAlertCredentials = "setAlertCredentials";
325+
321326
/// <summary>
322327
/// Represents the ImplicitlyWait command
323328
/// </summary>

Diff for: dotnet/src/webdriver/Remote/RemoteAlert.cs

+13
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ public void SendKeys(string keysToSend)
7777
parameters.Add("text", keysToSend);
7878
this.driver.InternalExecute(DriverCommand.SetAlertValue, parameters);
7979
}
80+
81+
/// <summary>
82+
/// Sets the user name and password in an alert prompting for credentials.
83+
/// </summary>
84+
/// <param name="userName">The user name to set.</param>
85+
/// <param name="password">The password to set.</param>
86+
public void SetAuthenticationCredentials(string userName, string password)
87+
{
88+
Dictionary<string, object> parameters = new Dictionary<string, object>();
89+
parameters.Add("username", userName);
90+
parameters.Add("password", password);
91+
this.driver.InternalExecute(DriverCommand.SetAlertCredentials, parameters);
92+
}
8093
#endregion
8194
}
8295
}

Diff for: dotnet/src/webdriver/UnhandledAlertException.cs

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ public void SendKeys(string keysToSend)
146146
ThrowAlreadyDismissed();
147147
}
148148

149+
public void SetAuthenticationCredentials(string userName, string password)
150+
{
151+
ThrowAlreadyDismissed();
152+
}
153+
149154
private static void ThrowAlreadyDismissed()
150155
{
151156
throw new InvalidOperationException("Alert was already dismissed");

Diff for: dotnet/test/common/AlertsTest.cs

+71
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,77 @@ public void ShouldThrowAnExceptionIfAnAlertHasNotBeenDealtWithAndDismissTheAlert
718718
Assert.AreEqual("Testing Alerts", driver.Title);
719719
}
720720

721+
[Test]
722+
[Category("JavaScript")]
723+
[IgnoreBrowser(Browser.Android)]
724+
[IgnoreBrowser(Browser.Chrome)]
725+
[IgnoreBrowser(Browser.Firefox)]
726+
[IgnoreBrowser(Browser.HtmlUnit)]
727+
[IgnoreBrowser(Browser.IPhone)]
728+
[IgnoreBrowser(Browser.Opera)]
729+
[IgnoreBrowser(Browser.PhantomJS, "Alert commands not yet implemented in GhostDriver")]
730+
[IgnoreBrowser(Browser.Remote)]
731+
[IgnoreBrowser(Browser.Safari)]
732+
[IgnoreBrowser(Browser.WindowsPhone, "Alert handling not yet implemented on Windows Phone")]
733+
public void ShouldBeAbleToHandleAuthenticationDialog()
734+
{
735+
driver.Url = authenticationPage;
736+
IAlert alert = WaitFor<IAlert>(AlertToBePresent);
737+
alert.SetAuthenticationCredentials("test", "test");
738+
alert.Accept();
739+
Assert.IsTrue(driver.FindElement(By.TagName("h1")).Text.Contains("authorized"));
740+
}
741+
742+
[Test]
743+
[Category("JavaScript")]
744+
[IgnoreBrowser(Browser.Android)]
745+
[IgnoreBrowser(Browser.Chrome)]
746+
[IgnoreBrowser(Browser.Firefox)]
747+
[IgnoreBrowser(Browser.HtmlUnit)]
748+
[IgnoreBrowser(Browser.IPhone)]
749+
[IgnoreBrowser(Browser.Opera)]
750+
[IgnoreBrowser(Browser.PhantomJS, "Alert commands not yet implemented in GhostDriver")]
751+
[IgnoreBrowser(Browser.Remote)]
752+
[IgnoreBrowser(Browser.Safari)]
753+
[IgnoreBrowser(Browser.WindowsPhone, "Alert handling not yet implemented on Windows Phone")]
754+
public void ShouldBeAbleToDismissAuthenticationDialog()
755+
{
756+
driver.Url = authenticationPage;
757+
IAlert alert = WaitFor<IAlert>(AlertToBePresent);
758+
alert.Dismiss();
759+
}
760+
761+
[Test]
762+
[Category("JavaScript")]
763+
[IgnoreBrowser(Browser.Android)]
764+
[IgnoreBrowser(Browser.Chrome)]
765+
[IgnoreBrowser(Browser.Firefox)]
766+
[IgnoreBrowser(Browser.HtmlUnit)]
767+
[IgnoreBrowser(Browser.IPhone)]
768+
[IgnoreBrowser(Browser.Opera)]
769+
[IgnoreBrowser(Browser.PhantomJS, "Alert commands not yet implemented in GhostDriver")]
770+
[IgnoreBrowser(Browser.Remote)]
771+
[IgnoreBrowser(Browser.Safari)]
772+
[IgnoreBrowser(Browser.WindowsPhone, "Alert handling not yet implemented on Windows Phone")]
773+
public void ShouldThrowAuthenticatingOnStandardAlert()
774+
{
775+
driver.Url = alertsPage;
776+
driver.FindElement(By.Id("alert")).Click();
777+
IAlert alert = WaitFor<IAlert>(AlertToBePresent);
778+
try
779+
{
780+
alert.SetAuthenticationCredentials("test", "test");
781+
Assert.Fail("Should not be able to Authenticate");
782+
}
783+
catch (UnhandledAlertException)
784+
{
785+
// this is an expected exception
786+
}
787+
788+
// but the next call should be good.
789+
alert.Dismiss();
790+
}
791+
721792
private IAlert AlertToBePresent()
722793
{
723794
return driver.SwitchTo().Alert();

Diff for: dotnet/test/common/DriverTestFixture.cs

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public abstract class DriverTestFixture
7878
public string slowLoadingAlertPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("slowLoadingAlert.html");
7979
public string dragDropOverflowPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("dragDropOverflow.html");
8080
public string missedJsReferencePage = EnvironmentManager.Instance.UrlBuilder.WhereIs("missedJsReference.html");
81+
public string authenticationPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("basicAuth");
8182

8283
protected IWebDriver driver;
8384

Diff for: dotnet/test/ie/WebDriver.IE.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<ItemGroup>
8585
<None Include="WebDriver.IE.Tests.config">
8686
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
87+
<SubType>Designer</SubType>
8788
</None>
8889
<None Include="WebDriver.IE.Tests.nunit">
8990
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

0 commit comments

Comments
 (0)