service.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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):
  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. if not os.path.exists(executable_path):
  31. raise Exception("SafariDriver requires Safari 10 on OSX El Capitan or greater")
  32. if port == 0:
  33. port = utils.free_port()
  34. self.quiet = quiet
  35. log = PIPE
  36. if quiet:
  37. log = open(os.devnull, 'w')
  38. service.Service.__init__(self, executable_path, port, log)
  39. def command_line_args(self):
  40. return ["-p", "%s" % self.port]
  41. @property
  42. def service_url(self):
  43. """
  44. Gets the url of the SafariDriver Service
  45. """
  46. return "http://localhost:%d" % self.port