Skip to content

Commit 6dd04e6

Browse files
arnonaxjimevans
authored andcommitted
Added the new value of the element to the ElementValueChanged and ElementValueChanging 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 <james.h.evans.jr@gmail.com>
1 parent 81371b4 commit 6dd04e6

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

dotnet/src/support/Events/EventFiringWebDriver.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ public EventFiringWebDriver(IWebDriver parentDriver)
8484
/// <summary>
8585
/// Fires before the driver changes the value of an element via Clear(), SendKeys() or Toggle().
8686
/// </summary>
87-
public event EventHandler<WebElementEventArgs> ElementValueChanging;
87+
public event EventHandler<WebElementValueEventArgs> ElementValueChanging;
8888

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

9494
/// <summary>
9595
/// Fires before the driver starts to find an element.
@@ -621,7 +621,17 @@ protected virtual void OnElementClicked(WebElementEventArgs e)
621621
/// Raises the <see cref="ElementValueChanging"/> event.
622622
/// </summary>
623623
/// <param name="e">A <see cref="WebElementEventArgs"/> that contains the event data.</param>
624+
[Obsolete("Use the new overload that takes a WebElementValueEventArgs argument")]
624625
protected virtual void OnElementValueChanging(WebElementEventArgs e)
626+
{
627+
this.OnElementValueChanging(new WebElementValueEventArgs(e.Driver, e.Element, null));
628+
}
629+
630+
/// <summary>
631+
/// Raises the <see cref="ElementValueChanging"/> event.
632+
/// </summary>
633+
/// <param name="e">A <see cref="WebElementValueEventArgs"/> that contains the event data.</param>
634+
protected virtual void OnElementValueChanging(WebElementValueEventArgs e)
625635
{
626636
if (this.ElementValueChanging != null)
627637
{
@@ -633,7 +643,17 @@ protected virtual void OnElementValueChanging(WebElementEventArgs e)
633643
/// Raises the <see cref="ElementValueChanged"/> event.
634644
/// </summary>
635645
/// <param name="e">A <see cref="WebElementEventArgs"/> that contains the event data.</param>
646+
[Obsolete("Use the new overload that takes a WebElementValueEventArgs argument")]
636647
protected virtual void OnElementValueChanged(WebElementEventArgs e)
648+
{
649+
this.OnElementValueChanged(e);
650+
}
651+
652+
/// <summary>
653+
/// Raises the <see cref="ElementValueChanged"/> event.
654+
/// </summary>
655+
/// <param name="e">A <see cref="WebElementValueEventArgs"/> that contains the event data.</param>
656+
protected virtual void OnElementValueChanged(WebElementValueEventArgs e)
637657
{
638658
if (this.ElementValueChanged != null)
639659
{
@@ -1337,7 +1357,7 @@ public void Clear()
13371357
{
13381358
try
13391359
{
1340-
WebElementEventArgs e = new WebElementEventArgs(this.parentDriver.WrappedDriver, this.underlyingElement);
1360+
WebElementValueEventArgs e = new WebElementValueEventArgs(this.parentDriver.WrappedDriver, this.underlyingElement, null);
13411361
this.parentDriver.OnElementValueChanging(e);
13421362
this.underlyingElement.Clear();
13431363
this.parentDriver.OnElementValueChanged(e);
@@ -1357,7 +1377,7 @@ public void SendKeys(string text)
13571377
{
13581378
try
13591379
{
1360-
WebElementEventArgs e = new WebElementEventArgs(this.parentDriver.WrappedDriver, this.underlyingElement);
1380+
WebElementValueEventArgs e = new WebElementValueEventArgs(this.parentDriver.WrappedDriver, this.underlyingElement, text);
13611381
this.parentDriver.OnElementValueChanging(e);
13621382
this.underlyingElement.SendKeys(text);
13631383
this.parentDriver.OnElementValueChanged(e);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// <copyright file="WebElementValueEventArgs.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
namespace OpenQA.Selenium.Support.Events
20+
{
21+
/// <summary>
22+
/// Provides data for events related to finding elements.
23+
/// </summary>
24+
public class WebElementValueEventArgs : WebElementEventArgs
25+
{
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="WebElementValueEventArgs"/> class.
28+
/// </summary>
29+
/// <param name="driver">The WebDriver instance used for the action.</param>
30+
/// <param name="element">The element used for the action.</param>
31+
/// <param name="value">The new value for the element.</param>
32+
public WebElementValueEventArgs(IWebDriver driver, IWebElement element, string value)
33+
: base(driver, element)
34+
{
35+
this.Value = value;
36+
}
37+
38+
/// <summary>
39+
/// Gets the Value that is written to the element
40+
/// </summary>
41+
public string Value { get; private set; }
42+
}
43+
}

dotnet/src/support/WebDriver.Support.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net45;net40;net35;netstandard2.0</TargetFrameworks>

dotnet/test/support/Events/EventFiringWebDriverTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,29 @@ public void ShouldFireClickEvent()
8383
Assert.AreEqual(expectedLog, log.ToString());
8484
}
8585

86+
[Test]
87+
public void ShouldFireValueChangedEvent()
88+
{
89+
mockDriver.Setup(_ => _.FindElement(It.IsAny<By>())).Returns(mockElement.Object);
90+
mockElement.Setup(_ => _.Clear());
91+
mockElement.Setup(_ => _.SendKeys(It.IsAny<string>()));
92+
93+
EventFiringWebDriver firingDriver = new EventFiringWebDriver(mockDriver.Object);
94+
firingDriver.ElementValueChanging += (sender, e) => log.AppendFormat("ValueChanging '{0}'", e.Value).AppendLine();
95+
firingDriver.ElementValueChanged += (sender, e) => log.AppendFormat("ValueChanged '{0}'", e.Value).AppendLine();
96+
97+
var element = firingDriver.FindElement(By.Name("foo"));
98+
element.Clear();
99+
element.SendKeys("Dummy Text");
100+
101+
string expectedLog = @"ValueChanging ''
102+
ValueChanged ''
103+
ValueChanging 'Dummy Text'
104+
ValueChanged 'Dummy Text'
105+
";
106+
Assert.AreEqual(expectedLog, log.ToString());
107+
}
108+
86109
[Test]
87110
public void ShouldFireFindByEvent()
88111
{

0 commit comments

Comments
 (0)