Skip to content

Commit fd84a1b

Browse files
committed
rb - add support for Apple's new safari driver
1 parent 380b2c6 commit fd84a1b

File tree

7 files changed

+133
-9
lines changed

7 files changed

+133
-9
lines changed

rb/.rubocop.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,35 @@ Style/FileName:
3838
Style/MutableConstant:
3939
Exclude:
4040
- 'lib/selenium/webdriver/common/socket_poller.rb'
41-
- 'lib/selenium/webdriver/remote/bridge.rb'
41+
- 'lib/selenium/webdriver/remote/legacy_bridge.rb'
4242
- 'lib/selenium/webdriver/remote/w3c_bridge.rb'
4343

4444
# TODO: Refactor Chrome::Bridge#create_capabilities
4545
Metrics/PerceivedComplexity:
4646
Max: 11
4747
Exclude:
48-
- 'lib/selenium/webdriver/chrome/bridge.rb'
48+
- 'lib/selenium/webdriver/chrome/legacy_bridge.rb'
4949

5050
Metrics/CyclomaticComplexity:
5151
Max: 9
5252
Exclude:
53-
- 'lib/selenium/webdriver/chrome/bridge.rb'
53+
- 'lib/selenium/webdriver/chrome/legacy_bridge.rb'
5454
- 'lib/selenium/webdriver/common/driver.rb'
5555
- 'spec/integration/selenium/webdriver/spec_support/test_environment.rb'
5656

5757
Metrics/ClassLength:
5858
Max: 140
5959
Exclude:
6060
- 'lib/selenium/webdriver/firefox/profile.rb'
61-
- 'lib/selenium/webdriver/remote/bridge.rb'
61+
- 'lib/selenium/webdriver/remote/legacy_bridge.rb'
6262
- 'lib/selenium/webdriver/remote/capabilities.rb'
6363
- 'lib/selenium/webdriver/remote/w3c_bridge.rb'
6464
- 'spec/integration/selenium/webdriver/spec_support/test_environment.rb'
6565

6666
Metrics/AbcSize:
6767
Max: 33
6868
Exclude:
69-
- 'lib/selenium/webdriver/chrome/bridge.rb'
69+
- 'lib/selenium/webdriver/chrome/legacy_bridge.rb'
7070
- 'lib/selenium/webdriver/support/color.rb'
7171

7272
Metrics/LineLength:
@@ -80,4 +80,4 @@ Metrics/ModuleLength:
8080
Metrics/MethodLength:
8181
Max: 33
8282
Exclude:
83-
- 'lib/selenium/webdriver/chrome/bridge.rb'
83+
- 'lib/selenium/webdriver/chrome/legacy_bridge.rb'

rb/CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Firefox:
1515
* Rename wires to geckodriver
1616
* Change default usage from FirefoxDriver to geckodriver
1717

18+
Safari:
19+
* Initial support for Apple's Safari Driver in Sierra (issue #2475)
20+
1821
2.53.0 (2016-03-15)
1922
===================
2023

rb/lib/selenium/webdriver/common/driver.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ def for(browser, opts = {})
6969
when :phantomjs
7070
PhantomJS::Bridge.new(opts)
7171
when :safari
72-
Safari::Bridge.new(opts)
72+
if Safari::LegacyBridge.legacy?
73+
Safari::LegacyBridge.new(opts)
74+
else
75+
Safari::AppleBridge.new(opts)
76+
end
7377
else
7478
raise ArgumentError, "unknown driver: #{browser.inspect}"
7579
end

rb/lib/selenium/webdriver/safari.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ def path
5757
def resource_path
5858
@resource_path ||= Pathname.new(File.expand_path('../safari/resources', __FILE__))
5959
end
60+
61+
def driver_path=(path)
62+
Platform.assert_executable path
63+
@driver_path = path
64+
end
65+
66+
def driver_path
67+
@driver_path || '/usr/bin/safaridriver'
68+
end
6069
end
6170
end # Safari
6271
end # WebDriver
@@ -65,4 +74,6 @@ def resource_path
6574
require 'selenium/webdriver/safari/browser'
6675
require 'selenium/webdriver/safari/server'
6776
require 'selenium/webdriver/safari/options'
68-
require 'selenium/webdriver/safari/bridge'
77+
require 'selenium/webdriver/safari/legacy_bridge'
78+
require 'selenium/webdriver/safari/apple_bridge'
79+
require 'selenium/webdriver/safari/service'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 Safari
23+
# @api private
24+
class AppleBridge < Remote::Bridge
25+
def initialize(opts = {})
26+
opts[:desired_capabilities] ||= Remote::Capabilities.safari
27+
28+
@service = Service.new(Safari.driver_path, Service::DEFAULT_PORT, *extract_service_args(opts))
29+
@service.start
30+
31+
opts[:url] = @service.uri
32+
33+
super
34+
end
35+
36+
def quit
37+
super
38+
ensure
39+
@service.stop if @service
40+
end
41+
42+
private
43+
44+
def extract_service_args(opts)
45+
service_log_path = opts.delete(:service_log_path)
46+
service_log_path ? ["--log-path=#{service_log_path}"] : []
47+
end
48+
end # AppleBridge
49+
end # Safari
50+
end # WebDriver
51+
end # Selenium

rb/lib/selenium/webdriver/safari/bridge.rb renamed to rb/lib/selenium/webdriver/safari/legacy_bridge.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
module Selenium
2121
module WebDriver
2222
module Safari
23-
class Bridge < Remote::Bridge
23+
# @api private
24+
class LegacyBridge < Remote::Bridge
2425
COMMAND_TIMEOUT = 60
2526

27+
def self.legacy?
28+
!File.exist?(Safari.driver_path)
29+
end
30+
2631
def initialize(opts = {})
2732
command_timeout = Integer(opts[:timeout] || COMMAND_TIMEOUT)
2833
safari_options = opts.delete(:options) || Safari::Options.new(opts)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 Safari
23+
#
24+
# @api private
25+
#
26+
27+
class Service < WebDriver::Service
28+
DEFAULT_PORT = 7050
29+
30+
private
31+
32+
def start_process
33+
server_command = [@executable_path, "--port=#{@port}", *@extra_args]
34+
@process = ChildProcess.build(*server_command)
35+
36+
@process.io.inherit! if $DEBUG
37+
@process.start
38+
end
39+
40+
def stop_server
41+
connect_to_server { |http| http.head('/shutdown') }
42+
end
43+
44+
def cannot_connect_error_text
45+
"unable to connect to safaridriver #{@host}:#{@port}"
46+
end
47+
end # Service
48+
end # Safari
49+
end # WebDriver
50+
end # Selenium

0 commit comments

Comments
 (0)