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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
# 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 pytest
from playwright.async_api import Error, Locator, Page, expect
from tests.server import Server
from tests.utils import TARGET_CLOSED_ERROR_MESSAGE
async def test_should_work(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
before_count = 0
after_count = 0
original_locator = page.get_by_text("This interstitial covers the button")
async def handler(locator: Locator) -> None:
assert locator == original_locator
nonlocal before_count
nonlocal after_count
before_count += 1
await page.locator("#close").click()
after_count += 1
await page.add_locator_handler(original_locator, handler)
for args in [
["mouseover", 1],
["mouseover", 1, "capture"],
["mouseover", 2],
["mouseover", 2, "capture"],
["pointerover", 1],
["pointerover", 1, "capture"],
["none", 1],
["remove", 1],
["hide", 1],
]:
await page.locator("#aside").hover()
before_count = 0
after_count = 0
await page.evaluate(
"(args) => { window.clicked = 0; window.setupAnnoyingInterstitial(...args); }",
args,
)
assert before_count == 0
assert after_count == 0
await page.locator("#target").click()
assert before_count == args[1]
assert after_count == args[1]
assert await page.evaluate("window.clicked") == 1
await expect(page.locator("#interstitial")).not_to_be_visible()
async def test_should_work_with_a_custom_check(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
async def handler() -> None:
if await page.get_by_text("This interstitial covers the button").is_visible():
await page.locator("#close").click()
await page.add_locator_handler(page.locator("body"), handler, no_wait_after=True)
for args in [
["mouseover", 2],
["none", 1],
["remove", 1],
["hide", 1],
]:
await page.locator("#aside").hover()
await page.evaluate(
"(args) => { window.clicked = 0; window.setupAnnoyingInterstitial(...args); }",
args,
)
await page.locator("#target").click()
assert await page.evaluate("window.clicked") == 1
await expect(page.locator("#interstitial")).not_to_be_visible()
async def test_should_work_with_locator_hover(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
await page.add_locator_handler(
page.get_by_text("This interstitial covers the button"),
lambda: page.locator("#close").click(),
)
await page.locator("#aside").hover()
await page.evaluate(
'() => { window.setupAnnoyingInterstitial("pointerover", 1, "capture"); }'
)
await page.locator("#target").hover()
await expect(page.locator("#interstitial")).not_to_be_visible()
assert (
await page.eval_on_selector(
"#target", "e => window.getComputedStyle(e).backgroundColor"
)
== "rgb(255, 255, 0)"
)
async def test_should_not_work_with_force_true(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
await page.add_locator_handler(
page.get_by_text("This interstitial covers the button"),
lambda: page.locator("#close").click(),
)
await page.locator("#aside").hover()
await page.evaluate('() => { window.setupAnnoyingInterstitial("none", 1); }')
await page.locator("#target").click(force=True, timeout=2000)
assert await page.locator("#interstitial").is_visible()
assert await page.evaluate("window.clicked") is None
async def test_should_throw_when_page_closes(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
await page.add_locator_handler(
page.get_by_text("This interstitial covers the button"), lambda: page.close()
)
await page.locator("#aside").hover()
await page.evaluate(
'() => { window.clicked = 0; window.setupAnnoyingInterstitial("mouseover", 1); }'
)
with pytest.raises(Error) as exc:
await page.locator("#target").click()
assert TARGET_CLOSED_ERROR_MESSAGE in exc.value.message
async def test_should_throw_when_handler_times_out(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
called = 0
stall_future: asyncio.Future[None] = asyncio.Future()
async def handler() -> None:
nonlocal called
called += 1
# Deliberately timeout.
await stall_future
await page.add_locator_handler(
page.get_by_text("This interstitial covers the button"), handler
)
await page.locator("#aside").hover()
await page.evaluate(
'() => { window.clicked = 0; window.setupAnnoyingInterstitial("mouseover", 1); }'
)
with pytest.raises(Error) as exc:
await page.locator("#target").click(timeout=3000)
assert "Timeout 3000ms exceeded" in exc.value.message
with pytest.raises(Error) as exc:
await page.locator("#target").click(timeout=3000)
assert "Timeout 3000ms exceeded" in exc.value.message
# Should not enter the same handler while it is still running.
assert called == 1
stall_future.cancel()
async def test_should_work_with_to_be_visible(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
called = 0
async def handler() -> None:
nonlocal called
called += 1
await page.locator("#close").click()
await page.add_locator_handler(
page.get_by_text("This interstitial covers the button"), handler
)
await page.evaluate(
'() => { window.clicked = 0; window.setupAnnoyingInterstitial("remove", 1); }'
)
await expect(page.locator("#target")).to_be_visible()
await expect(page.locator("#interstitial")).not_to_be_visible()
assert called == 1
async def test_should_work_when_owner_frame_detaches(
page: Page, server: Server
) -> None:
await page.goto(server.EMPTY_PAGE)
await page.evaluate(
"""
() => {
const iframe = document.createElement('iframe');
iframe.src = 'data:text/html,<body>hello from iframe</body>';
document.body.append(iframe);
const target = document.createElement('button');
target.textContent = 'Click me';
target.id = 'target';
target.addEventListener('click', () => window._clicked = true);
document.body.appendChild(target);
const closeButton = document.createElement('button');
closeButton.textContent = 'close';
closeButton.id = 'close';
closeButton.addEventListener('click', () => iframe.remove());
document.body.appendChild(closeButton);
}
"""
)
await page.add_locator_handler(
page.frame_locator("iframe").locator("body"),
lambda: page.locator("#close").click(),
)
await page.locator("#target").click()
assert await page.query_selector("iframe") is None
assert await page.evaluate("window._clicked") is True
async def test_should_work_with_times_option(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
called = 0
def _handler() -> None:
nonlocal called
called += 1
await page.add_locator_handler(
page.locator("body"), _handler, no_wait_after=True, times=2
)
await page.locator("#aside").hover()
await page.evaluate(
"""
() => {
window.clicked = 0;
window.setupAnnoyingInterstitial('mouseover', 4);
}
"""
)
with pytest.raises(Error) as exc_info:
await page.locator("#target").click(timeout=3000)
assert called == 2
assert await page.evaluate("window.clicked") == 0
await expect(page.locator("#interstitial")).to_be_visible()
assert "Timeout 3000ms exceeded" in exc_info.value.message
assert (
'<div>This interstitial covers the button</div> from <div class="visible" id="interstitial">…</div> subtree intercepts pointer events'
in exc_info.value.message
)
async def test_should_wait_for_hidden_by_default(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
called = 0
async def _handler(button: Locator) -> None:
nonlocal called
called += 1
await button.click()
await page.add_locator_handler(page.get_by_role("button", name="close"), _handler)
await page.locator("#aside").hover()
await page.evaluate(
"""
() => {
window.clicked = 0;
window.setupAnnoyingInterstitial('timeout', 1);
}
"""
)
await page.locator("#target").click()
assert await page.evaluate("window.clicked") == 1
await expect(page.locator("#interstitial")).not_to_be_visible()
assert called == 1
async def test_should_wait_for_hidden_by_default_2(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
called = 0
def _handler() -> None:
nonlocal called
called += 1
await page.add_locator_handler(page.get_by_role("button", name="close"), _handler)
await page.locator("#aside").hover()
await page.evaluate(
"""
() => {
window.clicked = 0;
window.setupAnnoyingInterstitial('hide', 1);
}
"""
)
with pytest.raises(Error) as exc_info:
await page.locator("#target").click(timeout=3000)
assert await page.evaluate("window.clicked") == 0
assert await page.locator("#interstitial").is_visible()
assert called == 1
assert (
'locator handler has finished, waiting for get_by_role("button", name="close") to be hidden'
in exc_info.value.message
)
async def test_should_work_with_noWaitAfter(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
called = 0
async def _handler(button: Locator) -> None:
nonlocal called
called += 1
if called == 1:
await button.click()
else:
await page.locator("#interstitial").wait_for(state="hidden")
await page.add_locator_handler(
page.get_by_role("button", name="close"), _handler, no_wait_after=True
)
await page.locator("#aside").hover()
await page.evaluate(
"""
() => {
window.clicked = 0;
window.setupAnnoyingInterstitial('timeout', 1);
}
"""
)
await page.locator("#target").click()
assert await page.evaluate("window.clicked") == 1
await expect(page.locator("#interstitial")).not_to_be_visible()
assert called == 2
async def test_should_removeLocatorHandler(page: Page, server: Server) -> None:
await page.goto(server.PREFIX + "/input/handle-locator.html")
called = 0
async def _handler(locator: Locator) -> None:
nonlocal called
called += 1
await locator.click()
await page.add_locator_handler(page.get_by_role("button", name="close"), _handler)
await page.evaluate(
"""
() => {
window.clicked = 0;
window.setupAnnoyingInterstitial('hide', 1);
}
"""
)
await page.locator("#target").click()
assert called == 1
assert await page.evaluate("window.clicked") == 1
await expect(page.locator("#interstitial")).not_to_be_visible()
await page.evaluate(
"""
() => {
window.clicked = 0;
window.setupAnnoyingInterstitial('hide', 1);
}
"""
)
await page.remove_locator_handler(page.get_by_role("button", name="close"))
with pytest.raises(Error) as error:
await page.locator("#target").click(timeout=3000)
assert called == 1
assert await page.evaluate("window.clicked") == 0
await expect(page.locator("#interstitial")).to_be_visible()
assert "Timeout 3000ms exceeded" in error.value.message
|