|
| 1 | +// <copyright file="ShadowRoot.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 | +using System; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.Collections.ObjectModel; |
| 22 | + |
| 23 | +namespace OpenQA.Selenium |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Provides a representation of an element's shadow root. |
| 27 | + /// </summary> |
| 28 | + public class ShadowRoot : ISearchContext, IWrapsDriver |
| 29 | + { |
| 30 | + /// <summary> |
| 31 | + /// The property name that represents an element shadow root in the wire protocol. |
| 32 | + /// </summary> |
| 33 | + public const string ShadowRootReferencePropertyName = "shadow-6066-11e4-a52e-4f735466cecf"; |
| 34 | + |
| 35 | + private WebDriver driver; |
| 36 | + private string shadowRootId; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes a new instance of the <see cref="ShadowRoot"/> class. |
| 40 | + /// </summary> |
| 41 | + /// <param name="parentDriver">The <see cref="WebDriver"/> instance that is driving this shadow root.</param> |
| 42 | + /// <param name="id">The ID value provided to identify the shadow root.</param> |
| 43 | + public ShadowRoot(WebDriver parentDriver, string id) |
| 44 | + { |
| 45 | + this.driver = parentDriver; |
| 46 | + this.shadowRootId = id; |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets the <see cref="IWebDriver"/> driving this shadow root. |
| 51 | + /// </summary> |
| 52 | + public IWebDriver WrappedDriver |
| 53 | + { |
| 54 | + get { return this.driver; } |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Finds the first <see cref="IWebElement"/> using the given method. |
| 59 | + /// </summary> |
| 60 | + /// <param name="by">The locating mechanism to use.</param> |
| 61 | + /// <returns>The first matching <see cref="IWebElement"/> on the current context.</returns> |
| 62 | + /// <exception cref="NoSuchElementException">If no element matches the criteria.</exception> |
| 63 | + public IWebElement FindElement(By by) |
| 64 | + { |
| 65 | + if (by == null) |
| 66 | + { |
| 67 | + throw new ArgumentNullException("by", "by cannot be null"); |
| 68 | + } |
| 69 | + |
| 70 | + Dictionary<string, object> parameters = new Dictionary<string, object>(); |
| 71 | + parameters.Add("id", this.shadowRootId); |
| 72 | + parameters.Add("using", by.Mechanism); |
| 73 | + parameters.Add("value", by.Criteria); |
| 74 | + Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElement, parameters); |
| 75 | + return this.driver.GetElementFromResponse(commandResponse); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Finds all <see cref="IWebElement">IWebElements</see> within the current context |
| 80 | + /// using the given mechanism. |
| 81 | + /// </summary> |
| 82 | + /// <param name="by">The locating mechanism to use.</param> |
| 83 | + /// <returns>A <see cref="ReadOnlyCollection{T}"/> of all <see cref="IWebElement">WebElements</see> |
| 84 | + /// matching the current criteria, or an empty list if nothing matches.</returns> |
| 85 | + public ReadOnlyCollection<IWebElement> FindElements(By by) |
| 86 | + { |
| 87 | + if (by == null) |
| 88 | + { |
| 89 | + throw new ArgumentNullException("by", "by cannot be null"); |
| 90 | + } |
| 91 | + |
| 92 | + Dictionary<string, object> parameters = new Dictionary<string, object>(); |
| 93 | + parameters.Add("id", this.shadowRootId); |
| 94 | + parameters.Add("using", by.Mechanism); |
| 95 | + parameters.Add("value", by.Criteria); |
| 96 | + Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElements, parameters); |
| 97 | + return this.driver.GetElementsFromResponse(commandResponse); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments