1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import math
import uuid
from asyncio.tasks import Task
from typing import Any, Callable, List, Tuple, Union
from pyee import EventEmitter
from playwright._impl._connection import ChannelOwner
from playwright._impl._errors import Error, TimeoutError
class Waiter:
def __init__(self, channel_owner: ChannelOwner, event: str) -> None:
self._result: asyncio.Future = asyncio.Future()
self._wait_id = uuid.uuid4().hex
self._loop = channel_owner._loop
self._pending_tasks: List[Task] = []
self._channel = channel_owner._channel
self._registered_listeners: List[Tuple[EventEmitter, str, Callable]] = []
self._logs: List[str] = []
self._wait_for_event_info_before(self._wait_id, event)
def _wait_for_event_info_before(self, wait_id: str, event: str) -> None:
self._channel.send_no_reply(
"waitForEventInfo",
None,
{
"info": {
"waitId": wait_id,
"phase": "before",
"event": event,
}
},
)
def _wait_for_event_info_after(self, wait_id: str, error: Exception = None) -> None:
self._channel._connection.wrap_api_call_sync(
lambda: self._channel.send_no_reply(
"waitForEventInfo",
None,
{
"info": {
"waitId": wait_id,
"phase": "after",
**({"error": str(error)} if error else {}),
},
},
),
True,
)
def reject_on_event(
self,
emitter: EventEmitter,
event: str,
error: Union[Error, Callable[..., Error]],
predicate: Callable = None,
) -> None:
def listener(event_data: Any = None) -> None:
if not predicate or predicate(event_data):
self._reject(error() if callable(error) else error)
emitter.on(event, listener)
self._registered_listeners.append((emitter, event, listener))
def reject_on_timeout(self, timeout: float, message: str) -> None:
if timeout == 0:
return
async def reject() -> None:
await asyncio.sleep(timeout / 1000)
self._reject(TimeoutError(message))
self._pending_tasks.append(self._loop.create_task(reject()))
def _cleanup(self) -> None:
for task in self._pending_tasks:
if not task.done():
task.cancel()
for listener in self._registered_listeners:
listener[0].remove_listener(listener[1], listener[2])
def _fulfill(self, result: Any) -> None:
self._cleanup()
if not self._result.done():
self._result.set_result(result)
self._wait_for_event_info_after(self._wait_id)
def _reject(self, exception: Exception) -> None:
self._cleanup()
if exception:
base_class = TimeoutError if isinstance(exception, TimeoutError) else Error
exception = base_class(str(exception) + format_log_recording(self._logs))
if not self._result.done():
self._result.set_exception(exception)
self._wait_for_event_info_after(self._wait_id, exception)
def wait_for_event(
self,
emitter: EventEmitter,
event: str,
predicate: Callable = None,
) -> None:
def listener(event_data: Any = None) -> None:
if not predicate or predicate(event_data):
self._fulfill(event_data)
emitter.on(event, listener)
self._registered_listeners.append((emitter, event, listener))
def result(self) -> asyncio.Future:
return self._result
def log(self, message: str) -> None:
self._logs.append(message)
try:
self._channel._connection.wrap_api_call_sync(
lambda: self._channel.send_no_reply(
"waitForEventInfo",
None,
{
"info": {
"waitId": self._wait_id,
"phase": "log",
"message": message,
},
},
),
True,
)
except Exception:
pass
def throw_on_timeout(timeout: float, exception: Exception) -> asyncio.Task:
async def throw() -> None:
await asyncio.sleep(timeout / 1000)
raise exception
return asyncio.create_task(throw())
def format_log_recording(log: List[str]) -> str:
if not log:
return ""
header = " logs "
header_length = 60
left_length = math.floor((header_length - len(header)) / 2)
right_length = header_length - len(header) - left_length
new_line = "\n"
return f"{new_line}{'=' * left_length}{header}{'=' * right_length}{new_line}{new_line.join(log)}{new_line}{'=' * header_length}"
|