|
| 1 | +// <copyright file="FirefoxDriverService.cs" company="WebDriver Committers"> |
| 2 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The SFC licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// </copyright> |
| 18 | + |
| 19 | +using System; |
| 20 | +using System.Collections.Generic; |
| 21 | +using System.Globalization; |
| 22 | +using System.IO; |
| 23 | +using System.Text; |
| 24 | +using OpenQA.Selenium.Internal; |
| 25 | + |
| 26 | +namespace OpenQA.Selenium.Firefox |
| 27 | +{ |
| 28 | + /// <summary> |
| 29 | + /// Exposes the service provided by the native FirefoxDriver executable. |
| 30 | + /// </summary> |
| 31 | + public sealed class FirefoxDriverService : DriverService |
| 32 | + { |
| 33 | + private const string FirefoxDriverServiceFileName = "wires.exe"; |
| 34 | + private static readonly Uri FirefoxDriverDownloadUrl = new Uri("https://github.com/jgraham/wires/releases"); |
| 35 | + private string browserBinaryPath = @"C:\Program Files (x86)\Nightly\firefox.exe"; |
| 36 | + private int browserCommunicationPort = -1; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Initializes a new instance of the FirefoxDriverService class. |
| 40 | + /// </summary> |
| 41 | + /// <param name="executablePath">The full path to the Firefox driver executable.</param> |
| 42 | + /// <param name="executableFileName">The file name of the Firefox driver executable.</param> |
| 43 | + /// <param name="port">The port on which the Firefox driver executable should listen.</param> |
| 44 | + private FirefoxDriverService(string executablePath, string executableFileName, int port) |
| 45 | + : base(executablePath, port, executableFileName, FirefoxDriverDownloadUrl) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets or sets the location of the Firefox binary executable. |
| 51 | + /// </summary> |
| 52 | + public string FirefoxBinaryPath |
| 53 | + { |
| 54 | + get { return this.browserBinaryPath; } |
| 55 | + set { this.browserBinaryPath = value; } |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets or sets the port used by the driver executable to communicate with the browser. |
| 60 | + /// </summary> |
| 61 | + public int BrowserCommunicationPort |
| 62 | + { |
| 63 | + get { return this.browserCommunicationPort; } |
| 64 | + set { this.browserCommunicationPort = value; } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets a value indicating whether to ignore the absence of a status end point. |
| 69 | + /// </summary> |
| 70 | + protected override bool IgnoreMissingStatusEndPoint |
| 71 | + { |
| 72 | + get { return true; } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Gets a value indicating the time to wait for an initial connection before timing out. |
| 77 | + /// </summary> |
| 78 | + protected override TimeSpan InitialConnectionTimeout |
| 79 | + { |
| 80 | + get { return TimeSpan.FromSeconds(2); } |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets the command-line arguments for the driver service. |
| 85 | + /// </summary> |
| 86 | + protected override string CommandLineArguments |
| 87 | + { |
| 88 | + get |
| 89 | + { |
| 90 | + StringBuilder argsBuilder = new StringBuilder(); |
| 91 | + if (this.browserCommunicationPort > 0) |
| 92 | + { |
| 93 | + argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --marionette-port {0}", this.browserCommunicationPort); |
| 94 | + } |
| 95 | + |
| 96 | + if (this.Port > 0) |
| 97 | + { |
| 98 | + argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --webdriver-port {0}", this.Port); |
| 99 | + } |
| 100 | + |
| 101 | + if (!string.IsNullOrEmpty(this.browserBinaryPath)) |
| 102 | + { |
| 103 | + argsBuilder.AppendFormat(CultureInfo.InvariantCulture, " --binary \"{0}\"", this.browserBinaryPath); |
| 104 | + } |
| 105 | + |
| 106 | + return argsBuilder.ToString().Trim(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Creates a default instance of the FirefoxDriverService. |
| 112 | + /// </summary> |
| 113 | + /// <returns>A FirefoxDriverService that implements default settings.</returns> |
| 114 | + public static FirefoxDriverService CreateDefaultService() |
| 115 | + { |
| 116 | + string serviceDirectory = DriverService.FindDriverServiceExecutable(FirefoxDriverServiceFileName, FirefoxDriverDownloadUrl); |
| 117 | + return CreateDefaultService(serviceDirectory); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Creates a default instance of the FirefoxDriverService using a specified path to the Firefox driver executable. |
| 122 | + /// </summary> |
| 123 | + /// <param name="driverPath">The directory containing the Firefox driver executable.</param> |
| 124 | + /// <returns>A FirefoxDriverService using a random port.</returns> |
| 125 | + public static FirefoxDriverService CreateDefaultService(string driverPath) |
| 126 | + { |
| 127 | + return CreateDefaultService(driverPath, FirefoxDriverServiceFileName); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Creates a default instance of the FirefoxDriverService using a specified path to the ChromeDriver executable with the given name. |
| 132 | + /// </summary> |
| 133 | + /// <param name="driverPath">The directory containing the Firefox driver executable.</param> |
| 134 | + /// <param name="driverExecutableFileName">The name of th Firefox driver executable file.</param> |
| 135 | + /// <returns>A FirefoxDriverService using a random port.</returns> |
| 136 | + public static FirefoxDriverService CreateDefaultService(string driverPath, string driverExecutableFileName) |
| 137 | + { |
| 138 | + return new FirefoxDriverService(driverPath, driverExecutableFileName, PortUtilities.FindFreePort()); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments