Skip to content

Commit 4872d8c

Browse files
committed
Fix issue with getting console width
1 parent c406380 commit 4872d8c

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

seleniumbase/console_scripts/sb_print.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def main():
130130
used_width = None # code_width and few spaces on right for padding
131131
magic_syntax = None # the syntax generated by rich.syntax.Syntax()
132132
try:
133-
console_width = os.get_terminal_size()[0]
133+
console_width = os.get_terminal_size().columns
134134
if console_width:
135135
console_width = int(console_width)
136136
except Exception:

seleniumbase/fixtures/shared_utils.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
"""
22
This module contains shared utility methods.
33
"""
4-
import fasteners
54
import subprocess
65
import sys
7-
import time
8-
from selenium.common.exceptions import ElementNotVisibleException
9-
from selenium.common.exceptions import NoAlertPresentException
10-
from selenium.common.exceptions import NoSuchAttributeException
11-
from selenium.common.exceptions import NoSuchElementException
12-
from selenium.common.exceptions import NoSuchFrameException
13-
from selenium.common.exceptions import NoSuchWindowException
14-
from seleniumbase.common.exceptions import TextNotVisibleException
156
from seleniumbase.fixtures import constants
167
from seleniumbase import config as sb_config
178

189

1910
def pip_install(package, version=None):
11+
import fasteners
12+
2013
pip_install_lock = fasteners.InterProcessLock(
2114
constants.PipInstall.LOCKFILE
2215
)
@@ -32,11 +25,47 @@ def pip_install(package, version=None):
3225
)
3326

3427

28+
def is_windows():
29+
platform = sys.platform
30+
if "win32" in platform or "win64" in platform or "x64" in platform:
31+
return True
32+
else:
33+
return False
34+
35+
36+
def get_terminal_width():
37+
import os
38+
39+
width = 80 # default
40+
try:
41+
width = os.get_terminal_size().columns
42+
except Exception:
43+
try:
44+
if is_windows():
45+
raise Exception("Don't even try 'tput cols' on Windows!")
46+
width = int(subprocess.check_output(["tput", "cols"]))
47+
except Exception:
48+
try:
49+
import shutil
50+
51+
width = shutil.get_terminal_size((80, 20)).columns
52+
except Exception:
53+
pass
54+
return width
55+
56+
3557
def format_exc(exception, message):
3658
"""
3759
Formats an exception message to make the output cleaner.
3860
"""
61+
from selenium.common.exceptions import ElementNotVisibleException
62+
from selenium.common.exceptions import NoAlertPresentException
63+
from selenium.common.exceptions import NoSuchAttributeException
64+
from selenium.common.exceptions import NoSuchElementException
65+
from selenium.common.exceptions import NoSuchFrameException
66+
from selenium.common.exceptions import NoSuchWindowException
3967
from seleniumbase.common.exceptions import NoSuchFileException
68+
from seleniumbase.common.exceptions import TextNotVisibleException
4069

4170
if exception == Exception:
4271
exc = Exception
@@ -101,6 +130,8 @@ def check_if_time_limit_exceeded():
101130
and sb_config.time_limit
102131
and not sb_config.recorder_mode
103132
):
133+
import time
134+
104135
time_limit = sb_config.time_limit
105136
now_ms = int(time.time() * 1000)
106137
if now_ms > sb_config.start_time_ms + sb_config.time_limit_ms:

seleniumbase/plugins/sb_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def SB(
9595
from seleniumbase import config as sb_config
9696
from seleniumbase.config import settings
9797
from seleniumbase.fixtures import constants
98+
from seleniumbase.fixtures import shared_utils
9899

99100
sb_config_backup = sb_config
100101
sys_argv = sys.argv
@@ -692,6 +693,7 @@ def SB(
692693
else:
693694
sb.headless_active = False
694695
test_name = None
696+
terminal_width = shared_utils.get_terminal_width()
695697
if test:
696698
import colorama
697699
import os
@@ -703,7 +705,6 @@ def SB(
703705
stack_base = traceback.format_stack()[0].split(os.sep)[-1]
704706
test_name = stack_base.split(", in ")[0].replace('", line ', ":")
705707
test_name += ":SB"
706-
terminal_width = os.get_terminal_size()[0]
707708
start_text = "=== {%s} starts ===" % test_name
708709
remaining_spaces = terminal_width - len(start_text)
709710
left_space = ""
@@ -764,7 +765,6 @@ def SB(
764765
if not test_passed:
765766
result = "failed"
766767
c1 = colorama.Fore.RED
767-
terminal_width = os.get_terminal_size()[0]
768768
end_text = (
769769
"=== {%s} %s in %.2fs ==="
770770
% (test_name, result, run_time)

0 commit comments

Comments
 (0)