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