Skip to content

Commit 31534dd

Browse files
committed
Starting BrowserBatteries
1 parent b7cd3ed commit 31534dd

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ Browser/.installed
3636

3737
browser_batteries/BrowserBatteries/bin/
3838
browser_batteries/robotframework_browser_batteries.egg-info
39-
browser_batteries/.DS_Store
4039

4140
# Robot Framework outputs
4241
log.html

Browser/dev-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pytest >= 7.4.2
33
pytest-watch >= 4.2.0
44
mypy >= 1.8.0
55
mypy-protobuf >= 3.5.0
6-
ruff>=0.12.0
6+
ruff>=0.12.8
77
wheel>=0.40.0
88
robotframework-pabot >= 4.0.0
99
twine >= 4.0.2

Browser/playwright.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
import platform
1919
import time
2020
from functools import cached_property
21+
from io import TextIOWrapper
2122
from pathlib import Path
2223
from subprocess import DEVNULL, STDOUT, CalledProcessError, Popen, run
2324
from typing import TYPE_CHECKING, Optional, Union
2425

2526
import grpc # type: ignore
2627

28+
try:
29+
from BrowserBatteries import start_grpc_server
30+
except ImportError:
31+
start_grpc_server = None # type: ignore[assignment]
32+
2733
from Browser.generated import playwright_pb2_grpc
2834
from Browser.generated.playwright_pb2 import Request
2935

@@ -36,6 +42,10 @@
3642
from .utils import PlaywrightLogTypes, find_free_port, logger
3743

3844

45+
def _run_grpc_server() -> bool:
46+
return start_grpc_server is not None
47+
48+
3949
class Playwright(LibraryComponent):
4050
"""A wrapper for communicating with nodejs Playwright process."""
4151

@@ -66,7 +76,15 @@ def _playwright_process(self) -> Optional[Popen]:
6676
return process
6777

6878
def ensure_node_dependencies(self):
69-
# Checks if node is in PATH, errors if it isn't
79+
"""Ensure that node dependencies are installed.
80+
81+
If BrowserBatteries is installed, does nothing.
82+
"""
83+
if _run_grpc_server():
84+
logger.trace(
85+
"Running gRPC server from BrowserBatteries, no need to check node"
86+
)
87+
return
7088
try:
7189
run(["node", "-v"], stdout=DEVNULL, check=True)
7290
except (CalledProcessError, FileNotFoundError, PermissionError) as err:
@@ -114,13 +132,19 @@ def start_playwright(self) -> Optional[Popen]:
114132
f"ROBOT_FRAMEWORK_BROWSER_NODE_PORT {existing_port} defined in env, skipping Browser process start"
115133
)
116134
return None
117-
current_dir = Path(__file__).parent
118-
workdir = current_dir / "wrapper"
119-
playwright_script = workdir / "index.js"
120135
if self.playwright_log:
121136
logfile = self.playwright_log.open("w", encoding="utf-8")
122137
else:
123138
logfile = Path(os.devnull).open("w", encoding="utf-8") # noqa: SIM115
139+
if _run_grpc_server():
140+
return start_grpc_server(logfile)
141+
return self._start_playwright_from_node(logfile)
142+
143+
def _start_playwright_from_node(self, logfile: TextIOWrapper) -> Popen:
144+
"""Start Playwright from nodejs wrapper."""
145+
current_dir = Path(__file__).parent
146+
workdir = current_dir / "wrapper"
147+
playwright_script = workdir / "index.js"
124148
host = str(self.host) if self.host is not None else "127.0.0.1"
125149
port = str(find_free_port())
126150
if self.enable_playwright_debug == PlaywrightLogTypes.playwright:

browser_batteries/BrowserBatteries/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,16 @@
1313
# limitations under the License.
1414

1515

16-
def run_grpc_server():
17-
"""Run the gRPC server."""
16+
from io import TextIOWrapper
17+
from pathlib import Path
18+
from subprocess import Popen
19+
20+
from robot.api import logger
21+
22+
23+
def start_grpc_server(logfile: TextIOWrapper) -> Popen:
24+
"""Run the prebuilt gRPC server."""
25+
current_dir = Path(__file__).parent
26+
playwright_script = current_dir / "bin" / "grpc_server"
27+
logger.trace(f"Starting gRPC server from {playwright_script}")
28+
return Popen([str(playwright_script)])

browser_batteries/BrowserBatteries/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)