firefox_binary.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 os
  18. import platform
  19. from subprocess import Popen, STDOUT
  20. from selenium.common.exceptions import WebDriverException
  21. from selenium.webdriver.common import utils
  22. import time
  23. class FirefoxBinary(object):
  24. NO_FOCUS_LIBRARY_NAME = "x_ignore_nofocus.so"
  25. def __init__(self, firefox_path=None, log_file=None):
  26. """
  27. Creates a new instance of Firefox binary.
  28. :Args:
  29. - firefox_path - Path to the Firefox executable. By default, it will be detected from the standard locations.
  30. - log_file - A file object to redirect the firefox process output to. It can be sys.stdout.
  31. Please note that with parallel run the output won't be synchronous.
  32. By default, it will be redirected to /dev/null.
  33. """
  34. self._start_cmd = firefox_path
  35. # We used to default to subprocess.PIPE instead of /dev/null, but after
  36. # a while the pipe would fill up and Firefox would freeze.
  37. self._log_file = log_file or open(os.devnull, "wb")
  38. self.command_line = None
  39. if self._start_cmd is None:
  40. self._start_cmd = self._get_firefox_start_cmd()
  41. if not self._start_cmd.strip():
  42. raise WebDriverException(
  43. "Failed to find firefox binary. You can set it by specifying "
  44. "the path to 'firefox_binary':\n\nfrom "
  45. "selenium.webdriver.firefox.firefox_binary import "
  46. "FirefoxBinary\n\nbinary = "
  47. "FirefoxBinary('/path/to/binary')\ndriver = "
  48. "webdriver.Firefox(firefox_binary=binary)")
  49. # Rather than modifying the environment of the calling Python process
  50. # copy it and modify as needed.
  51. self._firefox_env = os.environ.copy()
  52. self._firefox_env["MOZ_CRASHREPORTER_DISABLE"] = "1"
  53. self._firefox_env["MOZ_NO_REMOTE"] = "1"
  54. self._firefox_env["NO_EM_RESTART"] = "1"
  55. def add_command_line_options(self, *args):
  56. self.command_line = args
  57. def launch_browser(self, profile, timeout=30):
  58. """Launches the browser for the given profile name.
  59. It is assumed the profile already exists.
  60. """
  61. self.profile = profile
  62. self._start_from_profile_path(self.profile.path)
  63. self._wait_until_connectable(timeout=timeout)
  64. def kill(self):
  65. """Kill the browser.
  66. This is useful when the browser is stuck.
  67. """
  68. if self.process:
  69. self.process.kill()
  70. self.process.wait()
  71. def _start_from_profile_path(self, path):
  72. self._firefox_env["XRE_PROFILE_PATH"] = path
  73. if platform.system().lower() == 'linux':
  74. self._modify_link_library_path()
  75. command = [self._start_cmd, "-foreground"]
  76. if self.command_line is not None:
  77. for cli in self.command_line:
  78. command.append(cli)
  79. self.process = Popen(
  80. command, stdout=self._log_file, stderr=STDOUT,
  81. env=self._firefox_env)
  82. def _wait_until_connectable(self, timeout=30):
  83. """Blocks until the extension is connectable in the firefox."""
  84. count = 0
  85. while not utils.is_connectable(self.profile.port):
  86. if self.process.poll() is not None:
  87. # Browser has exited
  88. raise WebDriverException(
  89. "The browser appears to have exited "
  90. "before we could connect. If you specified a log_file in "
  91. "the FirefoxBinary constructor, check it for details.")
  92. if count >= timeout:
  93. self.kill()
  94. raise WebDriverException(
  95. "Can't load the profile. Possible firefox version mismatch. "
  96. "You must use GeckoDriver instead for Firefox 48+. Profile "
  97. "Dir: %s If you specified a log_file in the "
  98. "FirefoxBinary constructor, check it for details."
  99. % (self.profile.path))
  100. count += 1
  101. time.sleep(1)
  102. return True
  103. def _find_exe_in_registry(self):
  104. try:
  105. from _winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
  106. except ImportError:
  107. from winreg import OpenKey, QueryValue, HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER
  108. import shlex
  109. keys = (r"SOFTWARE\Classes\FirefoxHTML\shell\open\command",
  110. r"SOFTWARE\Classes\Applications\firefox.exe\shell\open\command")
  111. command = ""
  112. for path in keys:
  113. try:
  114. key = OpenKey(HKEY_LOCAL_MACHINE, path)
  115. command = QueryValue(key, "")
  116. break
  117. except OSError:
  118. try:
  119. key = OpenKey(HKEY_CURRENT_USER, path)
  120. command = QueryValue(key, "")
  121. break
  122. except OSError:
  123. pass
  124. else:
  125. return ""
  126. if not command:
  127. return ""
  128. return shlex.split(command)[0]
  129. def _get_firefox_start_cmd(self):
  130. """Return the command to start firefox."""
  131. start_cmd = ""
  132. if platform.system() == "Darwin":
  133. start_cmd = "/Applications/Firefox.app/Contents/MacOS/firefox-bin"
  134. # fallback to homebrew installation for mac users
  135. if not os.path.exists(start_cmd):
  136. start_cmd = os.path.expanduser("~") + start_cmd
  137. elif platform.system() == "Windows":
  138. start_cmd = (self._find_exe_in_registry() or self._default_windows_location())
  139. elif platform.system() == 'Java' and os._name == 'nt':
  140. start_cmd = self._default_windows_location()
  141. else:
  142. for ffname in ["firefox", "iceweasel"]:
  143. start_cmd = self.which(ffname)
  144. if start_cmd is not None:
  145. break
  146. else:
  147. # couldn't find firefox on the system path
  148. raise RuntimeError(
  149. "Could not find firefox in your system PATH." +
  150. " Please specify the firefox binary location or install firefox")
  151. return start_cmd
  152. def _default_windows_location(self):
  153. program_files = [os.getenv("PROGRAMFILES", r"C:\Program Files"),
  154. os.getenv("PROGRAMFILES(X86)", r"C:\Program Files (x86)")]
  155. for path in program_files:
  156. binary_path = os.path.join(path, r"Mozilla Firefox\firefox.exe")
  157. if os.access(binary_path, os.X_OK):
  158. return binary_path
  159. return ""
  160. def _modify_link_library_path(self):
  161. existing_ld_lib_path = os.environ.get('LD_LIBRARY_PATH', '')
  162. new_ld_lib_path = self._extract_and_check(
  163. self.profile, self.NO_FOCUS_LIBRARY_NAME, "x86", "amd64")
  164. new_ld_lib_path += existing_ld_lib_path
  165. self._firefox_env["LD_LIBRARY_PATH"] = new_ld_lib_path
  166. self._firefox_env['LD_PRELOAD'] = self.NO_FOCUS_LIBRARY_NAME
  167. def _extract_and_check(self, profile, no_focus_so_name, x86, amd64):
  168. paths = [x86, amd64]
  169. built_path = ""
  170. for path in paths:
  171. library_path = os.path.join(profile.path, path)
  172. if not os.path.exists(library_path):
  173. os.makedirs(library_path)
  174. import shutil
  175. shutil.copy(os.path.join(
  176. os.path.dirname(__file__),
  177. path,
  178. self.NO_FOCUS_LIBRARY_NAME),
  179. library_path)
  180. built_path += library_path + ":"
  181. return built_path
  182. def which(self, fname):
  183. """Returns the fully qualified path by searching Path of the given
  184. name"""
  185. for pe in os.environ['PATH'].split(os.pathsep):
  186. checkname = os.path.join(pe, fname)
  187. if os.access(checkname, os.X_OK) and not os.path.isdir(checkname):
  188. return checkname
  189. return None