Skip to content

Commit

Permalink
Adding support for authentication dialogs in the .NET bindings
Browse files Browse the repository at this point in the history
This commit includes adding tests for the new authentication
functionality.
  • Loading branch information
jimevans committed Jul 14, 2015
1 parent 7604d12 commit ca03a1d
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dotnet/src/webdriver/IAlert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@ public interface IAlert
/// </summary>
/// <param name="keysToSend">The keystrokes to send.</param>
void SendKeys(string keysToSend);

/// <summary>
/// Sets the user name and password in an alert prompting for credentials.
/// </summary>
/// <param name="userName">The user name to set.</param>
/// <param name="password">The password to set.</param>
void SetAuthenticationCredentials(string userName, string password);
}
}
1 change: 1 addition & 0 deletions dotnet/src/webdriver/Remote/CommandInfoRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ private void InitializeCommandDictionary()
this.commandDictionary.Add(DriverCommand.AcceptAlert, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/accept_alert"));
this.commandDictionary.Add(DriverCommand.GetAlertText, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/alert_text"));
this.commandDictionary.Add(DriverCommand.SetAlertValue, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/alert_text"));
this.commandDictionary.Add(DriverCommand.SetAlertCredentials, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/alert/credentials"));
this.commandDictionary.Add(DriverCommand.SetTimeout, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/timeouts"));
this.commandDictionary.Add(DriverCommand.ImplicitlyWait, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/timeouts/implicit_wait"));
this.commandDictionary.Add(DriverCommand.SetAsyncScriptTimeout, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/timeouts/async_script"));
Expand Down
5 changes: 5 additions & 0 deletions dotnet/src/webdriver/Remote/DriverCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ public static class DriverCommand
/// </summary>
public static readonly string SetAlertValue = "setAlertValue";

/// <summary>
/// Represents the Authenticate command
/// </summary>
public static readonly string SetAlertCredentials = "setAlertCredentials";

/// <summary>
/// Represents the ImplicitlyWait command
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions dotnet/src/webdriver/Remote/RemoteAlert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ public void SendKeys(string keysToSend)
parameters.Add("text", keysToSend);
this.driver.InternalExecute(DriverCommand.SetAlertValue, parameters);
}

/// <summary>
/// Sets the user name and password in an alert prompting for credentials.
/// </summary>
/// <param name="userName">The user name to set.</param>
/// <param name="password">The password to set.</param>
public void SetAuthenticationCredentials(string userName, string password)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("username", userName);
parameters.Add("password", password);
this.driver.InternalExecute(DriverCommand.SetAlertCredentials, parameters);
}
#endregion
}
}
5 changes: 5 additions & 0 deletions dotnet/src/webdriver/UnhandledAlertException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public void SendKeys(string keysToSend)
ThrowAlreadyDismissed();
}

public void SetAuthenticationCredentials(string userName, string password)
{
ThrowAlreadyDismissed();
}

private static void ThrowAlreadyDismissed()
{
throw new InvalidOperationException("Alert was already dismissed");
Expand Down
71 changes: 71 additions & 0 deletions dotnet/test/common/AlertsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,77 @@ public void ShouldThrowAnExceptionIfAnAlertHasNotBeenDealtWithAndDismissTheAlert
Assert.AreEqual("Testing Alerts", driver.Title);
}

[Test]
[Category("JavaScript")]
[IgnoreBrowser(Browser.Android)]
[IgnoreBrowser(Browser.Chrome)]
[IgnoreBrowser(Browser.Firefox)]
[IgnoreBrowser(Browser.HtmlUnit)]
[IgnoreBrowser(Browser.IPhone)]
[IgnoreBrowser(Browser.Opera)]
[IgnoreBrowser(Browser.PhantomJS, "Alert commands not yet implemented in GhostDriver")]
[IgnoreBrowser(Browser.Remote)]
[IgnoreBrowser(Browser.Safari)]
[IgnoreBrowser(Browser.WindowsPhone, "Alert handling not yet implemented on Windows Phone")]
public void ShouldBeAbleToHandleAuthenticationDialog()
{
driver.Url = authenticationPage;
IAlert alert = WaitFor<IAlert>(AlertToBePresent);
alert.SetAuthenticationCredentials("test", "test");
alert.Accept();
Assert.IsTrue(driver.FindElement(By.TagName("h1")).Text.Contains("authorized"));
}

[Test]
[Category("JavaScript")]
[IgnoreBrowser(Browser.Android)]
[IgnoreBrowser(Browser.Chrome)]
[IgnoreBrowser(Browser.Firefox)]
[IgnoreBrowser(Browser.HtmlUnit)]
[IgnoreBrowser(Browser.IPhone)]
[IgnoreBrowser(Browser.Opera)]
[IgnoreBrowser(Browser.PhantomJS, "Alert commands not yet implemented in GhostDriver")]
[IgnoreBrowser(Browser.Remote)]
[IgnoreBrowser(Browser.Safari)]
[IgnoreBrowser(Browser.WindowsPhone, "Alert handling not yet implemented on Windows Phone")]
public void ShouldBeAbleToDismissAuthenticationDialog()
{
driver.Url = authenticationPage;
IAlert alert = WaitFor<IAlert>(AlertToBePresent);
alert.Dismiss();
}

[Test]
[Category("JavaScript")]
[IgnoreBrowser(Browser.Android)]
[IgnoreBrowser(Browser.Chrome)]
[IgnoreBrowser(Browser.Firefox)]
[IgnoreBrowser(Browser.HtmlUnit)]
[IgnoreBrowser(Browser.IPhone)]
[IgnoreBrowser(Browser.Opera)]
[IgnoreBrowser(Browser.PhantomJS, "Alert commands not yet implemented in GhostDriver")]
[IgnoreBrowser(Browser.Remote)]
[IgnoreBrowser(Browser.Safari)]
[IgnoreBrowser(Browser.WindowsPhone, "Alert handling not yet implemented on Windows Phone")]
public void ShouldThrowAuthenticatingOnStandardAlert()
{
driver.Url = alertsPage;
driver.FindElement(By.Id("alert")).Click();
IAlert alert = WaitFor<IAlert>(AlertToBePresent);
try
{
alert.SetAuthenticationCredentials("test", "test");
Assert.Fail("Should not be able to Authenticate");
}
catch (UnhandledAlertException)
{
// this is an expected exception
}

// but the next call should be good.
alert.Dismiss();
}

private IAlert AlertToBePresent()
{
return driver.SwitchTo().Alert();
Expand Down
1 change: 1 addition & 0 deletions dotnet/test/common/DriverTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public abstract class DriverTestFixture
public string slowLoadingAlertPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("slowLoadingAlert.html");
public string dragDropOverflowPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("dragDropOverflow.html");
public string missedJsReferencePage = EnvironmentManager.Instance.UrlBuilder.WhereIs("missedJsReference.html");
public string authenticationPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("basicAuth");

protected IWebDriver driver;

Expand Down
1 change: 1 addition & 0 deletions dotnet/test/ie/WebDriver.IE.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<ItemGroup>
<None Include="WebDriver.IE.Tests.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<SubType>Designer</SubType>
</None>
<None Include="WebDriver.IE.Tests.nunit">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down

0 comments on commit ca03a1d

Please sign in to comment.