Skip to content

Commit 734444e

Browse files
authored
Merge pull request #348 from seleniumbase/make-headless-mode-the-default-on-linux
Make --headless mode the default setting on Linux
2 parents c5f58c0 + 6d03efb commit 734444e

File tree

7 files changed

+84
-17
lines changed

7 files changed

+84
-17
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[<img src="https://cdn2.hubspot.net/hubfs/100006/images/super_logo_4d.png" title="SeleniumBase" height="48">](https://github.com/seleniumbase/SeleniumBase/blob/master/README.md)
1+
[<img src="https://cdn2.hubspot.net/hubfs/100006/images/super_logo_6.png" title="SeleniumBase" height="48">](https://github.com/seleniumbase/SeleniumBase/blob/master/README.md)
22

33
[<img src="https://img.shields.io/github/release/seleniumbase/SeleniumBase.svg" />](https://github.com/seleniumbase/SeleniumBase/releases) [<img src="https://dev.azure.com/seleniumbase/seleniumbase/_apis/build/status/seleniumbase.SeleniumBase?branchName=master" />](https://dev.azure.com/seleniumbase/seleniumbase/_build/latest?definitionId=1&branchName=master) [<img src="https://travis-ci.org/seleniumbase/SeleniumBase.svg?branch=master" alt="Build Status" />](https://travis-ci.org/seleniumbase/SeleniumBase) [<img src="https://badges.gitter.im/seleniumbase/SeleniumBase.svg" alt="Join the Gitter Chat" />](https://gitter.im/seleniumbase/SeleniumBase) [<img src="https://img.shields.io/badge/license-MIT-22BBCC.svg" alt="MIT License" />](https://github.com/seleniumbase/SeleniumBase/blob/master/LICENSE) [<img src="https://img.shields.io/github/stars/seleniumbase/seleniumbase.svg" alt="GitHub Stars" />](https://github.com/seleniumbase/SeleniumBase/stargazers)<br />
44

@@ -60,7 +60,7 @@ cd examples
6060
pytest my_first_test.py --browser=chrome
6161
```
6262
* Chrome is the default browser if not specified with ``--browser=BROWSER``.
63-
* You MUST add ``--headless`` for your tests to run on a headless machine (No GUI). You can also run in headless mode on any machine.
63+
* On Linux ``--headless`` is the default behavior (running with no GUI). You can also run in headless mode on any OS. If your Linux machine has a GUI and you want to see the web browser as tests run, add ``--headed`` or ``--gui``.
6464

6565
**Check out [my_first_test.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/my_first_test.py) to see what a simple test looks like:**
6666
* <i>By default, [CSS Selectors](https://www.w3schools.com/cssref/css_selectors.asp) are used for finding page elements.</i>
@@ -169,7 +169,7 @@ pytest test_suite.py --browser=chrome
169169
pytest test_suite.py --browser=firefox
170170
```
171171

172-
If you want to run tests headlessly, use ``--headless``, which you'll need to do if your system lacks a GUI interface. Even if your system does have a GUI interface, it may still support headless browser automation.
172+
If you want to run tests headlessly, use ``--headless``, which you'll need to do if your system lacks a GUI interface (``--headless`` is the default setting on Linux). Even if your system does have a GUI interface, it may still support headless browser automation.
173173

174174
To run Pytest multithreaded on multiple CPUs at the same time, add ``-n=NUM`` or ``-n NUM`` on the command line, where NUM is the number of CPUs you want to use.
175175

examples/raw_parameter_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
b = MyTestClass("test_basic")
2929
b.browser = "chrome"
3030
b.headless = False
31+
b.headed = False
3132
b.servername = "localhost"
3233
b.port = 4444
3334
b.data = None

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ self.open(url)
99

1010
self.open_url(url)
1111

12+
self.get(url)
13+
1214
self.visit(url)
1315

1416
self.click(selector, by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT, delay=0)

seleniumbase/fixtures/base_case.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ def open(self, url):
9292
self.__demo_mode_pause_if_active()
9393

9494
def open_url(self, url):
95-
""" Same as open() """
95+
""" Same as open() - Original saved for backwards compatibility. """
96+
self.open(url)
97+
98+
def get(self, url):
99+
""" Same as open() - WebDriver uses this method name. """
96100
self.open(url)
97101

98102
def visit(self, url):
99-
""" Same as open() """
103+
""" Same as open() - Some JS frameworks use this method name. """
100104
self.open(url)
101105

102106
def click(self, selector, by=By.CSS_SELECTOR,
@@ -3170,6 +3174,7 @@ def setUp(self, masterqa_mode=False):
31703174
self.with_selenium = sb_config.with_selenium # Should be True
31713175
self.headless = sb_config.headless
31723176
self.headless_active = False
3177+
self.headed = sb_config.headed
31733178
self.log_path = sb_config.log_path
31743179
self.with_testing_base = sb_config.with_testing_base
31753180
self.with_basic_test_info = sb_config.with_basic_test_info
@@ -3473,7 +3478,12 @@ def tearDown(self):
34733478
self.__quit_all_drivers()
34743479
if self.headless:
34753480
if self.headless_active:
3476-
self.display.stop()
3481+
try:
3482+
self.display.stop()
3483+
except AttributeError:
3484+
pass
3485+
except Exception:
3486+
pass
34773487
self.display = None
34783488
if self.with_db_reporting:
34793489
if has_exception:

seleniumbase/plugins/pytest_plugin.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import optparse
55
import pytest
6+
import sys
67
from seleniumbase import config as sb_config
78
from seleniumbase.core import log_helper
89
from seleniumbase.core import proxy_helper
@@ -159,8 +160,18 @@ def pytest_addoption(parser):
159160
action="store_true",
160161
dest='headless',
161162
default=False,
162-
help="""Using this makes Webdriver run headlessly,
163-
which is required on headless machines.""")
163+
help="""Using this makes Webdriver run web browsers
164+
headlessly, which is required on headless machines.
165+
Default: False on Mac/Windows. True on Linux.""")
166+
parser.addoption('--headed', '--gui',
167+
action="store_true",
168+
dest='headed',
169+
default=False,
170+
help="""Using this makes Webdriver run web browsers with
171+
a GUI when running tests on Linux machines.
172+
(The default setting on Linux is headless.)
173+
(The default setting on Mac or Windows is headed.)
174+
""")
164175
parser.addoption('--is_pytest', '--is-pytest',
165176
action="store_true",
166177
dest='is_pytest',
@@ -257,6 +268,7 @@ def pytest_configure(config):
257268
sb_config.with_selenium = config.getoption('with_selenium')
258269
sb_config.user_agent = config.getoption('user_agent')
259270
sb_config.headless = config.getoption('headless')
271+
sb_config.headed = config.getoption('headed')
260272
sb_config.extension_zip = config.getoption('extension_zip')
261273
sb_config.extension_dir = config.getoption('extension_dir')
262274
sb_config.with_testing_base = config.getoption('with_testing_base')
@@ -285,7 +297,16 @@ def pytest_configure(config):
285297
sb_config.save_screenshot = config.getoption('save_screenshot')
286298
sb_config.visual_baseline = config.getoption('visual_baseline')
287299
sb_config.timeout_multiplier = config.getoption('timeout_multiplier')
288-
sb_config.pytest_html_report = config.getoption("htmlpath") # --html=FILE
300+
sb_config.pytest_html_report = config.getoption('htmlpath') # --html=FILE
301+
302+
if "linux" in sys.platform and (
303+
not sb_config.headed and not sb_config.headless):
304+
print(
305+
"(Running with --headless on Linux. "
306+
"Use --headed or --gui to override.)")
307+
sb_config.headless = True
308+
if not sb_config.headless:
309+
sb_config.headed = True
289310

290311
if sb_config.with_testing_base:
291312
log_helper.log_folder_setup(sb_config.log_path, sb_config.archive_logs)

seleniumbase/plugins/selenium_plugin.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
by providing a WebDriver object for the tests to use.
44
"""
55

6+
import sys
67
from nose.plugins import Plugin
7-
from pyvirtualdisplay import Display
88
from seleniumbase.core import proxy_helper
99
from seleniumbase.fixtures import constants
1010

@@ -25,6 +25,7 @@ class SeleniumBrowser(Plugin):
2525
self.options.extension_zip -- load a Chrome Extension ZIP (--extension_zip)
2626
self.options.extension_dir -- load a Chrome Extension DIR (--extension_dir)
2727
self.options.headless -- the option to run headlessly (--headless)
28+
self.options.headed -- the option to run with a GUI on Linux (--headed)
2829
self.options.demo_mode -- the option to slow down Selenium (--demo_mode)
2930
self.options.demo_sleep -- Selenium action delay in DemoMode (--demo_sleep)
3031
self.options.highlights -- # of highlight animations shown (--highlights)
@@ -128,8 +129,18 @@ def options(self, parser, env):
128129
action="store_true",
129130
dest='headless',
130131
default=False,
131-
help="""Using this makes Webdriver run headlessly,
132-
which is required on headless machines.""")
132+
help="""Using this makes Webdriver run web browsers headlessly,
133+
which is required on headless machines.
134+
Default: False on Mac/Windows. True on Linux.""")
135+
parser.add_option(
136+
'--headed', '--gui',
137+
action="store_true",
138+
dest='headed',
139+
default=False,
140+
help="""Using this makes Webdriver run web browsers with
141+
a GUI when running tests on Linux machines.
142+
(The default setting on Linux is headless.)
143+
(The default setting on Mac or Windows is headed.)""")
133144
parser.add_option(
134145
'--demo_mode', '--demo-mode', '--demo',
135146
action="store_true",
@@ -233,6 +244,7 @@ def beforeTest(self, test):
233244
test.test.browser = self.options.browser
234245
test.test.cap_file = self.options.cap_file
235246
test.test.headless = self.options.headless
247+
test.test.headed = self.options.headed
236248
test.test.servername = self.options.servername
237249
test.test.port = self.options.port
238250
test.test.user_data_dir = self.options.user_data_dir
@@ -256,10 +268,26 @@ def beforeTest(self, test):
256268
if test.test.servername != "localhost":
257269
# Use Selenium Grid (Use --server=127.0.0.1 for localhost Grid)
258270
test.test.use_grid = True
271+
if "linux" in sys.platform and (
272+
not self.options.headed and not self.options.headless):
273+
print(
274+
"(Running with --headless on Linux. "
275+
"Use --headed or --gui to override.)")
276+
self.options.headless = True
277+
test.test.headless = True
278+
if not self.options.headless:
279+
self.options.headed = True
280+
test.test.headed = True
259281
if self.options.headless:
260-
self.display = Display(visible=0, size=(1920, 1200))
261-
self.display.start()
262-
self.headless_active = True
282+
try:
283+
from pyvirtualdisplay import Display
284+
self.display = Display(visible=0, size=(1440, 1080))
285+
self.display.start()
286+
self.headless_active = True
287+
except Exception:
288+
# pyvirtualdisplay might not be necessary anymore because
289+
# Chrome and Firefox now have built-in headless displays
290+
pass
263291
# The driver will be received later
264292
self.driver = None
265293
test.test.driver = self.driver
@@ -278,4 +306,9 @@ def afterTest(self, test):
278306
pass
279307
if self.options.headless:
280308
if self.headless_active:
281-
self.display.stop()
309+
try:
310+
self.display.stop()
311+
except AttributeError:
312+
pass
313+
except Exception:
314+
pass

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
setup(
1919
name='seleniumbase',
20-
version='1.25.6',
20+
version='1.26.0',
2121
description='Reliable Browser Automation & Testing Framework',
2222
long_description=long_description,
2323
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)