Skip to content

Commit 8c71754

Browse files
committed
Add the new "--variables={}" command-line option
1 parent ae326c6 commit 8c71754

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11453,6 +11453,32 @@ def setUp(self, masterqa_mode=False):
1145311453
self.var1 = sb_config.var1
1145411454
self.var2 = sb_config.var2
1145511455
self.var3 = sb_config.var3
11456+
variables = sb_config.variables
11457+
if variables and type(variables) is str and len(variables) > 0:
11458+
import ast
11459+
11460+
bad_input = False
11461+
if (
11462+
not variables.startswith("{")
11463+
or not variables.endswith("}")
11464+
):
11465+
bad_input = True
11466+
else:
11467+
try:
11468+
variables = ast.literal_eval(variables)
11469+
if not type(variables) is dict:
11470+
bad_input = True
11471+
except Exception:
11472+
bad_input = True
11473+
if bad_input:
11474+
raise Exception(
11475+
'\nExpecting a Python dictionary for "variables"!'
11476+
"\nEg. --variables=\"{'KEY1':'VALUE', 'KEY2':123}\""
11477+
)
11478+
else:
11479+
variables = {}
11480+
sb_config.variables = variables
11481+
self.variables = sb_config.variables
1145611482
self.slow_mode = sb_config.slow_mode
1145711483
self.demo_mode = sb_config.demo_mode
1145811484
self.demo_sleep = sb_config.demo_sleep

seleniumbase/plugins/base_plugin.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
""" This is the Nosetest plugin for setting base configuration and logging. """
33

4+
import ast
45
import sys
56
import time
67
from nose.plugins import Plugin
@@ -20,6 +21,7 @@ class Base(Plugin):
2021
--var1=STRING (Extra test data. Access with "self.var1" in tests.)
2122
--var2=STRING (Extra test data. Access with "self.var2" in tests.)
2223
--var3=STRING (Extra test data. Access with "self.var3" in tests.)
24+
--variables=DICT (Extra test data. Access with "self.variables".)
2325
--settings-file=FILE (Override default SeleniumBase settings.)
2426
--archive-logs (Archive old log files instead of deleting them.)
2527
--archive-downloads (Archive old downloads instead of deleting.)
@@ -86,6 +88,12 @@ def options(self, parser, env):
8688
default=None,
8789
help="Extra data to pass to tests from the command line.",
8890
)
91+
parser.add_option(
92+
"--variables",
93+
dest="variables",
94+
default=None,
95+
help="A var dict to pass to tests from the command line.",
96+
)
8997
parser.add_option(
9098
"--settings_file",
9199
"--settings-file",
@@ -182,13 +190,37 @@ def configure(self, options, conf):
182190
report_helper.clear_out_old_report_logs(archive_past_runs=False)
183191

184192
def beforeTest(self, test):
193+
variables = self.options.variables
194+
if variables and type(variables) is str and len(variables) > 0:
195+
bad_input = False
196+
if (
197+
not variables.startswith("{")
198+
or not variables.endswith("}")
199+
):
200+
bad_input = True
201+
else:
202+
try:
203+
variables = ast.literal_eval(variables)
204+
if not type(variables) is dict:
205+
bad_input = True
206+
except Exception:
207+
bad_input = True
208+
if bad_input:
209+
raise Exception(
210+
'\nExpecting a Python dictionary for "variables"!'
211+
"\nEg. --variables=\"{'KEY1':'VALUE', 'KEY2':123}\""
212+
)
213+
else:
214+
variables = {}
215+
test.test.is_nosetest = True
185216
test.test.environment = self.options.environment
186217
test.test.env = self.options.environment # Add a shortened version
187218
test.test.account = self.options.account
188219
test.test.data = self.options.data
189220
test.test.var1 = self.options.var1
190221
test.test.var2 = self.options.var2
191222
test.test.var3 = self.options.var3
223+
test.test.variables = variables # Already verified is a dictionary
192224
test.test.settings_file = self.options.settings_file
193225
test.test.log_path = self.options.log_path
194226
if self.options.archive_downloads:

seleniumbase/plugins/pytest_plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def pytest_addoption(parser):
3333
--var1=STRING (Extra test data. Access with "self.var1" in tests.)
3434
--var2=STRING (Extra test data. Access with "self.var2" in tests.)
3535
--var3=STRING (Extra test data. Access with "self.var3" in tests.)
36+
--variables=DICT (Extra test data. Access with "self.variables".)
3637
--user-data-dir=DIR (Set the Chrome user data directory to use.)
3738
--protocol=PROTOCOL (The Selenium Grid protocol: http|https.)
3839
--server=SERVER (The Selenium Grid server/IP used for tests.)
@@ -217,6 +218,12 @@ def pytest_addoption(parser):
217218
default=None,
218219
help="Extra data to pass to tests from the command line.",
219220
)
221+
parser.addoption(
222+
"--variables",
223+
dest="variables",
224+
default=None,
225+
help="A var dict to pass to tests from the command line.",
226+
)
220227
parser.addoption(
221228
"--cap_file",
222229
"--cap-file",
@@ -1068,6 +1075,7 @@ def pytest_configure(config):
10681075
sb_config.var1 = config.getoption("var1")
10691076
sb_config.var2 = config.getoption("var2")
10701077
sb_config.var3 = config.getoption("var3")
1078+
sb_config.variables = config.getoption("variables")
10711079
sb_config.environment = config.getoption("environment")
10721080
sb_config.with_selenium = config.getoption("with_selenium")
10731081
sb_config.user_agent = config.getoption("user_agent")

0 commit comments

Comments
 (0)