|
| 1 | +# encoding: utf-8 |
| 2 | +# |
| 3 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The SFC licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +module Selenium |
| 21 | + module WebDriver |
| 22 | + module Edge |
| 23 | + |
| 24 | + # |
| 25 | + # @api private |
| 26 | + # |
| 27 | + class Service |
| 28 | + START_TIMEOUT = 20 |
| 29 | + SOCKET_LOCK_TIMEOUT = 45 |
| 30 | + STOP_TIMEOUT = 5 |
| 31 | + DEFAULT_PORT = 17556 |
| 32 | + MISSING_TEXT = "Unable to find MicrosoftWebDriver. Please download the server from https://www.microsoft.com/en-us/download/details.aspx?id=48212. More info at https://github.com/SeleniumHQ/selenium/wiki/MicrosoftWebDriver." |
| 33 | + |
| 34 | + def self.executable_path |
| 35 | + @executable_path ||= ( |
| 36 | + path = Platform.find_binary "MicrosoftWebDriver" |
| 37 | + path or raise Error::WebDriverError, MISSING_TEXT |
| 38 | + Platform.assert_executable path |
| 39 | + |
| 40 | + path |
| 41 | + ) |
| 42 | + end |
| 43 | + |
| 44 | + def self.executable_path=(path) |
| 45 | + Platform.assert_executable path |
| 46 | + @executable_path = path |
| 47 | + end |
| 48 | + |
| 49 | + def self.default_service(*extra_args) |
| 50 | + new executable_path, DEFAULT_PORT, *extra_args |
| 51 | + end |
| 52 | + |
| 53 | + def initialize(executable_path, port, *extra_args) |
| 54 | + @executable_path = executable_path |
| 55 | + @host = Platform.localhost |
| 56 | + @port = Integer(port) |
| 57 | + |
| 58 | + raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1 |
| 59 | + |
| 60 | + @extra_args = extra_args |
| 61 | + end |
| 62 | + |
| 63 | + def start |
| 64 | + Platform.exit_hook { stop } # make sure we don't leave the server running |
| 65 | + |
| 66 | + socket_lock.locked do |
| 67 | + find_free_port |
| 68 | + start_process |
| 69 | + connect_until_stable |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def stop |
| 74 | + return if @process.nil? || @process.exited? |
| 75 | + |
| 76 | + Net::HTTP.start(@host, @port) do |http| |
| 77 | + http.open_timeout = STOP_TIMEOUT / 2 |
| 78 | + http.read_timeout = STOP_TIMEOUT / 2 |
| 79 | + |
| 80 | + http.head("/shutdown") |
| 81 | + end |
| 82 | + |
| 83 | + @process.poll_for_exit STOP_TIMEOUT |
| 84 | + rescue ChildProcess::TimeoutError |
| 85 | + # ok, force quit |
| 86 | + @process.stop STOP_TIMEOUT |
| 87 | + end |
| 88 | + |
| 89 | + def uri |
| 90 | + URI.parse "http://#{@host}:#{@port}" |
| 91 | + end |
| 92 | + |
| 93 | + def find_free_port |
| 94 | + @port = PortProber.above @port |
| 95 | + end |
| 96 | + |
| 97 | + def start_process |
| 98 | + server_command = [@executable_path, "--port=#{@port}", *@extra_args] |
| 99 | + @process = ChildProcess.build(*server_command) |
| 100 | + |
| 101 | + @process.io.inherit! if $DEBUG == true |
| 102 | + @process.start |
| 103 | + end |
| 104 | + |
| 105 | + def connect_until_stable |
| 106 | + @socket_poller = SocketPoller.new @host, @port, START_TIMEOUT |
| 107 | + |
| 108 | + unless @socket_poller.connected? |
| 109 | + raise Error::WebDriverError, "unable to connect to MicrosoftWebDriver #{@host}:#{@port}" |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + def socket_lock |
| 114 | + @socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT) |
| 115 | + end |
| 116 | + |
| 117 | + end # Service |
| 118 | + end # Edge |
| 119 | + end # WebDriver |
| 120 | +end # Service |
0 commit comments