@@ -42,6 +42,7 @@ def _get_stderr_fileno() -> Optional[int]:
42
42
43
43
class Transport (ABC ):
44
44
def __init__ (self ) -> None :
45
+ self .on_error_future : asyncio .Future
45
46
self .on_message = lambda _ : None
46
47
47
48
@abstractmethod
@@ -55,9 +56,14 @@ def dispose(self) -> None:
55
56
async def wait_until_stopped (self ) -> None :
56
57
pass
57
58
58
- async def run (self ) -> None :
59
+ async def start (self ) -> None :
60
+ if not hasattr (self , "on_error_future" ):
61
+ self .on_error_future = asyncio .Future ()
59
62
self ._loop = asyncio .get_running_loop ()
60
- self .on_error_future : asyncio .Future = asyncio .Future ()
63
+
64
+ @abstractmethod
65
+ async def run (self ) -> None :
66
+ pass
61
67
62
68
@abstractmethod
63
69
def send (self , message : Dict ) -> None :
@@ -93,17 +99,28 @@ async def wait_until_stopped(self) -> None:
93
99
await self ._proc .wait ()
94
100
95
101
async def run (self ) -> None :
96
- await super (). run ()
102
+ await self . start ()
97
103
self ._stopped_future : asyncio .Future = asyncio .Future ()
98
104
99
- self ._proc = proc = await asyncio .create_subprocess_exec (
100
- str (self ._driver_executable ),
101
- "run-driver" ,
102
- stdin = asyncio .subprocess .PIPE ,
103
- stdout = asyncio .subprocess .PIPE ,
104
- stderr = _get_stderr_fileno (),
105
- limit = 32768 ,
106
- )
105
+ try :
106
+ self ._proc = proc = await asyncio .create_subprocess_exec (
107
+ str (self ._driver_executable ),
108
+ "run-driver" ,
109
+ stdin = asyncio .subprocess .PIPE ,
110
+ stdout = asyncio .subprocess .PIPE ,
111
+ stderr = _get_stderr_fileno (),
112
+ limit = 32768 ,
113
+ )
114
+ except FileNotFoundError :
115
+ self .on_error_future .set_exception (
116
+ Error (
117
+ "playwright's driver is not found, You can read the contributing guide "
118
+ "for some guidance on how to get everything setup for working on the code "
119
+ "https://github.com/microsoft/playwright-python/blob/master/CONTRIBUTING.md"
120
+ )
121
+ )
122
+ return
123
+
107
124
assert proc .stdout
108
125
assert proc .stdin
109
126
self ._output = proc .stdin
@@ -160,15 +177,22 @@ async def wait_until_stopped(self) -> None:
160
177
await self ._connection .wait_closed ()
161
178
162
179
async def run (self ) -> None :
163
- await super (). run ()
180
+ await self . start ()
164
181
165
182
options : Dict [str , Any ] = {}
166
183
if self .timeout is not None :
167
184
options ["close_timeout" ] = self .timeout / 1000
168
185
options ["ping_timeout" ] = self .timeout / 1000
186
+
169
187
if self .headers is not None :
170
188
options ["extra_headers" ] = self .headers
171
- self ._connection = await websockets .connect (self .ws_endpoint , ** options )
189
+ try :
190
+ self ._connection = await websockets .connect (self .ws_endpoint , ** options )
191
+ except Exception as err :
192
+ self .on_error_future .set_exception (
193
+ Error (f"playwright's websocket endpoint connection error: { err } " )
194
+ )
195
+ return
172
196
173
197
while not self ._stopped :
174
198
try :
0 commit comments