Skip to content

Commit 362525d

Browse files
committed
Add ability to run headless mode with Chrome and Firefox locally
1 parent 9e68652 commit 362525d

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _create_firefox_profile(downloads_path):
2929
return profile
3030

3131

32-
def get_driver(browser_name):
32+
def get_driver(browser_name, headless=False):
3333
'''
3434
Spins up a new web browser and returns the driver.
3535
Tests that run with pytest spin up the browser from here.
@@ -45,8 +45,12 @@ def get_driver(browser_name):
4545
profile = _create_firefox_profile(downloads_path)
4646
firefox_capabilities = DesiredCapabilities.FIREFOX.copy()
4747
firefox_capabilities['marionette'] = True
48+
options = webdriver.FirefoxOptions()
49+
if headless:
50+
options.add_argument('-headless')
4851
firefox_driver = webdriver.Firefox(
49-
firefox_profile=profile, capabilities=firefox_capabilities)
52+
firefox_profile=profile, capabilities=firefox_capabilities,
53+
firefox_options=options)
5054
except WebDriverException:
5155
# Don't use Geckodriver: Only works for old versions of Firefox
5256
profile = _create_firefox_profile(downloads_path)
@@ -79,6 +83,8 @@ def get_driver(browser_name):
7983
chrome_options.add_argument("--allow-file-access-from-files")
8084
chrome_options.add_argument("--allow-running-insecure-content")
8185
chrome_options.add_argument("--disable-infobars")
86+
if headless:
87+
chrome_options.add_argument("--headless")
8288
if settings.START_CHROME_IN_FULL_SCREEN_MODE:
8389
# Run Chrome in full screen mode on WINDOWS
8490
chrome_options.add_argument("--start-maximized")

seleniumbase/fixtures/base_case.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,8 @@ def setUp(self):
14201420
self.display.start()
14211421
self.headless_active = True
14221422
if self.with_selenium:
1423-
self.driver = browser_launcher.get_driver(self.browser)
1423+
self.driver = browser_launcher.get_driver(self.browser,
1424+
self.headless)
14241425

14251426
def __insert_test_result(self, state, err):
14261427
data_payload = TestcaseDataPayload()

seleniumbase/plugins/selenium_plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,5 @@ def __select_browser(self, browser_name):
201201
self.options.port),
202202
self.browser_settings)
203203
else:
204-
return browser_launcher.get_driver(browser_name)
204+
return browser_launcher.get_driver(browser_name,
205+
self.options.headless)

0 commit comments

Comments
 (0)