Skip to content

Commit 77831cd

Browse files
authored
Merge pull request #3057 from locustio/Use-enter-to-automatically-open-web-ui-in-default-browser
Use enter to automatically open web UI in default browser
2 parents c6eaf25 + a8c7b90 commit 77831cd

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

Diff for: locust/main.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import sys
1515
import time
1616
import traceback
17+
import webbrowser
1718

1819
import gevent
1920

@@ -444,16 +445,12 @@ def kill_workers(children):
444445
else:
445446
web_host = options.web_host
446447
if web_host:
447-
logger.info(f"Starting web interface at {protocol}://{web_host}:{options.web_port}{options.web_base_path}")
448-
if options.web_host_display_name:
449-
logger.info(f"Starting web interface at {options.web_host_display_name}")
448+
url = f"{protocol}://{web_host}:{options.web_port}{options.web_base_path}"
449+
elif options.web_host_display_name:
450+
url = f"{options.web_host_display_name}"
450451
else:
451-
if os.name == "nt":
452-
logger.info(
453-
f"Starting web interface at {protocol}://localhost:{options.web_port}{options.web_base_path} (accepting connections from all network interfaces)"
454-
)
455-
else:
456-
logger.info(f"Starting web interface at {protocol}://0.0.0.0:{options.web_port}{options.web_base_path}")
452+
url = f"{protocol}://{'localhost' if os.name == 'nt' else '0.0.0.0'}:{options.web_port}{options.web_base_path}"
453+
logger.info(f"Starting web interface at {url}, press enter to open your default browser.")
457454

458455
web_ui = environment.create_web_ui(
459456
host=web_host,
@@ -597,6 +594,8 @@ def start_automatic_run():
597594
"S": lambda: runner.start(max(0, runner.user_count - 10), 100)
598595
if runner.state != "spawning"
599596
else logging.warning("Spawning users, can't stop right now"),
597+
"\r": lambda: webbrowser.open_new_tab(url),
598+
"\n": lambda: webbrowser.open_new_tab(url),
600599
},
601600
)
602601
)

Diff for: locust/test/test_main.py

+51
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,57 @@ def t(self):
934934
self.assertIn("Shutting down (exit code 0)", output)
935935
self.assertEqual(0, proc.returncode)
936936

937+
@unittest.skipIf(os.name == "nt", reason="termios doesnt exist on windows, and thus we cannot import pty")
938+
def test_autospawn_browser(self):
939+
import pty
940+
941+
LOCUSTFILE_CONTENT = textwrap.dedent(
942+
"""
943+
944+
from pytest import MonkeyPatch
945+
import sys
946+
import webbrowser
947+
948+
monkeypatch = MonkeyPatch()
949+
950+
def open_new_tab(url):
951+
print("browser opened with url", url)
952+
sys.exit(0)
953+
954+
monkeypatch.setattr(webbrowser, "open_new_tab", open_new_tab)
955+
print("patched")
956+
from locust import User, TaskSet, task, between
957+
class UserSubclass(User):
958+
@task
959+
def t(self):
960+
print("Test task is running")
961+
962+
"""
963+
)
964+
with mock_locustfile(content=LOCUSTFILE_CONTENT) as mocked:
965+
stdin_m, stdin_s = pty.openpty()
966+
stdin = os.fdopen(stdin_m, "wb", 0)
967+
968+
proc = subprocess.Popen(
969+
[
970+
"locust",
971+
"-f",
972+
mocked.file_path,
973+
],
974+
stdin=stdin_s,
975+
stdout=PIPE,
976+
text=True,
977+
)
978+
gevent.sleep(1)
979+
stdin.write(b"\n")
980+
try:
981+
output, _ = proc.communicate(timeout=1)
982+
except Exception:
983+
proc.kill()
984+
output, _ = proc.communicate()
985+
986+
self.assertIn("browser opened", output)
987+
937988
def test_spawning_with_fixed(self):
938989
LOCUSTFILE_CONTENT = textwrap.dedent(
939990
"""

0 commit comments

Comments
 (0)