[py] Cache platform calls when starting the session(#8953)
import os
-import platform
+from platform import system
from subprocess import Popen, STDOUT
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common import utils
@@ -43,7 +43,7 @@ class FirefoxBinary(object):
# a while the pipe would fill up and Firefox would freeze.
self._log_file = log_file or open(os.devnull, "wb")
self.command_line = None
- if self._start_cmd is None:
+ if not self._start_cmd:
self._start_cmd = self._get_firefox_start_cmd()
if not self._start_cmd.strip():
raise WebDriverException(
@@ -59,6 +59,7 @@ class FirefoxBinary(object):
self._firefox_env["MOZ_CRASHREPORTER_DISABLE"] = "1"
self._firefox_env["MOZ_NO_REMOTE"] = "1"
self._firefox_env["NO_EM_RESTART"] = "1"
+ self.platform = system().lower()
def add_command_line_options(self, *args):
self.command_line = args
@@ -84,10 +85,10 @@ class FirefoxBinary(object):
def _start_from_profile_path(self, path):
self._firefox_env["XRE_PROFILE_PATH"] = path
- if platform.system().lower() == 'linux':
+ if platform == 'linux':
self._modify_link_library_path()
command = [self._start_cmd, "-foreground"]
- if self.command_line is not None:
+ if self.command_line:
for cli in self.command_line:
command.append(cli)
self.process = Popen(
@@ -98,7 +99,7 @@ class FirefoxBinary(object):
"""Blocks until the extension is connectable in the firefox."""
count = 0
while not utils.is_connectable(self.profile.port):
- if self.process.poll() is not None:
+ if self.process.poll():
# Browser has exited
raise WebDriverException(
"The browser appears to have exited "
@@ -148,19 +149,19 @@ class FirefoxBinary(object):
def _get_firefox_start_cmd(self):
"""Return the command to start firefox."""
start_cmd = ""
- if platform.system() == "Darwin":
+ if self.platform == "darwin": #small darwin due to lower() in self.platform
start_cmd = "/Applications/Firefox.app/Contents/MacOS/firefox-bin"
# fallback to homebrew installation for mac users
if not os.path.exists(start_cmd):
start_cmd = os.path.expanduser("~") + start_cmd
- elif platform.system() == "Windows":
+ elif self.platform == "windows": #same
start_cmd = (self._find_exe_in_registry() or self._default_windows_location())
- elif platform.system() == 'Java' and os._name == 'nt':
+ elif self.platform == 'java' and os._name == 'nt':
start_cmd = self._default_windows_location()
else:
for ffname in ["firefox", "iceweasel"]:
start_cmd = self.which(ffname)
- if start_cmd is not None:
+ if start_cmd:
break
else:
# couldn't find firefox on the system path
