Skip to content

Commit 5d0f51f

Browse files
committed
[py] User Server class in conftest
1 parent 77eb0a6 commit 5d0f51f

File tree

2 files changed

+47
-84
lines changed

2 files changed

+47
-84
lines changed

py/conftest.py

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717

1818
import os
1919
import platform
20-
import socket
21-
import subprocess
22-
import time
23-
from urllib.request import urlopen
2420

2521
import pytest
2622

2723
from selenium import webdriver
24+
from selenium.webdriver.remote.server import Server
2825
from test.selenium.webdriver.common.network import get_lan_ip
2926
from test.selenium.webdriver.common.webserver import SimpleWebServer
3027

@@ -125,7 +122,7 @@ def driver(request):
125122

126123
# skip tests in the 'remote' directory if run with a local driver
127124
if request.node.path.parts[-2] == "remote" and driver_class != "Remote":
128-
pytest.skip(f"Remote tests can't be run with driver '{driver_option}'")
125+
pytest.skip(f"Remote tests can't be run with driver '{driver_option.lower()}'")
129126

130127
# skip tests that can't run on certain platforms
131128
_platform = platform.system()
@@ -295,60 +292,21 @@ def server(request):
295292
yield None
296293
return
297294

298-
_host = "localhost"
299-
_port = 4444
300-
_path = os.path.join(
295+
path = os.path.join(
301296
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
302297
"java/src/org/openqa/selenium/grid/selenium_server_deploy.jar",
303298
)
304299

305-
def wait_for_server(url, timeout):
306-
start = time.time()
307-
while time.time() - start < timeout:
308-
try:
309-
urlopen(url)
310-
return 1
311-
except OSError:
312-
time.sleep(0.2)
313-
return 0
314-
315-
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
316-
url = f"http://{_host}:{_port}/status"
317-
try:
318-
_socket.connect((_host, _port))
319-
print(
320-
"The remote driver server is already running or something else"
321-
"is using port {}, continuing...".format(_port)
322-
)
323-
except Exception:
324-
remote_env = os.environ.copy()
325-
if platform.system() == "Linux":
326-
# There are issues with window size/position when running Firefox
327-
# under Wayland, so we use XWayland instead.
328-
remote_env["MOZ_ENABLE_WAYLAND"] = "0"
329-
print("Starting the Selenium server")
330-
process = subprocess.Popen(
331-
[
332-
"java",
333-
"-jar",
334-
_path,
335-
"standalone",
336-
"--port",
337-
"4444",
338-
"--selenium-manager",
339-
"true",
340-
"--enable-managed-downloads",
341-
"true",
342-
],
343-
env=remote_env,
344-
)
345-
print(f"Selenium server running as process: {process.pid}")
346-
assert wait_for_server(url, 10), f"Timed out waiting for Selenium server at {url}"
347-
print("Selenium server is ready")
348-
yield process
349-
process.terminate()
350-
process.wait()
351-
print("Selenium server has been terminated")
300+
remote_env = os.environ.copy()
301+
if platform.system() == "Linux":
302+
# There are issues with window size/position when running Firefox
303+
# under Wayland, so we use XWayland instead.
304+
remote_env["MOZ_ENABLE_WAYLAND"] = "0"
305+
306+
server = Server(path=path, env=remote_env)
307+
server.start()
308+
yield server
309+
server.stop()
352310

353311

354312
@pytest.fixture(autouse=True, scope="session")

py/selenium/webdriver/remote/server.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,23 @@
2929
class Server:
3030
"""Manage the Selenium Remote (Grid) Server.
3131
32+
Selenium Manager will detect the server location and download it if necessary, unless an existing server path is specified.
33+
3234
Parameters:
3335
-----------
3436
host : str
35-
Hostname or IP address to bind to.
37+
Hostname or IP address to bind to (determined automatically if not specified)
3638
port : str
37-
Port to listen on.
39+
Port to listen on ('4444' if not specified)
3840
path : str
39-
Path/filename of existing server .jar file (if not specified, server will be downloaded).
41+
Path/filename of existing server .jar file (Selenium Manager is used if not specified)
4042
version : str
41-
Version of server to download (if not specified, latest will be downloaded).
43+
Version of server to download (latest version if not specified)
4244
env: dict
43-
Environment variables passed to server environment.
45+
Environment variables passed to server environment
4446
"""
4547

46-
def __init__(self, host="localhost", port="4444", path=None, version=None, env=None):
48+
def __init__(self, host=None, port="4444", path=None, version=None, env=None):
4749
if path and version:
4850
raise TypeError("Not allowed to specify a version when using an existing server path")
4951

@@ -79,38 +81,41 @@ def _wait_for_server(self, timeout=10):
7981

8082
def start(self):
8183
"""Start the server."""
82-
if not self.path:
84+
85+
if self.path is None:
8386
selenium_manager = SeleniumManager()
8487
args = ["--grid"]
8588
if self.version:
8689
args.append(self.version)
8790
self.path = selenium_manager.binary_paths(args)["driver_path"]
88-
java = shutil.which("java")
89-
if java is None:
91+
92+
java_path = shutil.which("java")
93+
if java_path is None:
9094
raise OSError("Can't find java on system PATH. JRE is required to run the Selenium server")
95+
96+
command = [
97+
java_path,
98+
"-jar",
99+
self.path,
100+
"standalone",
101+
"--port",
102+
self.port,
103+
"--selenium-manager",
104+
"true",
105+
"--enable-managed-downloads",
106+
"true",
107+
]
108+
if self.host is not None:
109+
command.extend(["--host", self.host])
110+
91111
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
112+
host = self.host if self.host is not None else "localhost"
92113
try:
93-
sock.connect((self.host, int(self.port)))
94-
raise ConnectionError(
95-
f"The remote driver server is already running or something else is using port {self.port}"
96-
)
114+
sock.connect((host, int(self.port)))
115+
raise ConnectionError(f"Selenium server is already running, or something else is using port {self.port}")
97116
except ConnectionRefusedError:
98-
print("Starting Selenium server")
99-
self.process = subprocess.Popen(
100-
[
101-
"java",
102-
"-jar",
103-
self.path,
104-
"standalone",
105-
"--port",
106-
self.port,
107-
"--selenium-manager",
108-
"true",
109-
"--enable-managed-downloads",
110-
"true",
111-
],
112-
env=self.env,
113-
)
117+
print(f"Starting Selenium server at: {self.path}")
118+
self.process = subprocess.Popen(command, env=self.env)
114119
print(f"Selenium server running as process: {self.process.pid}")
115120
if not self._wait_for_server():
116121
f"Timed out waiting for Selenium server at {self.status_url}"

0 commit comments

Comments
 (0)