Skip to content

Commit 95c336e

Browse files
committed
Add "--rcs" option to reuse session for tests in same class
1 parent 1173c16 commit 95c336e

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

seleniumbase/core/session_helper.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
from seleniumbase import config as sb_config
3+
4+
is_windows = False
5+
if sys.platform in ["win32", "win64", "x64"]:
6+
is_windows = True
7+
8+
9+
def end_reused_class_session_as_needed():
10+
if (
11+
hasattr(sb_config, "reuse_class_session")
12+
and sb_config.reuse_class_session
13+
and hasattr(sb_config, "shared_driver")
14+
and sb_config.shared_driver
15+
):
16+
if (
17+
not is_windows
18+
or (
19+
hasattr(sb_config.shared_driver, "service")
20+
and sb_config.shared_driver.service.process
21+
)
22+
):
23+
try:
24+
sb_config.shared_driver.quit()
25+
except Exception:
26+
sb_config.shared_driver = None

seleniumbase/fixtures/base_case.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def test_anything(self):
6464
from seleniumbase.config import settings
6565
from seleniumbase.core import download_helper
6666
from seleniumbase.core import log_helper
67+
from seleniumbase.core import session_helper
6768
from seleniumbase.fixtures import constants
6869
from seleniumbase.fixtures import css_to_xpath
6970
from seleniumbase.fixtures import js_utils
@@ -14610,6 +14611,37 @@ def tearDown(self):
1461014611
# User forgot to call "self.process_deferred_asserts()" in test
1461114612
raise deferred_exception
1461214613

14614+
def __end_reused_class_session_as_needed(self):
14615+
if (
14616+
hasattr(sb_config, "reuse_class_session")
14617+
and sb_config.reuse_class_session
14618+
and hasattr(sb_config, "shared_driver")
14619+
and sb_config.shared_driver
14620+
):
14621+
if (
14622+
not is_windows
14623+
or (
14624+
hasattr(sb_config.shared_driver, "service")
14625+
and sb_config.shared_driver.service.process
14626+
)
14627+
):
14628+
try:
14629+
sb_config.shared_driver.quit()
14630+
except Exception:
14631+
sb_config.shared_driver = None
14632+
14633+
@classmethod
14634+
def setUpClass(self):
14635+
# Only used when: "--rcs" / "--reuse-class-session"
14636+
# Close existing sessions before the class starts.
14637+
session_helper.end_reused_class_session_as_needed()
14638+
14639+
@classmethod
14640+
def tearDownClass(self):
14641+
# Only used when: "--rcs" / "--reuse-class-session"
14642+
# Close existing sessions after the class finishes.
14643+
session_helper.end_reused_class_session_as_needed()
14644+
1461314645

1461414646
r"""----------------------------------------------------------------->
1461514647
| ______ __ _ ____ |

seleniumbase/plugins/pytest_plugin.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ def pytest_addoption(parser):
102102
--incognito (Enable Chrome's Incognito mode.)
103103
--guest (Enable Chrome's Guest mode.)
104104
--devtools (Open Chrome's DevTools when the browser opens.)
105-
--reuse-session | --rs (Reuse browser session between tests.)
105+
--reuse-session | --rs (Reuse browser session for all tests.)
106+
--reuse-class-session | --rcs (Reuse session for tests in class.)
106107
--crumbs (Delete all cookies between tests reusing a session.)
107108
--disable-beforeunload (Disable the "beforeunload" event on Chrome.)
108109
--window-size=WIDTH,HEIGHT (Set the browser's starting window size.)
@@ -1039,7 +1040,17 @@ def pytest_addoption(parser):
10391040
dest="reuse_session",
10401041
default=False,
10411042
help="""The option to reuse the selenium browser window
1042-
session between tests.""",
1043+
session for all tests.""",
1044+
)
1045+
parser.addoption(
1046+
"--rcs",
1047+
"--reuse_class_session",
1048+
"--reuse-class-session",
1049+
action="store_true",
1050+
dest="reuse_class_session",
1051+
default=False,
1052+
help="""The option to reuse the selenium browser window
1053+
session for all tests within the same class.""",
10431054
)
10441055
parser.addoption(
10451056
"--crumbs",
@@ -1048,7 +1059,9 @@ def pytest_addoption(parser):
10481059
default=False,
10491060
help="""The option to delete all cookies between tests
10501061
that reuse the same browser session. This option
1051-
is only needed when using "--reuse-session".""",
1062+
is only useful if using "--reuse-session"/"--rs"
1063+
or "--reuse-class-session"/"--rcs" because tests
1064+
use a new clean browser if not reusing sessions.""",
10521065
)
10531066
parser.addoption(
10541067
"--disable-beforeunload",
@@ -1444,6 +1457,9 @@ def pytest_configure(config):
14441457
sb_config.guest_mode = config.getoption("guest_mode")
14451458
sb_config.devtools = config.getoption("devtools")
14461459
sb_config.reuse_session = config.getoption("reuse_session")
1460+
sb_config.reuse_class_session = config.getoption("reuse_class_session")
1461+
if sb_config.reuse_class_session:
1462+
sb_config.reuse_session = True
14471463
sb_config.shared_driver = None # The default driver for session reuse
14481464
sb_config.crumbs = config.getoption("crumbs")
14491465
sb_config._disable_beforeunload = config.getoption("_disable_beforeunload")

0 commit comments

Comments
 (0)