Skip to content

Commit 7936f94

Browse files
committed
Improve debugging for the SB() context manager
1 parent 023dbdb commit 7936f94

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import sys
2+
3+
4+
def add_hook(always=False, style="default", debug=False):
5+
import os
6+
7+
if os.environ.get("NO_COLOR", ""):
8+
return # https://no-color.org
9+
isatty = getattr(sys.stderr, "isatty", lambda: False)
10+
if always or isatty():
11+
colorizer = Colorizer(style, debug)
12+
sys.excepthook = colorizer.colorize_traceback
13+
14+
15+
class Colorizer(object):
16+
def __init__(self, style, debug=False):
17+
self.style = style
18+
self.debug = debug
19+
20+
def colorize_traceback(self, type, value, tb):
21+
import traceback
22+
import pygments.lexers
23+
24+
tb_text = "".join(traceback.format_exception(type, value, tb))
25+
lexer_name = "pytb" if sys.version_info < (3, ) else "py3tb"
26+
lexer = pygments.lexers.get_lexer_by_name(lexer_name)
27+
tb_colored = pygments.highlight(tb_text, lexer, self.formatter)
28+
self.stream.write(tb_colored)
29+
30+
@property
31+
def formatter(self):
32+
from pygments.formatters import get_formatter_by_name
33+
import pygments.util
34+
35+
colors = _get_term_color_support()
36+
if self.debug:
37+
sys.stderr.write("Detected support for %s colors\n" % colors)
38+
if colors == 256:
39+
fmt_options = {"style": self.style}
40+
elif self.style in ("light", "dark"):
41+
fmt_options = {"bg": self.style}
42+
else:
43+
fmt_options = {"bg": "dark"}
44+
fmt_alias = "terminal256" if colors == 256 else "terminal"
45+
try:
46+
return get_formatter_by_name(fmt_alias, **fmt_options)
47+
except pygments.util.ClassNotFound as ex:
48+
if self.debug:
49+
sys.stderr.write(str(ex) + "\n")
50+
return get_formatter_by_name(fmt_alias)
51+
52+
@property
53+
def stream(self):
54+
try:
55+
import colorama
56+
except ImportError:
57+
return sys.stderr
58+
return colorama.AnsiToWin32(sys.stderr)
59+
60+
61+
def _get_term_color_support():
62+
try:
63+
import curses
64+
except ImportError:
65+
return 16
66+
curses.setupterm()
67+
return curses.tigetnum("colors")

seleniumbase/plugins/sb_manager.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Usage --> ``with SB() as sb:``
77
Usage example -->
88
from seleniumbase import SB
9-
with SB() as sb: # Lots of args! (Eg. headless=True)
9+
with SB() as sb: # Many args! Eg. SB(browser="edge")
1010
sb.open("https://google.com/ncr")
1111
sb.type('[name="q"]', "SeleniumBase on GitHub\n")
1212
sb.click('a[href*="github.com/seleniumbase"]')
@@ -109,10 +109,12 @@ def SB(
109109
from seleniumbase import BaseCase
110110
from seleniumbase import config as sb_config
111111
from seleniumbase.config import settings
112+
from seleniumbase.core import colored_traceback
112113
from seleniumbase.fixtures import constants
113114
from seleniumbase.fixtures import shared_utils
114115

115116
sb_config_backup = sb_config
117+
sb_config._do_sb_post_mortem = False
116118
sys_argv = sys.argv
117119
archive_logs = False
118120
existing_runner = False
@@ -753,6 +755,7 @@ def SB(
753755
sb.headless_active = False
754756
test_name = None
755757
terminal_width = shared_utils.get_terminal_width()
758+
colored_traceback.add_hook()
756759
if test:
757760
import colorama
758761
import os
@@ -787,6 +790,11 @@ def SB(
787790
sb.setUp()
788791
test_passed = True # This can change later
789792
teardown_exception = None
793+
if "--trace" in sys_argv:
794+
import pdb
795+
796+
pdb.set_trace() # Debug Mode
797+
# Type "s" and press [Enter] to step into "yield sb".
790798
try:
791799
yield sb
792800
except Exception as e:
@@ -803,8 +811,10 @@ def SB(
803811
sb.cm_filename = filename
804812
except Exception:
805813
sb.cm_filename = None
806-
# Tests will raise an exception later if "raise_test_failure"
814+
# Tests will raise an exception if raise_test_failure is True
807815
finally:
816+
if sb._has_failure and "--pdb" in sys_argv:
817+
sb_config._do_sb_post_mortem = True
808818
try:
809819
sb.tearDown()
810820
except Exception as t_e:
@@ -821,7 +831,7 @@ def SB(
821831
sb_config._context_of_runner = True
822832
if test_name:
823833
result = "passed"
824-
if not test_passed:
834+
if test and not test_passed:
825835
result = "failed"
826836
c1 = colorama.Fore.RED
827837
end_text = (
@@ -840,7 +850,7 @@ def SB(
840850
left_space = left_spaces * "="
841851
right_spaces = remaining_spaces - left_spaces
842852
right_space = right_spaces * "="
843-
if not test_passed:
853+
if test and not test_passed:
844854
print(the_traceback)
845855
if not test_name.startswith("runpy.py:"):
846856
print(

0 commit comments

Comments
 (0)