1# Copyright 2008-2011 WebDriver committers 2# Copyright 2008-2011 Google Inc. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import logging 17import time 18 19from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 20from selenium.webdriver.common import utils 21from selenium.webdriver.remote.command import Command 22from selenium.webdriver.remote.remote_connection import RemoteConnection 23from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 24 25 26LOGGER = logging.getLogger(__name__) 27PORT = 0 # 28HOST = None 29_URL = "" 30class ExtensionConnection(RemoteConnection): 31 def __init__(self, host, firefox_profile, firefox_binary=None, timeout=30): 32 self.profile = firefox_profile 33 self.binary = firefox_binary 34 HOST = host 35 if self.binary is None: 36 self.binary = FirefoxBinary() 37 38 if HOST is None: 39 HOST = "127.0.0.1" 40 41 PORT = utils.free_port() 42 self.profile.port = PORT 43 self.profile.update_preferences() 44 45 self.profile.add_extension() 46 47 self.binary.launch_browser(self.profile) 48 _URL = "http://%s:%d/hub" % (HOST, PORT) 49 RemoteConnection.__init__( 50 self, _URL) 51 52 def quit(self, sessionId=None): 53 self.execute(Command.QUIT, {'sessionId':sessionId}) 54 while self.is_connectable(): 55 LOGGER.info("waiting to quit") 56 time.sleep(1) 57 58 def connect(self): 59 """Connects to the extension and retrieves the session id.""" 60 return self.execute(Command.NEW_SESSION, {'desiredCapabilities': DesiredCapabilities.FIREFOX}) 61 62 @classmethod 63 def connect_and_quit(self): 64 """Connects to an running browser and quit immediately.""" 65 self._request('%s/extensions/firefox/quit' % _URL) 66 67 @classmethod 68 def is_connectable(self): 69 """Trys to connect to the extension but do not retrieve context.""" 70 utils.is_connectable(self.port) 71 72class ExtensionConnectionError(Exception): 73 """An internal error occurred int the extension. 74 75 Might be caused by bad input or bugs in webdriver 76 """ 77 pass 78 79 80