Skip to content

Commit 494d90b

Browse files
committed
[py] Add server tests
1 parent 5d0f51f commit 494d90b

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

py/selenium/webdriver/remote/server.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class Server:
3535
-----------
3636
host : str
3737
Hostname or IP address to bind to (determined automatically if not specified)
38-
port : str
39-
Port to listen on ('4444' if not specified)
38+
port : int or str
39+
Port to listen on (4444 if not specified)
4040
path : str
4141
Path/filename of existing server .jar file (Selenium Manager is used if not specified)
4242
version : str
@@ -45,12 +45,12 @@ class Server:
4545
Environment variables passed to server environment
4646
"""
4747

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

5252
self.host = host
53-
self.port = port
53+
self.port = self._validate_port(port)
5454
self.path = self._validate_path(path)
5555
self.version = self._validate_version(version)
5656
self.env = env
@@ -63,10 +63,19 @@ def _validate_path(self, path):
6363
raise OSError(f"Can't find server .jar located at {path}")
6464
return path
6565

66+
def _validate_port(self, port):
67+
try:
68+
port = int(port)
69+
except ValueError:
70+
raise TypeError(f"{__class__.__name__}.__init__() got an invalid port: '{port}'")
71+
if not (0 <= port <= 65535):
72+
raise ValueError("port must be 0-65535")
73+
return port
74+
6675
def _validate_version(self, version):
6776
if version:
6877
if not re.match(r"^\d+\.\d+\.\d+$", str(version)):
69-
raise TypeError(f"{__class__}.__init__() invalid version argument: '{version}'")
78+
raise TypeError(f"{__class__.__name__}.__init__() got an invalid version: '{version}'")
7079
return version
7180

7281
def _wait_for_server(self, timeout=10):
@@ -99,7 +108,7 @@ def start(self):
99108
self.path,
100109
"standalone",
101110
"--port",
102-
self.port,
111+
str(self.port),
103112
"--selenium-manager",
104113
"true",
105114
"--enable-managed-downloads",
@@ -111,7 +120,7 @@ def start(self):
111120
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
112121
host = self.host if self.host is not None else "localhost"
113122
try:
114-
sock.connect((host, int(self.port)))
123+
sock.connect((host, self.port))
115124
raise ConnectionError(f"Selenium server is already running, or something else is using port {self.port}")
116125
except ConnectionRefusedError:
117126
print(f"Starting Selenium server at: {self.path}")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import re
19+
20+
import pytest
21+
22+
from selenium.webdriver.remote.server import Server
23+
24+
25+
def test_server_with_bad_path():
26+
path = "/path/to/nowhere"
27+
msg = f"Can't find server .jar located at {path}"
28+
with pytest.raises(OSError, match=re.escape(msg)):
29+
Server(path=path)
30+
31+
def test_server_with_invalid_version():
32+
versions = ("0.0", "invalid")
33+
for version in versions:
34+
msg = f"Server.__init__() got an invalid version: '{version}'"
35+
with pytest.raises(TypeError, match=re.escape(msg)):
36+
Server(version=version)
37+
38+
def test_server_with_invalid_port():
39+
port = "invalid"
40+
msg = f"Server.__init__() got an invalid port: '{port}'"
41+
with pytest.raises(TypeError, match=re.escape(msg)):
42+
Server(port=port)
43+
44+
def test_server_with_port_out_of_range():
45+
with pytest.raises(ValueError, match="port must be 0-65535"):
46+
Server(port=99999)

0 commit comments

Comments
 (0)