|
29 | 29 | class Server: |
30 | 30 | """Manage the Selenium Remote (Grid) Server. |
31 | 31 |
|
| 32 | + Selenium Manager will detect the server location and download it if necessary, unless an existing server path is specified. |
| 33 | +
|
32 | 34 | Parameters: |
33 | 35 | ----------- |
34 | 36 | host : str |
35 | | - Hostname or IP address to bind to. |
| 37 | + Hostname or IP address to bind to (determined automatically if not specified) |
36 | 38 | port : str |
37 | | - Port to listen on. |
| 39 | + Port to listen on ('4444' if not specified) |
38 | 40 | 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) |
40 | 42 | 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) |
42 | 44 | env: dict |
43 | | - Environment variables passed to server environment. |
| 45 | + Environment variables passed to server environment |
44 | 46 | """ |
45 | 47 |
|
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): |
47 | 49 | if path and version: |
48 | 50 | raise TypeError("Not allowed to specify a version when using an existing server path") |
49 | 51 |
|
@@ -79,38 +81,41 @@ def _wait_for_server(self, timeout=10): |
79 | 81 |
|
80 | 82 | def start(self): |
81 | 83 | """Start the server.""" |
82 | | - if not self.path: |
| 84 | + |
| 85 | + if self.path is None: |
83 | 86 | selenium_manager = SeleniumManager() |
84 | 87 | args = ["--grid"] |
85 | 88 | if self.version: |
86 | 89 | args.append(self.version) |
87 | 90 | 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: |
90 | 94 | 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 | + |
91 | 111 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 112 | + host = self.host if self.host is not None else "localhost" |
92 | 113 | 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}") |
97 | 116 | 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) |
114 | 119 | print(f"Selenium server running as process: {self.process.pid}") |
115 | 120 | if not self._wait_for_server(): |
116 | 121 | f"Timed out waiting for Selenium server at {self.status_url}" |
|
0 commit comments