diff options
author | Gary W. Smith <gary.w.smith@hp.com> | 2014-10-08 09:22:53 -0700 |
---|---|---|
committer | Akihiro Motoki <motoki@da.jp.nec.com> | 2014-10-25 22:30:57 +0900 |
commit | 697888b94ac628825aa9388c427a5613bab3a78f (patch) | |
tree | b7bc333b369a66489b9800ae637528840fc67225 | |
parent | 9e777f2530f3cecda97f4a0c2b261afe89dc295d (diff) | |
download | horizon-697888b94ac628825aa9388c427a5613bab3a78f.tar.gz |
Remove selenium dependency when not using selenium tests
Closes-Bug: 1377372
Change-Id: I6a493989d7280eaa2a1c999a9d1be4365aa77d52
(cherry picked from commit aad4565d9f64a97f10a9528f2c0efc5039343f2c)
-rw-r--r-- | horizon/test/webdriver.py | 103 |
1 files changed, 60 insertions, 43 deletions
diff --git a/horizon/test/webdriver.py b/horizon/test/webdriver.py index 0974e91fe..8750c9dcf 100644 --- a/horizon/test/webdriver.py +++ b/horizon/test/webdriver.py @@ -17,62 +17,79 @@ # limitations under the License. # +import logging +import os import platform import shutil import subprocess -from selenium.common import exceptions as selenium_exceptions -from selenium.webdriver import firefox +LOG = logging.getLogger(__name__) +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): - """Workarounds selenium firefox issues. + class FirefoxBinary(firefox.firefox_binary.FirefoxBinary): + """Workarounds selenium firefox issues. - There is race condition in the way firefox is spawned. The exact cause - hasn't been properly diagnosed yet but it's around: + There is race condition in the way firefox is spawned. The exact cause + hasn't been properly diagnosed yet but it's around: - - getting a free port from the OS with selenium.webdriver.common.utils - free_port(), + - getting a free port from the OS with selenium.webdriver.common.utils + free_port(), - - release the port immediately but record it in ff prefs so that ff can - listen on that port for the internal http server. + - release the port immediately but record it in ff prefs so that ff can + listen on that port for the internal http server. - It has been observed that this leads to hanging processes for 'firefox - -silent'. - """ + It has been observed that this leads to hanging processes for 'firefox + -silent'. + """ - def _start_from_profile_path(self, path): - self._firefox_env["XRE_PROFILE_PATH"] = path + def _start_from_profile_path(self, path): + self._firefox_env["XRE_PROFILE_PATH"] = path - if platform.system().lower() == 'linux': - self._modify_link_library_path() - command = [self._start_cmd, "-silent"] - if self.command_line is not None: - for cli in self.command_line: - command.append(cli) + if platform.system().lower() == 'linux': + self._modify_link_library_path() + command = [self._start_cmd, "-silent"] + if self.command_line is not None: + for cli in self.command_line: + command.append(cli) -# The following exists upstream and is known to create hanging firefoxes, -# leading to zombies. -# subprocess.Popen(command, stdout=self._log_file, -# stderr=subprocess.STDOUT, -# env=self._firefox_env).communicate() - command[1] = '-foreground' - self.process = subprocess.Popen( - command, stdout=self._log_file, stderr=subprocess.STDOUT, - env=self._firefox_env) + # The following exists upstream and is known to create hanging firefoxes, + # leading to zombies. + # subprocess.Popen(command, stdout=self._log_file, + # stderr=subprocess.STDOUT, + # env=self._firefox_env).communicate() + command[1] = '-foreground' + self.process = subprocess.Popen( + command, stdout=self._log_file, stderr=subprocess.STDOUT, + env=self._firefox_env) + class WebDriver(firefox.webdriver.WebDriver): + """Workarounds selenium firefox issues.""" -class WebDriver(firefox.webdriver.WebDriver): - """Workarounds selenium firefox issues.""" + def __init__(self, firefox_profile=None, firefox_binary=None, + 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, - 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 +except ImportError as e: + # NOTE(saschpe): 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. + LOG.warning("{0}, force WITH_SELENIUM=False".format(str(e))) + os.environ['WITH_SELENIUM'] = '' |