forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevToolsDomains.cs
140 lines (123 loc) · 6.44 KB
/
DevToolsDomains.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
// <copyright file="DevToolsDomains.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 System;
using System.Collections.Generic;
#nullable enable
namespace OpenQA.Selenium.DevTools
{
/// <summary>
/// Interface providing version-independent implementations of operations available using the DevTools Protocol.
/// </summary>
public abstract class DevToolsDomains
{
// By default, we will look for a supported version within this
// number of versions, as that will most likely still work.
private const int DefaultVersionRange = 5;
// This is the list of known supported DevTools version implementation.
// When new versions are implemented for support, new types must be
// added to this array and to the method below.
private static int[] SupportedDevToolsVersions =>
[
130,
132,
131,
85
];
private static DevToolsDomains? CreateDevToolsDomain(int protocolVersion, DevToolsSession session) => protocolVersion switch
{
130 => new V130.V130Domains(session),
132 => new V132.V132Domains(session),
131 => new V131.V131Domains(session),
85 => new V85.V85Domains(session),
_ => null
};
/// <summary>
/// Gets the version-specific domains for the DevTools session. This value must be cast to a version specific type to be at all useful.
/// </summary>
public abstract DevToolsSessionDomains VersionSpecificDomains { get; }
/// <summary>
/// Gets the object used for manipulating network information in the browser.
/// </summary>
public abstract Network Network { get; }
/// <summary>
/// Gets the object used for manipulating the browser's JavaScript execution.
/// </summary>
public abstract JavaScript JavaScript { get; }
/// <summary>
/// Gets the object used for manipulating DevTools Protocol targets.
/// </summary>
public abstract Target Target { get; }
/// <summary>
/// Gets the object used for manipulating the browser's logs.
/// </summary>
public abstract Log Log { get; }
/// <summary>
/// Initializes the supplied DevTools session's domains for the specified browser version.
/// </summary>
/// <param name="protocolVersion">The version of the DevTools Protocol to use.</param>
/// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param>
/// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns>
/// <exception cref="ArgumentException">If <paramref name="protocolVersion"/> is negative.</exception>
/// <exception cref="WebDriverException">If the desired protocol version is not supported.</exception>
public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session)
{
return InitializeDomains(protocolVersion, session, DefaultVersionRange);
}
/// <summary>
/// Initializes the supplied DevTools session's domains for the specified browser version within the specified number of versions.
/// </summary>
/// <param name="protocolVersion">The version of the DevTools Protocol to use.</param>
/// <param name="session">The <see cref="DevToolsSession"/> for which to initialiize the domains.</param>
/// <param name="versionRange">The range of versions within which to match the provided version number. Defaults to 5 versions.</param>
/// <returns>The <see cref="DevToolsDomains"/> object containing the version-specific domains.</returns>
/// <exception cref="ArgumentException">If <paramref name="protocolVersion"/> is negative.</exception>
/// <exception cref="WebDriverException">If the desired protocol version is not in the supported range.</exception>
public static DevToolsDomains InitializeDomains(int protocolVersion, DevToolsSession session, int versionRange)
{
if (versionRange < 0)
{
throw new ArgumentException("Version range must be positive", nameof(versionRange));
}
// Return fast on an exact match
DevToolsDomains? domains = CreateDevToolsDomain(protocolVersion, session);
if (domains is not null)
{
return domains;
}
return CreateFallbackDomain(protocolVersion, session, versionRange);
}
private static DevToolsDomains CreateFallbackDomain(int desiredVersion, DevToolsSession session, int versionRange)
{
// Get the list of supported versions and sort descending
List<int> supportedVersions = new List<int>(SupportedDevToolsVersions);
supportedVersions.Sort((first, second) => second.CompareTo(first));
foreach (int supportedVersion in supportedVersions)
{
// Match the version with the desired version within the
// version range, using "The Price Is Right" style matching
// (that is, closest without going over).
if (desiredVersion >= supportedVersion && desiredVersion - supportedVersion < versionRange)
{
return CreateDevToolsDomain(supportedVersion, session)!;
}
}
throw new WebDriverException($"DevTools version is not in the supported range. Desired version={desiredVersion}, range={versionRange}. Supported versions: {string.Join(", ", supportedVersions)}");
}
}
}