forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShadowRoot.cs
137 lines (120 loc) · 5.9 KB
/
ShadowRoot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// <copyright file="ShadowRoot.cs" company="Selenium 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>
using OpenQA.Selenium.Internal;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
#nullable enable
namespace OpenQA.Selenium
{
/// <summary>
/// Provides a representation of an element's shadow root.
/// </summary>
public class ShadowRoot : ISearchContext, IWrapsDriver, IWebDriverObjectReference
{
/// <summary>
/// The property name that represents an element shadow root in the wire protocol.
/// </summary>
public const string ShadowRootReferencePropertyName = "shadow-6066-11e4-a52e-4f735466cecf";
private readonly WebDriver driver;
private readonly string shadowRootId;
/// <summary>
/// Initializes a new instance of the <see cref="ShadowRoot"/> class.
/// </summary>
/// <param name="parentDriver">The <see cref="WebDriver"/> instance that is driving this shadow root.</param>
/// <param name="id">The ID value provided to identify the shadow root.</param>
/// <exception cref="ArgumentNullException">If <paramref name="parentDriver"/> or <paramref name="id"/> are <see langword="null"/>.</exception>
public ShadowRoot(WebDriver parentDriver, string id)
{
this.driver = parentDriver ?? throw new ArgumentNullException(nameof(parentDriver));
this.shadowRootId = id ?? throw new ArgumentNullException(nameof(id));
}
/// <summary>
/// Gets the <see cref="IWebDriver"/> driving this shadow root.
/// </summary>
public IWebDriver WrappedDriver => this.driver;
/// <summary>
/// Gets the internal ID for this ShadowRoot.
/// </summary>
string IWebDriverObjectReference.ObjectReferenceId => this.shadowRootId;
internal static bool TryCreate(WebDriver parentDriver, Dictionary<string, object?> shadowRootDictionary, [NotNullWhen(true)] out ShadowRoot? shadowRoot)
{
if (shadowRootDictionary is null)
{
throw new ArgumentNullException(nameof(shadowRootDictionary), "The dictionary containing the shadow root reference cannot be null");
}
if (shadowRootDictionary.TryGetValue(ShadowRootReferencePropertyName, out object? shadowRootValue))
{
shadowRoot = new ShadowRoot(parentDriver, shadowRootValue?.ToString()!);
return true;
}
shadowRoot = null;
return false;
}
/// <summary>
/// Finds the first <see cref="IWebElement"/> using the given method.
/// </summary>
/// <param name="by">The locating mechanism to use.</param>
/// <returns>The first matching <see cref="IWebElement"/> on the current context.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
/// <exception cref="NoSuchElementException">If no element matches the criteria.</exception>
public IWebElement FindElement(By by)
{
if (by is null)
{
throw new ArgumentNullException(nameof(by), "by cannot be null");
}
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("id", this.shadowRootId);
parameters.Add("using", by.Mechanism);
parameters.Add("value", by.Criteria);
Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElement, parameters);
return this.driver.GetElementFromResponse(commandResponse)!;
}
/// <summary>
/// Finds all <see cref="IWebElement">IWebElements</see> within the current context
/// using the given mechanism.
/// </summary>
/// <param name="by">The locating mechanism to use.</param>
/// <returns>A <see cref="ReadOnlyCollection{T}"/> of all <see cref="IWebElement">WebElements</see>
/// matching the current criteria, or an empty list if nothing matches.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="by"/> is <see langword="null"/>.</exception>
public ReadOnlyCollection<IWebElement> FindElements(By by)
{
if (by is null)
{
throw new ArgumentNullException(nameof(by), "by cannot be null");
}
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("id", this.shadowRootId);
parameters.Add("using", by.Mechanism);
parameters.Add("value", by.Criteria);
Response commandResponse = this.driver.InternalExecute(DriverCommand.FindShadowChildElements, parameters);
return this.driver.GetElementsFromResponse(commandResponse);
}
Dictionary<string, object> IWebDriverObjectReference.ToDictionary()
{
return new Dictionary<string, object>
{
[ShadowRootReferencePropertyName] = this.shadowRootId
};
}
}
}