Skip to content

Commit 5764ff9

Browse files
nvborisenkodiemol
andauthored
[dotnet] Be more friendly for single file publishing and native compilation (#12045)
* Use GetExecutingAssembly instead of GetCallingAssembly * Make determination of selenium manager easier * Invert conditional statement for net45 * Even remove normalizing of base selenium maager directory seems no cases to entrace into this if statement * Remove unnecessary local variable assigment --------- Co-authored-by: Diego Molina <[email protected]>
1 parent da0dcef commit 5764ff9

File tree

2 files changed

+47
-66
lines changed

2 files changed

+47
-66
lines changed

dotnet/src/webdriver/Internal/ResourceUtilities.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace OpenQA.Selenium.Internal
2929
/// <summary>
3030
/// Encapsulates methods for finding and extracting WebDriver resources.
3131
/// </summary>
32-
public static class ResourceUtilities
32+
internal static class ResourceUtilities
3333
{
3434
private static string assemblyVersion;
3535
private static string platformFamily;
@@ -43,7 +43,7 @@ public static string AssemblyVersion
4343
{
4444
if (string.IsNullOrEmpty(assemblyVersion))
4545
{
46-
Assembly executingAssembly = Assembly.GetCallingAssembly();
46+
Assembly executingAssembly = Assembly.GetExecutingAssembly();
4747
Version versionResource = executingAssembly.GetName().Version;
4848
assemblyVersion = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", versionResource.Major, versionResource.Minor, versionResource.Revision);
4949
}
@@ -106,7 +106,7 @@ public static Stream GetResourceStream(string fileName, string resourceId)
106106
throw new WebDriverException("The file specified does not exist, and you have specified no internal resource ID");
107107
}
108108

109-
Assembly executingAssembly = Assembly.GetCallingAssembly();
109+
Assembly executingAssembly = Assembly.GetExecutingAssembly();
110110
resourceStream = executingAssembly.GetManifestResourceStream(resourceId);
111111
}
112112

@@ -118,18 +118,6 @@ public static Stream GetResourceStream(string fileName, string resourceId)
118118
return resourceStream;
119119
}
120120

121-
/// <summary>
122-
/// Returns a value indicating whether a resource exists with the specified ID.
123-
/// </summary>
124-
/// <param name="resourceId">ID of the embedded resource to check for.</param>
125-
/// <returns><see langword="true"/> if the resource exists in the calling assembly; otherwise <see langword="false"/>.</returns>
126-
public static bool IsValidResourceName(string resourceId)
127-
{
128-
Assembly executingAssembly = Assembly.GetCallingAssembly();
129-
List<string> resourceNames = new List<string>(executingAssembly.GetManifestResourceNames());
130-
return resourceNames.Contains(resourceId);
131-
}
132-
133121
private static string GetPlatformString()
134122
{
135123
string platformName = "unknown";

dotnet/src/webdriver/SeleniumManager.cs

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Collections.Generic;
2323
using System.Diagnostics;
2424
using System.Globalization;
25+
using System.IO;
2526
using System.Reflection;
2627

2728
#if !NET45 && !NET46 && !NET47
@@ -38,8 +39,40 @@ namespace OpenQA.Selenium
3839
/// </summary>
3940
public static class SeleniumManager
4041
{
41-
private static string basePath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
42-
private static string binary;
42+
private readonly static string binaryFullPath;
43+
44+
static SeleniumManager()
45+
{
46+
#if NET45
47+
var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
48+
#else
49+
var currentDirectory = AppContext.BaseDirectory;
50+
#endif
51+
52+
string binary;
53+
#if NET45 || NET46 || NET47
54+
binary = "selenium-manager/windows/selenium-manager.exe";
55+
#else
56+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
57+
{
58+
binary = "selenium-manager/windows/selenium-manager.exe";
59+
}
60+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
61+
{
62+
binary = "selenium-manager/linux/selenium-manager";
63+
}
64+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
65+
{
66+
binary = "selenium-manager/macos/selenium-manager";
67+
}
68+
else
69+
{
70+
throw new WebDriverException("Selenium Manager did not find supported operating system");
71+
}
72+
#endif
73+
74+
binaryFullPath = Path.Combine(currentDirectory, binary);
75+
}
4376

4477
/// <summary>
4578
/// Determines the location of the correct driver.
@@ -50,9 +83,6 @@ public static class SeleniumManager
5083
/// </returns>
5184
public static string DriverPath(DriverOptions options)
5285
{
53-
var binaryFile = Binary;
54-
if (binaryFile == null) return null;
55-
5686
StringBuilder argsBuilder = new StringBuilder();
5787
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser \"{0}\"", options.BrowserName);
5888
argsBuilder.Append(" --output json");
@@ -68,8 +98,7 @@ public static string DriverPath(DriverOptions options)
6898
argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-path \"{0}\"", browserBinary);
6999
}
70100

71-
72-
return RunCommand(binaryFile, argsBuilder.ToString());
101+
return RunCommand(binaryFullPath, argsBuilder.ToString());
73102
}
74103

75104

@@ -96,40 +125,6 @@ private static string BrowserBinary(DriverOptions options)
96125
return null;
97126
}
98127

99-
/// <summary>
100-
/// Gets the location of the correct Selenium Manager binary.
101-
/// </summary>
102-
private static string Binary
103-
{
104-
get
105-
{
106-
if (string.IsNullOrEmpty(binary))
107-
{
108-
#if NET45 || NET46 || NET47
109-
binary = "selenium-manager/windows/selenium-manager.exe";
110-
#else
111-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
112-
{
113-
binary = "selenium-manager/windows/selenium-manager.exe";
114-
}
115-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
116-
{
117-
binary = "selenium-manager/linux/selenium-manager";
118-
}
119-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
120-
{
121-
binary = "selenium-manager/macos/selenium-manager";
122-
}
123-
else
124-
{
125-
throw new WebDriverException("Selenium Manager did not find supported operating system");
126-
}
127-
#endif
128-
}
129-
130-
return binary;
131-
}
132-
}
133128

134129
/// <summary>
135130
/// Executes a process with the given arguments.
@@ -142,7 +137,7 @@ private static string Binary
142137
private static string RunCommand(string fileName, string arguments)
143138
{
144139
Process process = new Process();
145-
process.StartInfo.FileName = $"{basePath}/{fileName}";
140+
process.StartInfo.FileName = binaryFullPath;
146141
process.StartInfo.Arguments = arguments;
147142
process.StartInfo.UseShellExecute = false;
148143
process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
@@ -151,7 +146,6 @@ private static string RunCommand(string fileName, string arguments)
151146
process.StartInfo.RedirectStandardError = true;
152147

153148
StringBuilder outputBuilder = new StringBuilder();
154-
int processExitCode;
155149

156150
DataReceivedEventHandler outputHandler = (sender, e) => outputBuilder.AppendLine(e.Data);
157151

@@ -164,14 +158,20 @@ private static string RunCommand(string fileName, string arguments)
164158
process.BeginOutputReadLine();
165159

166160
process.WaitForExit();
161+
162+
if (process.ExitCode != 0)
163+
{
164+
// 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
165+
166+
throw new WebDriverException($"Selenium Manager process exited abnormally with {process.ExitCode} code: {fileName} {arguments}\n{outputBuilder}");
167+
}
167168
}
168169
catch (Exception ex)
169170
{
170171
throw new WebDriverException($"Error starting process: {fileName} {arguments}", ex);
171172
}
172173
finally
173174
{
174-
processExitCode = process.ExitCode;
175175
process.OutputDataReceived -= outputHandler;
176176
}
177177

@@ -188,13 +188,6 @@ private static string RunCommand(string fileName, string arguments)
188188
throw new WebDriverException($"Error deserializing Selenium Manager's response: {output}", ex);
189189
}
190190

191-
// 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
192-
193-
if (processExitCode != 0)
194-
{
195-
throw new WebDriverException($"Invalid response from process (code {processExitCode}): {fileName} {arguments}\n{result}");
196-
}
197-
198191
return result;
199192
}
200193
}

0 commit comments

Comments
 (0)