extension_connection.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 logging
  18. import time
  19. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  20. from selenium.webdriver.common import utils
  21. from selenium.webdriver.remote.command import Command
  22. from selenium.webdriver.remote.remote_connection import RemoteConnection
  23. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
  24. LOGGER = logging.getLogger(__name__)
  25. PORT = 0
  26. HOST = None
  27. _URL = ""
  28. class ExtensionConnection(RemoteConnection):
  29. def __init__(self, host, firefox_profile, firefox_binary=None, timeout=30):
  30. self.profile = firefox_profile
  31. self.binary = firefox_binary
  32. HOST = host
  33. timeout = int(timeout)
  34. if self.binary is None:
  35. self.binary = FirefoxBinary()
  36. if HOST is None:
  37. HOST = "127.0.0.1"
  38. PORT = utils.free_port()
  39. self.profile.port = PORT
  40. self.profile.update_preferences()
  41. self.profile.add_extension()
  42. self.binary.launch_browser(self.profile, timeout=timeout)
  43. _URL = "http://%s:%d/hub" % (HOST, PORT)
  44. RemoteConnection.__init__(
  45. self, _URL, keep_alive=True)
  46. def quit(self, sessionId=None):
  47. self.execute(Command.QUIT, {'sessionId': sessionId})
  48. while self.is_connectable():
  49. LOGGER.info("waiting to quit")
  50. time.sleep(1)
  51. def connect(self):
  52. """Connects to the extension and retrieves the session id."""
  53. return self.execute(Command.NEW_SESSION,
  54. {'desiredCapabilities': DesiredCapabilities.FIREFOX})
  55. @classmethod
  56. def connect_and_quit(self):
  57. """Connects to an running browser and quit immediately."""
  58. self._request('%s/extensions/firefox/quit' % _URL)
  59. @classmethod
  60. def is_connectable(self):
  61. """Trys to connect to the extension but do not retrieve context."""
  62. utils.is_connectable(self.profile.port)
  63. class ExtensionConnectionError(Exception):
  64. """An internal error occurred int the extension.
  65. Might be caused by bad input or bugs in webdriver
  66. """
  67. pass