Skip to content

Commit 794debe

Browse files
committed
Add spec-compliant properties and methods to .NET IWebElement
The W3C WebDriver Specification defines additional commands for web elements. This commit introduces properties and methods for execution of those commands in the .NET language bindings. The added items on IWebElement are: * ComputedAccessibleLabel property: Allows the user to get the result of the accessible name and description computation for the accessible name of the element. * ComputedAccessibleRole property: Allows the user to get the result of the computation of the ARIA role for this element. Note that this property is subject to change its return type, as the specification needs clarification as to the return type. * GetDomProperty method: Allows the user to get the value of an IDL-defined property of the element. These properties are usually accessed via JavaScript. This method replaces the GetProperty method, which is now marked as obsolete, as it is being renamed to GetDomProperty for consistency in the Selenium API. * GetDomAttribute method: Allows the user to get the value of an attribute declared in the HTML markup of the element. This differs from the GetAttribute method, in that it _only_ returns declared attributes, whereas the GetAttribute method will return one of either a declared attribute or an IDL property of the element, without distinction between the two.
1 parent 73ade56 commit 794debe

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

dotnet/src/webdriver/Remote/DriverCommand.cs

+10
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ public static class DriverCommand
273273
/// </summary>
274274
public static readonly string GetElementValueOfCssProperty = "getElementValueOfCssProperty";
275275

276+
/// <summary>
277+
/// Represents GetComputedAccessibleLabel command
278+
/// </summary>
279+
public static readonly string GetComputedAccessibleLabel = "getComputedAccessibleLabel";
280+
281+
/// <summary>
282+
/// Represents GetComputedAccessibleRole command
283+
/// </summary>
284+
public static readonly string GetComputedAccessibleRole = "getComputedAccessibleRole";
285+
276286
/// <summary>
277287
/// Represents ElementEquals command
278288
/// </summary>

dotnet/src/webdriver/Remote/RemoteWebElement.cs

+89-8
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,38 @@ public virtual Point LocationOnScreenOnceScrolledIntoView
217217
}
218218
}
219219

220+
/// <summary>
221+
/// Gets the computed accessible label of this element.
222+
/// </summary>
223+
public virtual string ComputedAccessibleLabel
224+
{
225+
get
226+
{
227+
Dictionary<string, object> parameters = new Dictionary<string, object>();
228+
parameters.Add("id", this.Id);
229+
Response commandResponse = this.Execute(DriverCommand.GetComputedAccessibleLabel, parameters);
230+
return commandResponse.Value.ToString();
231+
}
232+
}
233+
234+
/// <summary>
235+
/// Gets the computed ARIA role for this element.
236+
/// </summary>
237+
public virtual string ComputedAccessibleRole
238+
{
239+
get
240+
{
241+
// TODO: Returning this as a string is incorrect. The W3C WebDriver Specification
242+
// needs to be updated to more throughly document the structure of what is returned
243+
// by this command. Once that is done, a type-safe class will be created, and will
244+
// be returned by this property.
245+
Dictionary<string, object> parameters = new Dictionary<string, object>();
246+
parameters.Add("id", this.Id);
247+
Response commandResponse = this.Execute(DriverCommand.GetComputedAccessibleRole, parameters);
248+
return commandResponse.Value.ToString();
249+
}
250+
}
251+
220252
/// <summary>
221253
/// Gets the coordinates identifying the location of this element using
222254
/// various frames of reference.
@@ -353,15 +385,15 @@ public virtual void Click()
353385
}
354386

355387
/// <summary>
356-
/// Gets the value of the specified attribute for this element.
388+
/// Gets the value of the specified attribute or property for this element.
357389
/// </summary>
358-
/// <param name="attributeName">The name of the attribute.</param>
359-
/// <returns>The attribute's current value. Returns a <see langword="null"/> if the
360-
/// value is not set.</returns>
390+
/// <param name="attributeName">The name of the attribute or property.</param>
391+
/// <returns>The attribute's or property's current value. Returns a <see langword="null"/>
392+
/// if the value is not set.</returns>
361393
/// <remarks>The <see cref="GetAttribute"/> method will return the current value
362-
/// of the attribute, even if the value has been modified after the page has been
363-
/// loaded. Note that the value of the following attributes will be returned even if
364-
/// there is no explicit attribute on the element:
394+
/// of the attribute or property, even if the value has been modified after the page
395+
/// has been loaded. Note that the value of the following attributes will be returned
396+
/// even if there is no explicit attribute on the element:
365397
/// <list type="table">
366398
/// <listheader>
367399
/// <term>Attribute name</term>
@@ -384,6 +416,9 @@ public virtual void Click()
384416
/// <description>Input and other UI elements</description>
385417
/// </item>
386418
/// </list>
419+
/// The method looks both in declared attributes in the HTML markup of the page, and
420+
/// in the properties of the elemnt as found when accessing the element's properties
421+
/// via JavaScript.
387422
/// </remarks>
388423
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
389424
public virtual string GetAttribute(string attributeName)
@@ -414,14 +449,60 @@ public virtual string GetAttribute(string attributeName)
414449
return attributeValue;
415450
}
416451

452+
/// <summary>
453+
/// Gets the value of a declared HTML attribute of this element.
454+
/// </summary>
455+
/// <param name="attributeName">The name of the HTML attribugte to get the value of.</param>
456+
/// <returns>The HTML attribute's current value. Returns a <see langword="null"/> if the
457+
/// value is not set or the declared attribute does not exist.</returns>
458+
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
459+
/// <remarks>
460+
/// As opposed to the <see cref="GetAttribute(string)"/> method, this method
461+
/// only returns attriibutes decalred in the element's HTML markup. To access the value
462+
/// of an IDL property of the element, either use the <see cref="GetAttribute(string)"/>
463+
/// method or the <see cref="GetDomProperty(string)"/> method.
464+
/// </remarks>
465+
public virtual string GetDomAttribute(string attributeName)
466+
{
467+
string attributeValue = string.Empty;
468+
Dictionary<string, object> parameters = new Dictionary<string, object>();
469+
parameters.Add("id", this.Id);
470+
parameters.Add("name", attributeName);
471+
472+
Response commandResponse = this.Execute(DriverCommand.GetElementAttribute, parameters);
473+
if (commandResponse.Value == null)
474+
{
475+
attributeValue = null;
476+
}
477+
else
478+
{
479+
attributeValue = commandResponse.Value.ToString();
480+
}
481+
482+
return attributeValue;
483+
}
484+
417485
/// <summary>
418486
/// Gets the value of a JavaScript property of this element.
419487
/// </summary>
420-
/// <param name="propertyName">The name JavaScript the JavaScript property to get the value of.</param>
488+
/// <param name="propertyName">The name of the JavaScript property to get the value of.</param>
421489
/// <returns>The JavaScript property's current value. Returns a <see langword="null"/> if the
422490
/// value is not set or the property does not exist.</returns>
423491
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
492+
[Obsolete("Use the GetDomProperty method instead.")]
424493
public virtual string GetProperty(string propertyName)
494+
{
495+
return this.GetDomProperty(propertyName);
496+
}
497+
498+
/// <summary>
499+
/// Gets the value of a JavaScript property of this element.
500+
/// </summary>
501+
/// <param name="propertyName">The name of the JavaScript property to get the value of.</param>
502+
/// <returns>The JavaScript property's current value. Returns a <see langword="null"/> if the
503+
/// value is not set or the property does not exist.</returns>
504+
/// <exception cref="StaleElementReferenceException">Thrown when the target element is no longer valid in the document DOM.</exception>
505+
public virtual string GetDomProperty(string propertyName)
425506
{
426507
string propertyValue = string.Empty;
427508
Dictionary<string, object> parameters = new Dictionary<string, object>();

dotnet/src/webdriver/Remote/W3CWireProtocolCommandInfoRepository.cs

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ protected override void InitializeCommandDictionary()
8484
this.TryAddCommand(DriverCommand.GetElementAttribute, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/element/{id}/attribute/{name}"));
8585
this.TryAddCommand(DriverCommand.GetElementProperty, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/element/{id}/property/{name}"));
8686
this.TryAddCommand(DriverCommand.GetElementValueOfCssProperty, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/element/{id}/css/{name}"));
87+
this.TryAddCommand(DriverCommand.GetComputedAccessibleLabel, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/element/{id}/computedlabel"));
88+
this.TryAddCommand(DriverCommand.GetComputedAccessibleRole, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/element/{id}/computedrole"));
8789
this.TryAddCommand(DriverCommand.GetElementText, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/element/{id}/text"));
8890
this.TryAddCommand(DriverCommand.GetElementTagName, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/element/{id}/name"));
8991
this.TryAddCommand(DriverCommand.GetElementRect, new CommandInfo(CommandInfo.GetCommand, "/session/{sessionId}/element/{id}/rect"));

0 commit comments

Comments
 (0)