Skip to content

Commit c726b3f

Browse files
committed
py: default Safari to use safaridriver from apple if it exists, add 'use_legacy' to Safari constructor to force the old way (not sure if it would even work, but leaving the option open)
1 parent 5d3639f commit c726b3f

File tree

2 files changed

+30
-57
lines changed

2 files changed

+30
-57
lines changed

py/selenium/webdriver/safari/service.py

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,50 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from os import devnull
19-
import subprocess
20-
import time
21-
from selenium.common.exceptions import WebDriverException
22-
from selenium.webdriver.common import utils
18+
import os
19+
from selenium.webdriver.common import service, utils
20+
from subprocess import PIPE
2321

24-
25-
class Service(object):
22+
class Service(service.Service):
2623
"""
2724
Object that manages the starting and stopping of the SafariDriver
2825
"""
2926

30-
def __init__(self, executable_path, port=0, quiet=False):
27+
def __init__(self, executable_path=None, port=0, quiet=False, use_legacy=False):
3128
"""
3229
Creates a new instance of the Service
3330
3431
:Args:
3532
- executable_path : Path to the SafariDriver
3633
- port : Port the service is running on """
3734

38-
self.port = port
39-
self.path = executable_path
40-
if self.port == 0:
41-
self.port = utils.free_port()
35+
if not use_legacy and os.path.exists('/usr/bin/safaridriver'):
36+
path = '/usr/bin/safaridriver'
37+
self.legacy_driver = False
38+
else:
39+
path = 'java'
40+
self.standalone_jar = executable_path
41+
self.legacy_driver = True
42+
if port == 0:
43+
port = utils.free_port()
4244
self.quiet = quiet
45+
log = PIPE
46+
if quiet:
47+
log = open(os.devnull, 'w')
48+
service.Service.__init__(self, path, port, log)
4349

44-
def start(self):
45-
"""
46-
Starts the SafariDriver Service.
47-
48-
:Exceptions:
49-
- WebDriverException : Raised either when it can't start the service
50-
or when it can't connect to the service
51-
"""
52-
kwargs = dict()
53-
if self.quiet:
54-
devnull_out = open(devnull, 'w')
55-
kwargs.update(stdout=devnull_out,
56-
stderr=devnull_out)
57-
try:
58-
self.process = subprocess.Popen(["java", "-jar", self.path, "-port", "%s" % self.port],
59-
**kwargs)
60-
except:
61-
raise WebDriverException(
62-
"SafariDriver executable needs to be available in the path.")
63-
time.sleep(10)
64-
count = 0
65-
while not utils.is_connectable(self.port):
66-
count += 1
67-
time.sleep(1)
68-
if count == 30:
69-
raise WebDriverException("Can not connect to the SafariDriver")
50+
def command_line_args(self):
51+
if self.legacy_driver:
52+
return ["-jar", self.standalone_jar, "-port", "%s" % self.port]
53+
return ["-p", "%s" % self.port]
7054

7155
@property
7256
def service_url(self):
7357
"""
7458
Gets the url of the SafariDriver Service
7559
"""
76-
return "http://localhost:%d/wd/hub" % self.port
77-
78-
def stop(self):
79-
"""
80-
Tells the SafariDriver to stop and cleans up the process
81-
"""
82-
# If it's dead don't worry
83-
if self.process is None:
84-
return
60+
if not self.legacy_driver:
61+
return "http://localhost:%d" % self.port
62+
else:
63+
return "http://localhost:%d/wd/hub" % self.port
8564

86-
self.process.kill()
87-
self.process.wait()

py/selenium/webdriver/safari/webdriver.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class WebDriver(RemoteWebDriver):
3333
"""
3434

3535
def __init__(self, executable_path=None, port=0,
36-
desired_capabilities=DesiredCapabilities.SAFARI, quiet=False):
36+
desired_capabilities=DesiredCapabilities.SAFARI, quiet=False, use_legacy_driver=False):
3737
"""
3838
Creates a new instance of the Safari driver.
3939
@@ -45,13 +45,9 @@ def __init__(self, executable_path=None, port=0,
4545
- port - port you would like the service to run, if left as 0, a free port will be found.
4646
- desired_capabilities: Dictionary object with desired capabilities (Can be used to provide various Safari switches).
4747
"""
48-
if executable_path is None:
49-
try:
50-
executable_path = os.environ["SELENIUM_SERVER_JAR"]
51-
except:
52-
raise Exception("No executable path given, please add one to Environment Variable \
53-
'SELENIUM_SERVER_JAR'")
54-
self.service = Service(executable_path, port=port, quiet=quiet)
48+
if not executable_path is None:
49+
executable_path = os.environ.get("SELENIUM_SERVER_JAR")
50+
self.service = Service(executable_path, port=port, quiet=quiet, use_legacy=use_legacy_driver)
5551
self.service.start()
5652

5753
RemoteWebDriver.__init__(

0 commit comments

Comments
 (0)