Remove selenium dependency when not using selenium tests

Closes-Bug: 1377372
Change-Id: I6a493989d7280eaa2a1c999a9d1be4365aa77d52
This commit is contained in:
Gary W. Smith 2014-10-08 09:22:53 -07:00
parent 1df385009a
commit aad4565d9f
1 changed files with 60 additions and 43 deletions

View File

@ -17,62 +17,79 @@
# limitations under the License. # limitations under the License.
# #
import logging
import os
import platform import platform
import shutil import shutil
import subprocess import subprocess
from selenium.common import exceptions as selenium_exceptions LOG = logging.getLogger(__name__)
from selenium.webdriver import firefox
try:
# NOTE: Several distribution can't ship selenium due to its
# non-free license. So they have to patch it out of test-requirements.txt
# Avoid import failure and force not running selenium tests.
# The entire file is encapsulated in the try block because the classes
# inherit from the firefox class contained in selenium.webdriver, and
# python will throw a NameError if the import is skipped.
from selenium.common import exceptions as selenium_exceptions
from selenium.webdriver import firefox
class FirefoxBinary(firefox.firefox_binary.FirefoxBinary): class FirefoxBinary(firefox.firefox_binary.FirefoxBinary):
"""Workarounds selenium firefox issues. """Workarounds selenium firefox issues.
There is race condition in the way firefox is spawned. The exact cause There is race condition in the way firefox is spawned. The exact cause
hasn't been properly diagnosed yet but it's around: hasn't been properly diagnosed yet but it's around:
- getting a free port from the OS with selenium.webdriver.common.utils - getting a free port from the OS with selenium.webdriver.common.utils
free_port(), free_port(),
- release the port immediately but record it in ff prefs so that ff can - release the port immediately but record it in ff prefs so that ff can
listen on that port for the internal http server. listen on that port for the internal http server.
It has been observed that this leads to hanging processes for 'firefox It has been observed that this leads to hanging processes for 'firefox
-silent'. -silent'.
""" """
def _start_from_profile_path(self, path): def _start_from_profile_path(self, path):
self._firefox_env["XRE_PROFILE_PATH"] = path self._firefox_env["XRE_PROFILE_PATH"] = path
if platform.system().lower() == 'linux': if platform.system().lower() == 'linux':
self._modify_link_library_path() self._modify_link_library_path()
command = [self._start_cmd, "-silent"] command = [self._start_cmd, "-silent"]
if self.command_line is not None: if self.command_line is not None:
for cli in self.command_line: for cli in self.command_line:
command.append(cli) command.append(cli)
# The following exists upstream and is known to create hanging firefoxes, # The following exists upstream and is known to create hanging firefoxes,
# leading to zombies. # leading to zombies.
# subprocess.Popen(command, stdout=self._log_file, # subprocess.Popen(command, stdout=self._log_file,
# stderr=subprocess.STDOUT, # stderr=subprocess.STDOUT,
# env=self._firefox_env).communicate() # env=self._firefox_env).communicate()
command[1] = '-foreground' command[1] = '-foreground'
self.process = subprocess.Popen( self.process = subprocess.Popen(
command, stdout=self._log_file, stderr=subprocess.STDOUT, command, stdout=self._log_file, stderr=subprocess.STDOUT,
env=self._firefox_env) env=self._firefox_env)
class WebDriver(firefox.webdriver.WebDriver):
"""Workarounds selenium firefox issues."""
class WebDriver(firefox.webdriver.WebDriver): def __init__(self, firefox_profile=None, firefox_binary=None,
"""Workarounds selenium firefox issues.""" timeout=30, capabilities=None, proxy=None):
try:
super(WebDriver, self).__init__(
firefox_profile, FirefoxBinary(), timeout, capabilities,
proxy)
except selenium_exceptions.WebDriverException:
# If we can't start, cleanup profile
shutil.rmtree(self.profile.path)
if self.profile.tempfolder is not None:
shutil.rmtree(self.profile.tempfolder)
raise
def __init__(self, firefox_profile=None, firefox_binary=None, timeout=30, except ImportError as e:
capabilities=None, proxy=None): # NOTE(saschpe): Several distribution can't ship selenium due to its
try: # non-free license. So they have to patch it out of test-requirements.txt
super(WebDriver, self).__init__( # Avoid import failure and force not running selenium tests.
firefox_profile, FirefoxBinary(), timeout, capabilities, proxy) LOG.warning("{0}, force WITH_SELENIUM=False".format(str(e)))
except selenium_exceptions.WebDriverException: os.environ['WITH_SELENIUM'] = ''
# If we can't start, cleanup profile
shutil.rmtree(self.profile.path)
if self.profile.tempfolder is not None:
shutil.rmtree(self.profile.tempfolder)
raise