-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathSeleniumManager.cs
211 lines (185 loc) · 8.6 KB
/
SeleniumManager.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
// <copyright file="SeleniumManager.cs" company="WebDriver 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 Newtonsoft.Json;
using OpenQA.Selenium.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace OpenQA.Selenium
{
/// <summary>
/// Wrapper for the Selenium Manager binary.
/// This implementation is still in beta, and may change.
/// </summary>
public static class SeleniumManager
{
private static readonly string BinaryFullPath = Environment.GetEnvironmentVariable("SE_MANAGER_PATH");
static SeleniumManager()
{
if (BinaryFullPath == null)
{
var currentDirectory = AppContext.BaseDirectory;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\windows\\selenium-manager.exe");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\linux\\selenium-manager");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
BinaryFullPath = Path.Combine(currentDirectory, "selenium-manager\\macos\\selenium-manager");
}
else
{
throw new PlatformNotSupportedException(
$"Selenium Manager doesn't support your runtime platform: {RuntimeInformation.OSDescription}");
}
}
if (!File.Exists(BinaryFullPath))
{
throw new WebDriverException($"Unable to locate or obtain Selenium Manager binary at {BinaryFullPath}");
}
}
/// <summary>
/// Determines the location of the correct driver.
/// </summary>
/// <param name="options">The correct path depends on which options are being used.</param>
/// <returns>
/// The location of the driver.
/// </returns>
public static string DriverPath(DriverOptions options)
{
StringBuilder argsBuilder = new StringBuilder();
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser \"{0}\"", options.BrowserName);
argsBuilder.Append(" --output json");
if (!string.IsNullOrEmpty(options.BrowserVersion))
{
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-version {0}", options.BrowserVersion);
}
string browserBinary = options.BinaryLocation;
if (!string.IsNullOrEmpty(browserBinary))
{
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-path \"{0}\"", browserBinary);
}
if (options.Proxy != null)
{
if (options.Proxy.SslProxy != null)
{
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --proxy \"{0}\"", options.Proxy.SslProxy);
}
else if (options.Proxy.HttpProxy != null)
{
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --proxy \"{0}\"", options.Proxy.HttpProxy);
}
}
Dictionary<string, object> output = RunCommand(BinaryFullPath, argsBuilder.ToString());
string browserPath = (string)output["browser_path"];
string driverPath = (string)output["driver_path"];
try
{
options.BinaryLocation = browserPath;
options.BrowserVersion = null;
}
catch (NotImplementedException)
{
// Cannot set Browser Location for this driver and that is ok
}
return driverPath;
}
/// <summary>
/// Executes a process with the given arguments.
/// </summary>
/// <param name="fileName">The path to the Selenium Manager.</param>
/// <param name="arguments">The switches to be used by Selenium Manager.</param>
/// <returns>
/// the standard output of the execution.
/// </returns>
private static Dictionary<string, object> RunCommand(string fileName, string arguments)
{
Process process = new Process();
process.StartInfo.FileName = BinaryFullPath;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
StringBuilder outputBuilder = new StringBuilder();
StringBuilder errorOutputBuilder = new StringBuilder();
DataReceivedEventHandler outputHandler = (sender, e) => outputBuilder.AppendLine(e.Data);
DataReceivedEventHandler errorOutputHandler = (sender, e) => errorOutputBuilder.AppendLine(e.Data);
try
{
process.OutputDataReceived += outputHandler;
process.ErrorDataReceived += errorOutputHandler;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
if (process.ExitCode != 0)
{
// We do not log any warnings coming from Selenium Manager like the other bindings as we don't have any logging in the .NET bindings
var exceptionMessageBuilder = new StringBuilder($"Selenium Manager process exited abnormally with {process.ExitCode} code: {fileName} {arguments}");
if (!string.IsNullOrWhiteSpace(errorOutputBuilder.ToString()))
{
exceptionMessageBuilder.AppendLine();
exceptionMessageBuilder.AppendLine("Error Output >>");
exceptionMessageBuilder.Append(errorOutputBuilder);
exceptionMessageBuilder.AppendLine("<<");
}
if (!string.IsNullOrWhiteSpace(outputBuilder.ToString()))
{
exceptionMessageBuilder.AppendLine();
exceptionMessageBuilder.AppendLine("Standard Output >>");
exceptionMessageBuilder.Append(outputBuilder);
exceptionMessageBuilder.AppendLine("<<");
}
throw new WebDriverException(exceptionMessageBuilder.ToString());
}
}
catch (Exception ex)
{
throw new WebDriverException($"Error starting process: {fileName} {arguments}", ex);
}
finally
{
process.OutputDataReceived -= outputHandler;
process.ErrorDataReceived -= errorOutputHandler;
}
string output = outputBuilder.ToString().Trim();
Dictionary<string, object> result;
try
{
Dictionary<string, object> deserializedOutput = JsonConvert.DeserializeObject<Dictionary<string, object>>(output, new ResponseValueJsonConverter());
result = deserializedOutput["result"] as Dictionary<string, object>;
}
catch (Exception ex)
{
throw new WebDriverException($"Error deserializing Selenium Manager's response: {output}", ex);
}
return result;
}
}
}