|
15 | 15 | # specific language governing permissions and limitations
|
16 | 16 | # under the License.
|
17 | 17 |
|
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 |
23 | 21 |
|
24 |
| - |
25 |
| -class Service(object): |
| 22 | +class Service(service.Service): |
26 | 23 | """
|
27 | 24 | Object that manages the starting and stopping of the SafariDriver
|
28 | 25 | """
|
29 | 26 |
|
30 |
| - def __init__(self, executable_path, port=0, quiet=False): |
| 27 | + def __init__(self, executable_path=None, port=0, quiet=False, use_legacy=False): |
31 | 28 | """
|
32 | 29 | Creates a new instance of the Service
|
33 | 30 |
|
34 | 31 | :Args:
|
35 | 32 | - executable_path : Path to the SafariDriver
|
36 | 33 | - port : Port the service is running on """
|
37 | 34 |
|
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() |
42 | 44 | self.quiet = quiet
|
| 45 | + log = PIPE |
| 46 | + if quiet: |
| 47 | + log = open(os.devnull, 'w') |
| 48 | + service.Service.__init__(self, path, port, log) |
43 | 49 |
|
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] |
70 | 54 |
|
71 | 55 | @property
|
72 | 56 | def service_url(self):
|
73 | 57 | """
|
74 | 58 | Gets the url of the SafariDriver Service
|
75 | 59 | """
|
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 |
85 | 64 |
|
86 |
| - self.process.kill() |
87 |
| - self.process.wait() |
0 commit comments