forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargetLocator.cs
241 lines (215 loc) · 9.95 KB
/
TargetLocator.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// <copyright file="TargetLocator.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.Text.RegularExpressions;
#nullable enable
namespace OpenQA.Selenium
{
/// <summary>
/// Provides a mechanism for finding elements on the page with locators.
/// </summary>
internal sealed class TargetLocator : ITargetLocator
{
private readonly WebDriver driver;
/// <summary>
/// Initializes a new instance of the <see cref="TargetLocator"/> class
/// </summary>
/// <param name="driver">The driver that is currently in use</param>
/// <exception cref="ArgumentNullException">If <paramref name="driver"/> is <see langword="null"/>.</exception>
public TargetLocator(WebDriver driver)
{
this.driver = driver ?? throw new ArgumentNullException(nameof(driver));
}
/// <summary>
/// Move to a different frame using its index
/// </summary>
/// <param name="frameIndex">The index of the </param>
/// <returns>A WebDriver instance that is currently in use</returns>
public IWebDriver Frame(int frameIndex)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("id", frameIndex);
this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters);
return this.driver;
}
/// <summary>
/// Move to different frame using its name
/// </summary>
/// <param name="frameName">name of the frame</param>
/// <returns>A WebDriver instance that is currently in use</returns>
/// <exception cref="ArgumentNullException">If <paramref name="frameName"/> is <see langword="null"/>.</exception>
public IWebDriver Frame(string frameName)
{
if (frameName == null)
{
throw new ArgumentNullException(nameof(frameName), "Frame name cannot be null");
}
string name = Regex.Replace(frameName, @"(['""\\#.:;,!?+<>=~*^$|%&@`{}\-/\[\]\(\)])", @"\$1");
ReadOnlyCollection<IWebElement> frameElements = this.driver.FindElements(By.CssSelector("frame[name='" + name + "'],iframe[name='" + name + "']"));
if (frameElements.Count == 0)
{
frameElements = this.driver.FindElements(By.CssSelector("frame#" + name + ",iframe#" + name));
if (frameElements.Count == 0)
{
throw new NoSuchFrameException("No frame element found with name or id " + frameName);
}
}
return this.Frame(frameElements[0]);
}
/// <summary>
/// Move to a frame element.
/// </summary>
/// <param name="frameElement">a previously found FRAME or IFRAME element.</param>
/// <returns>A WebDriver instance that is currently in use.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="frameElement"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If <paramref name="frameElement"/> cannot be converted to an <see cref="IWebDriverObjectReference"/>.</exception>
public IWebDriver Frame(IWebElement frameElement)
{
if (frameElement == null)
{
throw new ArgumentNullException(nameof(frameElement), "Frame element cannot be null");
}
IWebDriverObjectReference? elementReference = frameElement as IWebDriverObjectReference;
if (elementReference == null)
{
if (frameElement is IWrapsElement elementWrapper)
{
elementReference = elementWrapper.WrappedElement as IWebDriverObjectReference;
}
}
if (elementReference == null)
{
throw new ArgumentException($"{nameof(frameElement)} cannot be converted to {nameof(IWebDriverObjectReference)}", nameof(frameElement));
}
Dictionary<string, object> elementDictionary = elementReference.ToDictionary();
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("id", elementDictionary);
this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters);
return this.driver;
}
/// <summary>
/// Select the parent frame of the currently selected frame.
/// </summary>
/// <returns>An <see cref="IWebDriver"/> instance focused on the specified frame.</returns>
public IWebDriver ParentFrame()
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
this.driver.InternalExecute(DriverCommand.SwitchToParentFrame, parameters);
return this.driver;
}
/// <summary>
/// Change to the Window by passing in the name
/// </summary>
/// <param name="windowHandleOrName">Window handle or name of the window that you wish to move to</param>
/// <returns>A WebDriver instance that is currently in use</returns>
/// <exception cref="ArgumentNullException">If <paramref name="windowHandleOrName"/> is <see langword="null"/>.</exception>
public IWebDriver Window(string windowHandleOrName)
{
if (windowHandleOrName is null)
{
throw new ArgumentNullException(nameof(windowHandleOrName));
}
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("handle", windowHandleOrName);
try
{
this.driver.InternalExecute(DriverCommand.SwitchToWindow, parameters);
return this.driver;
}
catch (NoSuchWindowException)
{
// simulate search by name
string? original = null;
try
{
original = this.driver.CurrentWindowHandle;
}
catch (NoSuchWindowException)
{
}
foreach (string handle in this.driver.WindowHandles)
{
this.Window(handle);
if (windowHandleOrName == this.driver.ExecuteScript("return window.name")!.ToString())
{
return this.driver; // found by name
}
}
if (original != null)
{
this.Window(original);
}
throw;
}
}
/// <summary>
/// Creates a new browser window and switches the focus for future commands
/// of this driver to the new window.
/// </summary>
/// <param name="typeHint">The type of new browser window to be created.
/// The created window is not guaranteed to be of the requested type; if
/// the driver does not support the requested type, a new browser window
/// will be created of whatever type the driver does support.</param>
/// <returns>An <see cref="IWebDriver"/> instance focused on the new browser.</returns>
public IWebDriver NewWindow(WindowType typeHint)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("type", typeHint.ToString().ToLowerInvariant());
Response response = this.driver.InternalExecute(DriverCommand.NewWindow, parameters);
Dictionary<string, object> result = (Dictionary<string, object>)response.Value!;
string newWindowHandle = result["handle"].ToString()!;
this.Window(newWindowHandle);
return this.driver;
}
/// <summary>
/// Change the active frame to the default
/// </summary>
/// <returns>Element of the default</returns>
public IWebDriver DefaultContent()
{
Dictionary<string, object?> parameters = new Dictionary<string, object?>();
parameters.Add("id", null);
this.driver.InternalExecute(DriverCommand.SwitchToFrame, parameters);
return this.driver;
}
/// <summary>
/// Finds the active element on the page and returns it
/// </summary>
/// <returns>Element that is active</returns>
public IWebElement ActiveElement()
{
Response response = this.driver.InternalExecute(DriverCommand.GetActiveElement, null);
return this.driver.GetElementFromResponse(response)!;
}
/// <summary>
/// Switches to the currently active modal dialog for this particular driver instance.
/// </summary>
/// <returns>A handle to the dialog.</returns>
public IAlert Alert()
{
// N.B. We only execute the GetAlertText command to be able to throw
// a NoAlertPresentException if there is no alert found.
this.driver.InternalExecute(DriverCommand.GetAlertText, null);
return new Alert(this.driver);
}
}
}