service.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Licensed to the Software Freedom Conservancy (SFC) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The SFC licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. import os
  18. from selenium.webdriver.common import service, utils
  19. from subprocess import PIPE
  20. class Service(service.Service):
  21. """
  22. Object that manages the starting and stopping of the SafariDriver
  23. """
  24. def __init__(self, executable_path, port=0, quiet=False, service_args=None):
  25. """
  26. Creates a new instance of the Service
  27. :Args:
  28. - executable_path : Path to the SafariDriver
  29. - port : Port the service is running on
  30. - quiet : Suppress driver stdout and stderr
  31. - service_args : List of args to pass to the safaridriver service """
  32. if not os.path.exists(executable_path):
  33. if "Safari Technology Preview" in executable_path:
  34. message = "Safari Technology Preview does not seem to be installed. You can download it at https://developer.apple.com/safari/download/."
  35. else:
  36. message = "SafariDriver was not found; are you running Safari 10 or later? You can download Safari at https://developer.apple.com/safari/download/."
  37. raise Exception(message)
  38. if port == 0:
  39. port = utils.free_port()
  40. self.service_args = service_args or []
  41. self.quiet = quiet
  42. log = PIPE
  43. if quiet:
  44. log = open(os.devnull, 'w')
  45. service.Service.__init__(self, executable_path, port, log)
  46. def command_line_args(self):
  47. return ["-p", "%s" % self.port] + self.service_args
  48. @property
  49. def service_url(self):
  50. """
  51. Gets the url of the SafariDriver Service
  52. """
  53. return "http://localhost:%d" % self.port