@@ -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 } " )
0 commit comments