Skip to content

Commit a90c645

Browse files
committed
Implementing ByIdOrName in .NET support package.
Fixes issue #1175.
1 parent 165b71b commit a90c645

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// <copyright file="ByIdOrName.cs" company="WebDriver Committers">
2+
// Copyright 2007-2012 WebDriver committers
3+
// Copyright 2007-2012 Google Inc.
4+
// Portions copyright 2012 Software Freedom Conservancy
5+
//
6+
// Licensed 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+
using System;
20+
using System.Collections.Generic;
21+
using System.Collections.ObjectModel;
22+
using System.Globalization;
23+
using System.Text;
24+
25+
namespace OpenQA.Selenium.Support.PageObjects
26+
{
27+
/// <summary>
28+
/// Finds element when the id or the name attribute has the specified value.
29+
/// </summary>
30+
public class ByIdOrName : By
31+
{
32+
private string elementIdentifier = string.Empty;
33+
private By idFinder;
34+
private By nameFinder;
35+
36+
/// <summary>
37+
/// Initializes a new instance of the <see cref="ByIdOrName"/> class.
38+
/// </summary>
39+
/// <param name="elementIdentifier">The ID or Name to use in finding the element.</param>
40+
public ByIdOrName(string elementIdentifier)
41+
{
42+
if (string.IsNullOrEmpty(elementIdentifier))
43+
{
44+
throw new ArgumentException("element identifier cannot be null or the empty string", "elementIdentifier");
45+
}
46+
47+
this.elementIdentifier = elementIdentifier;
48+
this.idFinder = By.Id(this.elementIdentifier);
49+
this.nameFinder = By.Name(this.elementIdentifier);
50+
}
51+
52+
/// <summary>
53+
/// Find a single element.
54+
/// </summary>
55+
/// <param name="context">Context used to find the element.</param>
56+
/// <returns>The element that matches</returns>
57+
public override IWebElement FindElement(ISearchContext context)
58+
{
59+
try
60+
{
61+
return this.idFinder.FindElement(context);
62+
}
63+
catch (NoSuchElementException)
64+
{
65+
return this.nameFinder.FindElement(context);
66+
}
67+
}
68+
69+
/// <summary>
70+
/// Finds many elements
71+
/// </summary>
72+
/// <param name="context">Context used to find the element.</param>
73+
/// <returns>A readonly collection of elements that match.</returns>
74+
public override ReadOnlyCollection<IWebElement> FindElements(ISearchContext context)
75+
{
76+
List<IWebElement> elements = new List<IWebElement>();
77+
elements.AddRange(this.idFinder.FindElements(context));
78+
elements.AddRange(this.nameFinder.FindElements(context));
79+
80+
return elements.AsReadOnly();
81+
}
82+
83+
/// <summary>
84+
/// Writes out a description of this By object.
85+
/// </summary>
86+
/// <returns>Converts the value of this instance to a <see cref="System.String"/></returns>
87+
public override string ToString()
88+
{
89+
return string.Format(CultureInfo.InvariantCulture, "ByIdOrName([{0}])", this.elementIdentifier);
90+
}
91+
}
92+
}

dotnet/src/support/WebDriver.Support.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="PageObjects\ByChained.cs" />
8181
<Compile Include="PageObjects\ByFactory.cs" />
8282
<Compile Include="PageObjects\CacheLookupAttribute.cs" />
83+
<Compile Include="PageObjects\ByIdOrName.cs" />
8384
<Compile Include="PageObjects\FindsByAttribute.cs" />
8485
<Compile Include="PageObjects\FindsBySequenceAttribute.cs" />
8586
<Compile Include="PageObjects\How.cs" />

0 commit comments

Comments
 (0)