Skip to content

Commit

Permalink
Added the new value of the element to the ElementValueChanged and Ele…
Browse files Browse the repository at this point in the history
…mentValueChanging event args.

This commit also creates a new overload of OnElementChanging and
OnElementChanged but also keeps the original overloads to preserve
backward compatibility. Additionally, the original overloads are marked
with the [Obsolete] attribute to let users know they should migrate to the
new overloads.

Signed-off-by: Jim Evans <[email protected]>
  • Loading branch information
arnonax authored and jimevans committed Mar 21, 2018
1 parent 81371b4 commit 6dd04e6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
28 changes: 24 additions & 4 deletions dotnet/src/support/Events/EventFiringWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public EventFiringWebDriver(IWebDriver parentDriver)
/// <summary>
/// Fires before the driver changes the value of an element via Clear(), SendKeys() or Toggle().
/// </summary>
public event EventHandler<WebElementEventArgs> ElementValueChanging;
public event EventHandler<WebElementValueEventArgs> ElementValueChanging;

/// <summary>
/// Fires after the driver has changed the value of an element via Clear(), SendKeys() or Toggle().
/// </summary>
public event EventHandler<WebElementEventArgs> ElementValueChanged;
public event EventHandler<WebElementValueEventArgs> ElementValueChanged;

/// <summary>
/// Fires before the driver starts to find an element.
Expand Down Expand Up @@ -621,7 +621,17 @@ protected virtual void OnElementClicked(WebElementEventArgs e)
/// Raises the <see cref="ElementValueChanging"/> event.
/// </summary>
/// <param name="e">A <see cref="WebElementEventArgs"/> that contains the event data.</param>
[Obsolete("Use the new overload that takes a WebElementValueEventArgs argument")]
protected virtual void OnElementValueChanging(WebElementEventArgs e)
{
this.OnElementValueChanging(new WebElementValueEventArgs(e.Driver, e.Element, null));
}

/// <summary>
/// Raises the <see cref="ElementValueChanging"/> event.
/// </summary>
/// <param name="e">A <see cref="WebElementValueEventArgs"/> that contains the event data.</param>
protected virtual void OnElementValueChanging(WebElementValueEventArgs e)
{
if (this.ElementValueChanging != null)
{
Expand All @@ -633,7 +643,17 @@ protected virtual void OnElementValueChanging(WebElementEventArgs e)
/// Raises the <see cref="ElementValueChanged"/> event.
/// </summary>
/// <param name="e">A <see cref="WebElementEventArgs"/> that contains the event data.</param>
[Obsolete("Use the new overload that takes a WebElementValueEventArgs argument")]
protected virtual void OnElementValueChanged(WebElementEventArgs e)
{
this.OnElementValueChanged(e);
}

/// <summary>
/// Raises the <see cref="ElementValueChanged"/> event.
/// </summary>
/// <param name="e">A <see cref="WebElementValueEventArgs"/> that contains the event data.</param>
protected virtual void OnElementValueChanged(WebElementValueEventArgs e)
{
if (this.ElementValueChanged != null)
{
Expand Down Expand Up @@ -1337,7 +1357,7 @@ public void Clear()
{
try
{
WebElementEventArgs e = new WebElementEventArgs(this.parentDriver.WrappedDriver, this.underlyingElement);
WebElementValueEventArgs e = new WebElementValueEventArgs(this.parentDriver.WrappedDriver, this.underlyingElement, null);
this.parentDriver.OnElementValueChanging(e);
this.underlyingElement.Clear();
this.parentDriver.OnElementValueChanged(e);
Expand All @@ -1357,7 +1377,7 @@ public void SendKeys(string text)
{
try
{
WebElementEventArgs e = new WebElementEventArgs(this.parentDriver.WrappedDriver, this.underlyingElement);
WebElementValueEventArgs e = new WebElementValueEventArgs(this.parentDriver.WrappedDriver, this.underlyingElement, text);
this.parentDriver.OnElementValueChanging(e);
this.underlyingElement.SendKeys(text);
this.parentDriver.OnElementValueChanged(e);
Expand Down
43 changes: 43 additions & 0 deletions dotnet/src/support/Events/WebElementValueEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// <copyright file="WebElementValueEventArgs.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

namespace OpenQA.Selenium.Support.Events
{
/// <summary>
/// Provides data for events related to finding elements.
/// </summary>
public class WebElementValueEventArgs : WebElementEventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="WebElementValueEventArgs"/> class.
/// </summary>
/// <param name="driver">The WebDriver instance used for the action.</param>
/// <param name="element">The element used for the action.</param>
/// <param name="value">The new value for the element.</param>
public WebElementValueEventArgs(IWebDriver driver, IWebElement element, string value)
: base(driver, element)
{
this.Value = value;
}

/// <summary>
/// Gets the Value that is written to the element
/// </summary>
public string Value { get; private set; }
}
}
2 changes: 1 addition & 1 deletion dotnet/src/support/WebDriver.Support.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;net40;net35;netstandard2.0</TargetFrameworks>
Expand Down
23 changes: 23 additions & 0 deletions dotnet/test/support/Events/EventFiringWebDriverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ public void ShouldFireClickEvent()
Assert.AreEqual(expectedLog, log.ToString());
}

[Test]
public void ShouldFireValueChangedEvent()
{
mockDriver.Setup(_ => _.FindElement(It.IsAny<By>())).Returns(mockElement.Object);
mockElement.Setup(_ => _.Clear());
mockElement.Setup(_ => _.SendKeys(It.IsAny<string>()));

EventFiringWebDriver firingDriver = new EventFiringWebDriver(mockDriver.Object);
firingDriver.ElementValueChanging += (sender, e) => log.AppendFormat("ValueChanging '{0}'", e.Value).AppendLine();
firingDriver.ElementValueChanged += (sender, e) => log.AppendFormat("ValueChanged '{0}'", e.Value).AppendLine();

var element = firingDriver.FindElement(By.Name("foo"));
element.Clear();
element.SendKeys("Dummy Text");

string expectedLog = @"ValueChanging ''
ValueChanged ''
ValueChanging 'Dummy Text'
ValueChanged 'Dummy Text'
";
Assert.AreEqual(expectedLog, log.ToString());
}

[Test]
public void ShouldFireFindByEvent()
{
Expand Down

0 comments on commit 6dd04e6

Please sign in to comment.