|
17 | 17 | // </copyright>
|
18 | 18 |
|
19 | 19 | using System;
|
| 20 | +using System.Collections.Generic; |
| 21 | +using System.Globalization; |
20 | 22 | using System.IO;
|
| 23 | +using System.Text; |
21 | 24 |
|
22 | 25 | namespace OpenQA.Selenium
|
23 | 26 | {
|
24 | 27 | /// <summary>
|
25 | 28 | /// Finds a driver, checks if the provided path exists, if not, Selenium Manager is used.
|
26 |
| - /// This implementation is still in beta, and may change. |
| 29 | + /// This implementation is still in beta and may change. |
27 | 30 | /// </summary>
|
28 |
| - public static class DriverFinder |
| 31 | + public class DriverFinder |
29 | 32 | {
|
| 33 | + private DriverOptions options; |
| 34 | + private Dictionary<string, string> paths = new Dictionary<string, string>(); |
| 35 | + private const string BrowserPathKey = "browser_path"; |
| 36 | + private const string DriverPathKey = "driver_path"; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes a new instance of the <see cref="DriverFinder"/> class. |
| 40 | + /// </summary> |
| 41 | + public DriverFinder(DriverOptions options) |
| 42 | + { |
| 43 | + this.options = options; |
| 44 | + } |
| 45 | + |
30 | 46 | /// <summary>
|
31 |
| - /// Use Selenium Manager to locate the driver |
| 47 | + /// Gets the browser path retrieved by Selenium Manager |
32 | 48 | /// </summary>
|
33 |
| - /// <param name="options">DriverOptions with the current browser options.</param> |
34 | 49 | /// <returns>
|
35 |
| - /// The full path and name of the driver |
| 50 | + /// The full browser path |
36 | 51 | /// </returns>
|
37 |
| - /// <exception cref="NoSuchDriverException"></exception> |
38 |
| - public static string FullPath(DriverOptions options) |
| 52 | + public string BrowserPath() |
| 53 | + { |
| 54 | + return BinaryPaths()[BrowserPathKey]; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Gets the driver path retrieved by Selenium Manager |
| 59 | + /// </summary> |
| 60 | + /// <returns> |
| 61 | + /// The full driver path |
| 62 | + /// </returns> |
| 63 | + public string DriverPath() |
| 64 | + { |
| 65 | + return BinaryPaths()[DriverPathKey]; |
| 66 | + } |
| 67 | + |
| 68 | + public bool HasBrowserPath() |
| 69 | + { |
| 70 | + return !string.IsNullOrWhiteSpace(BrowserPath()); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Invokes Selenium Manager to get the binaries paths and validates if they exist. |
| 75 | + /// </summary> |
| 76 | + /// <returns> |
| 77 | + /// A Dictionary with the validated browser and driver path. |
| 78 | + /// </returns> |
| 79 | + /// <exception cref="NoSuchDriverException">If one of the paths does not exist.</exception> |
| 80 | + private Dictionary<string, string> BinaryPaths() |
39 | 81 | {
|
40 |
| - string executablePath; |
41 |
| - try |
| 82 | + if (paths.ContainsKey(DriverPathKey) && !string.IsNullOrWhiteSpace(paths[DriverPathKey])) |
42 | 83 | {
|
43 |
| - executablePath = SeleniumManager.DriverPath(options); |
| 84 | + return paths; |
44 | 85 | }
|
45 |
| - catch (Exception e) |
| 86 | + Dictionary<string, string> binaryPaths = SeleniumManager.BinaryPaths(CreateArguments()); |
| 87 | + string driverPath = binaryPaths[DriverPathKey]; |
| 88 | + string browserPath = binaryPaths[BrowserPathKey]; |
| 89 | + if (File.Exists(driverPath)) |
46 | 90 | {
|
47 |
| - throw new NoSuchDriverException($"Unable to obtain {options.BrowserName} using Selenium Manager", e); |
| 91 | + paths.Add(DriverPathKey, driverPath); |
48 | 92 | }
|
49 |
| - |
50 |
| - string message; |
51 |
| - if (executablePath == null) |
| 93 | + else |
52 | 94 | {
|
53 |
| - message = $"Unable to locate or obtain {options.BrowserName} driver"; |
| 95 | + throw new NoSuchDriverException($"The driver path is not a valid file: {driverPath}"); |
54 | 96 | }
|
55 |
| - else if (!File.Exists(executablePath)) |
| 97 | + if (File.Exists(browserPath)) |
56 | 98 | {
|
57 |
| - message = $"{options.BrowserName} driver located at {executablePath}, but invalid"; |
| 99 | + paths.Add(BrowserPathKey, browserPath); |
58 | 100 | }
|
59 | 101 | else
|
60 | 102 | {
|
61 |
| - return executablePath; |
| 103 | + throw new NoSuchDriverException($"The browser path is not a valid file: {browserPath}"); |
| 104 | + } |
| 105 | + return paths; |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Create arguments to invoke Selenium Manager |
| 110 | + /// </summary> |
| 111 | + /// <returns> |
| 112 | + /// A string with all arguments to invoke Selenium Manager |
| 113 | + /// </returns> |
| 114 | + /// <exception cref="NoSuchDriverException"></exception> |
| 115 | + private string CreateArguments() |
| 116 | + { |
| 117 | + StringBuilder argsBuilder = new StringBuilder(); |
| 118 | + argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser \"{0}\"", options.BrowserName); |
| 119 | + |
| 120 | + if (!string.IsNullOrEmpty(options.BrowserVersion)) |
| 121 | + { |
| 122 | + argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-version {0}", options.BrowserVersion); |
62 | 123 | }
|
63 | 124 |
|
64 |
| - throw new NoSuchDriverException(message); |
| 125 | + string browserBinary = options.BinaryLocation; |
| 126 | + if (!string.IsNullOrEmpty(browserBinary)) |
| 127 | + { |
| 128 | + argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --browser-path \"{0}\"", browserBinary); |
| 129 | + } |
| 130 | + |
| 131 | + if (options.Proxy != null) |
| 132 | + { |
| 133 | + if (options.Proxy.SslProxy != null) |
| 134 | + { |
| 135 | + argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --proxy \"{0}\"", options.Proxy.SslProxy); |
| 136 | + } |
| 137 | + else if (options.Proxy.HttpProxy != null) |
| 138 | + { |
| 139 | + argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --proxy \"{0}\"", options.Proxy.HttpProxy); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return argsBuilder.ToString(); |
65 | 144 | }
|
| 145 | + |
66 | 146 | }
|
67 | 147 | }
|
0 commit comments