webdriver.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. try:
  18. import http.client as http_client
  19. except ImportError:
  20. import httplib as http_client
  21. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  22. from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
  23. from .service import Service
  24. class WebDriver(RemoteWebDriver):
  25. """
  26. Controls the SafariDriver and allows you to drive the browser.
  27. """
  28. def __init__(self, port=0, executable_path="/usr/bin/safaridriver",
  29. desired_capabilities=DesiredCapabilities.SAFARI, quiet=False):
  30. """
  31. Creates a new instance of the Safari driver.
  32. Starts the service and then creates new instance of Safari Driver.
  33. :Args:
  34. - port - port you would like the service to run, if left as 0, a free port will be found.
  35. - desired_capabilities: Dictionary object with desired capabilities (Can be used to provide various Safari switches).
  36. - quiet - set to True to suppress stdout and stderr of the driver
  37. """
  38. self.service = Service(executable_path, port=port, quiet=quiet)
  39. self.service.start()
  40. RemoteWebDriver.__init__(
  41. self,
  42. command_executor=self.service.service_url,
  43. desired_capabilities=desired_capabilities)
  44. self._is_remote = False
  45. def quit(self):
  46. """
  47. Closes the browser and shuts down the SafariDriver executable
  48. that is started when starting the SafariDriver
  49. """
  50. try:
  51. RemoteWebDriver.quit(self)
  52. except http_client.BadStatusLine:
  53. pass
  54. finally:
  55. self.service.stop()