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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
|
# 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 json
import os
import re
import zipfile
from pathlib import Path
from typing import Awaitable, Callable, cast
import pytest
from playwright.async_api import Browser, BrowserContext, Error, Page, Route, expect
from tests.server import Server, TestServerRequest
from tests.utils import must
async def test_should_work(browser: Browser, server: Server, tmp_path: Path) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(record_har_path=path)
page = await context.new_page()
await page.goto(server.EMPTY_PAGE)
await context.close()
with open(path) as f:
data = json.load(f)
assert "log" in data
async def test_should_omit_content(
browser: Browser, server: Server, tmp_path: Path
) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(
record_har_path=path,
record_har_content="omit",
)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await context.close()
with open(path) as f:
data = json.load(f)
assert "log" in data
log = data["log"]
content1 = log["entries"][0]["response"]["content"]
assert "text" not in content1
assert "encoding" not in content1
async def test_should_omit_content_legacy(
browser: Browser, server: Server, tmp_path: Path
) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(
record_har_path=path, record_har_omit_content=True
)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await context.close()
with open(path) as f:
data = json.load(f)
assert "log" in data
log = data["log"]
content1 = log["entries"][0]["response"]["content"]
assert "text" not in content1
assert "encoding" not in content1
async def test_should_attach_content(
browser: Browser, server: Server, tmp_path: Path
) -> None:
path = os.path.join(tmp_path, "log.har.zip")
context = await browser.new_context(
record_har_path=path,
record_har_content="attach",
)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await page.evaluate("() => fetch('/pptr.png').then(r => r.arrayBuffer())")
await context.close()
with zipfile.ZipFile(path) as z:
with z.open("har.har") as har:
entries = json.load(har)["log"]["entries"]
assert "encoding" not in entries[0]["response"]["content"]
assert (
entries[0]["response"]["content"]["mimeType"]
== "text/html; charset=utf-8"
)
assert (
"75841480e2606c03389077304342fac2c58ccb1b"
in entries[0]["response"]["content"]["_file"]
)
assert entries[0]["response"]["content"]["size"] >= 96
assert entries[0]["response"]["content"]["compression"] == 0
assert "encoding" not in entries[1]["response"]["content"]
assert (
entries[1]["response"]["content"]["mimeType"]
== "text/css; charset=utf-8"
)
assert (
"79f739d7bc88e80f55b9891a22bf13a2b4e18adb"
in entries[1]["response"]["content"]["_file"]
)
assert entries[1]["response"]["content"]["size"] >= 37
assert entries[1]["response"]["content"]["compression"] == 0
assert "encoding" not in entries[2]["response"]["content"]
assert entries[2]["response"]["content"]["mimeType"] == "image/png"
assert (
"a4c3a18f0bb83f5d9fe7ce561e065c36205762fa"
in entries[2]["response"]["content"]["_file"]
)
assert entries[2]["response"]["content"]["size"] >= 6000
assert entries[2]["response"]["content"]["compression"] == 0
with z.open("75841480e2606c03389077304342fac2c58ccb1b.html") as f:
assert b"HAR Page" in f.read()
with z.open("79f739d7bc88e80f55b9891a22bf13a2b4e18adb.css") as f:
assert b"pink" in f.read()
with z.open("a4c3a18f0bb83f5d9fe7ce561e065c36205762fa.png") as f:
assert len(f.read()) == entries[2]["response"]["content"]["size"]
async def test_should_not_omit_content(
browser: Browser, server: Server, tmp_path: Path
) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(
record_har_path=path, record_har_omit_content=False
)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await context.close()
with open(path) as f:
data = json.load(f)
content1 = data["log"]["entries"][0]["response"]["content"]
assert "text" in content1
async def test_should_include_content(
browser: Browser, server: Server, tmp_path: Path
) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(record_har_path=path)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await context.close()
with open(path) as f:
data = json.load(f)
assert "log" in data
log = data["log"]
content1 = log["entries"][0]["response"]["content"]
assert content1["mimeType"] == "text/html; charset=utf-8"
assert "HAR Page" in content1["text"]
async def test_should_default_to_full_mode(
browser: Browser, server: Server, tmp_path: Path
) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(
record_har_path=path,
)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await context.close()
with open(path) as f:
data = json.load(f)
assert "log" in data
log = data["log"]
assert log["entries"][0]["request"]["bodySize"] >= 0
async def test_should_support_minimal_mode(
browser: Browser, server: Server, tmp_path: Path
) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(
record_har_path=path,
record_har_mode="minimal",
)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await context.close()
with open(path) as f:
data = json.load(f)
assert "log" in data
log = data["log"]
assert log["entries"][0]["request"]["bodySize"] == -1
async def test_should_filter_by_glob(
browser: Browser, server: Server, tmp_path: str
) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(
base_url=server.PREFIX,
record_har_path=path,
record_har_url_filter="/*.css",
ignore_https_errors=True,
)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await context.close()
with open(path) as f:
data = json.load(f)
assert "log" in data
log = data["log"]
assert len(log["entries"]) == 1
assert log["entries"][0]["request"]["url"].endswith("one-style.css")
async def test_should_filter_by_regexp(
browser: Browser, server: Server, tmp_path: str
) -> None:
path = os.path.join(tmp_path, "log.har")
context = await browser.new_context(
base_url=server.PREFIX,
record_har_path=path,
record_har_url_filter=re.compile("HAR.X?HTML", re.I),
ignore_https_errors=True,
)
page = await context.new_page()
await page.goto(server.PREFIX + "/har.html")
await context.close()
with open(path) as f:
data = json.load(f)
assert "log" in data
log = data["log"]
assert len(log["entries"]) == 1
assert log["entries"][0]["request"]["url"].endswith("har.html")
async def test_should_context_route_from_har_matching_the_method_and_following_redirects(
context: BrowserContext, assetdir: Path
) -> None:
await context.route_from_har(har=assetdir / "har-fulfill.har")
page = await context.new_page()
await page.goto("http://no.playwright/")
# HAR contains a redirect for the script that should be followed automatically.
assert await page.evaluate("window.value") == "foo"
# HAR contains a POST for the css file that should not be used.
await expect(page.locator("body")).to_have_css("background-color", "rgb(255, 0, 0)")
async def test_should_page_route_from_har_matching_the_method_and_following_redirects(
page: Page, assetdir: Path
) -> None:
await page.route_from_har(har=assetdir / "har-fulfill.har")
await page.goto("http://no.playwright/")
# HAR contains a redirect for the script that should be followed automatically.
assert await page.evaluate("window.value") == "foo"
# HAR contains a POST for the css file that should not be used.
await expect(page.locator("body")).to_have_css("background-color", "rgb(255, 0, 0)")
async def test_fallback_continue_should_continue_when_not_found_in_har(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(har=assetdir / "har-fulfill.har", not_found="fallback")
page = await context.new_page()
await page.goto(server.PREFIX + "/one-style.html")
await expect(page.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
async def test_by_default_should_abort_requests_not_found_in_har(
context: BrowserContext,
server: Server,
assetdir: Path,
is_chromium: bool,
is_webkit: bool,
) -> None:
await context.route_from_har(har=assetdir / "har-fulfill.har")
page = await context.new_page()
with pytest.raises(Error) as exc_info:
await page.goto(server.EMPTY_PAGE)
assert exc_info.value
if is_chromium:
assert "net::ERR_FAILED" in exc_info.value.message
elif is_webkit:
assert "Blocked by Web Inspector" in exc_info.value.message
else:
assert "NS_ERROR_FAILURE" in exc_info.value.message
async def test_fallback_continue_should_continue_requests_on_bad_har(
context: BrowserContext, server: Server, tmp_path: Path
) -> None:
path_to_invalid_har = tmp_path / "invalid.har"
with path_to_invalid_har.open("w") as f:
json.dump({"log": {}}, f)
await context.route_from_har(har=path_to_invalid_har, not_found="fallback")
page = await context.new_page()
await page.goto(server.PREFIX + "/one-style.html")
await expect(page.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
async def test_should_only_handle_requests_matching_url_filter(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(
har=assetdir / "har-fulfill.har", not_found="fallback", url="**/*.js"
)
page = await context.new_page()
async def handler(route: Route) -> None:
assert route.request.url == "http://no.playwright/"
await route.fulfill(
status=200,
content_type="text/html",
body='<script src="./script.js"></script><div>hello</div>',
)
await context.route("http://no.playwright/", handler)
await page.goto("http://no.playwright/")
assert await page.evaluate("window.value") == "foo"
await expect(page.locator("body")).to_have_css(
"background-color", "rgba(0, 0, 0, 0)"
)
async def test_should_only_handle_requests_matching_url_filter_no_fallback(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(har=assetdir / "har-fulfill.har", url="**/*.js")
page = await context.new_page()
async def handler(route: Route) -> None:
assert route.request.url == "http://no.playwright/"
await route.fulfill(
status=200,
content_type="text/html",
body='<script src="./script.js"></script><div>hello</div>',
)
await context.route("http://no.playwright/", handler)
await page.goto("http://no.playwright/")
assert await page.evaluate("window.value") == "foo"
await expect(page.locator("body")).to_have_css(
"background-color", "rgba(0, 0, 0, 0)"
)
async def test_should_only_handle_requests_matching_url_filter_no_fallback_page(
page: Page, server: Server, assetdir: Path
) -> None:
await page.route_from_har(har=assetdir / "har-fulfill.har", url="**/*.js")
async def handler(route: Route) -> None:
assert route.request.url == "http://no.playwright/"
await route.fulfill(
status=200,
content_type="text/html",
body='<script src="./script.js"></script><div>hello</div>',
)
await page.route("http://no.playwright/", handler)
await page.goto("http://no.playwright/")
assert await page.evaluate("window.value") == "foo"
await expect(page.locator("body")).to_have_css(
"background-color", "rgba(0, 0, 0, 0)"
)
async def test_should_support_regex_filter(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(
har=assetdir / "har-fulfill.har",
url=re.compile(r".*(\.js|.*\.css|no.playwright\/)"),
)
page = await context.new_page()
await page.goto("http://no.playwright/")
assert await page.evaluate("window.value") == "foo"
await expect(page.locator("body")).to_have_css("background-color", "rgb(255, 0, 0)")
async def test_should_change_document_url_after_redirected_navigation(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(har=assetdir / "har-redirect.har")
page = await context.new_page()
async with page.expect_navigation() as navigation_info:
await asyncio.gather(
page.wait_for_url("https://www.theverge.com/"),
page.goto("https://theverge.com/"),
)
response = await navigation_info.value
await expect(page).to_have_url("https://www.theverge.com/")
assert response.request.url == "https://www.theverge.com/"
assert await page.evaluate("window.location.href") == "https://www.theverge.com/"
async def test_should_change_document_url_after_redirected_navigation_on_click(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(
har=assetdir / "har-redirect.har", url=re.compile(r"/.*theverge.*/")
)
page = await context.new_page()
await page.goto(server.EMPTY_PAGE)
await page.set_content('<a href="https://theverge.com/">click me</a>')
async with page.expect_navigation() as navigation_info:
await asyncio.gather(
page.wait_for_url("https://www.theverge.com/"),
page.click("text=click me"),
)
response = await navigation_info.value
await expect(page).to_have_url("https://www.theverge.com/")
assert response.request.url == "https://www.theverge.com/"
assert await page.evaluate("window.location.href") == "https://www.theverge.com/"
async def test_should_go_back_to_redirected_navigation(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(
har=assetdir / "har-redirect.har", url=re.compile(r"/.*theverge.*/")
)
page = await context.new_page()
await page.goto("https://theverge.com/")
await page.goto(server.EMPTY_PAGE)
await expect(page).to_have_url(server.EMPTY_PAGE)
response = await page.go_back()
assert response
await expect(page).to_have_url("https://www.theverge.com/")
assert response.request.url == "https://www.theverge.com/"
assert await page.evaluate("window.location.href") == "https://www.theverge.com/"
async def test_should_go_forward_to_redirected_navigation(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(
har=assetdir / "har-redirect.har", url=re.compile(r"/.*theverge.*/")
)
page = await context.new_page()
await page.goto("https://theverge.com/")
await page.goto(server.EMPTY_PAGE)
await expect(page).to_have_url(server.EMPTY_PAGE)
await page.goto("https://theverge.com/")
await expect(page).to_have_url("https://www.theverge.com/")
await page.go_back()
await expect(page).to_have_url(server.EMPTY_PAGE)
response = await page.go_forward()
assert response
await expect(page).to_have_url("https://www.theverge.com/")
assert response.request.url == "https://www.theverge.com/"
assert await page.evaluate("window.location.href") == "https://www.theverge.com/"
async def test_should_reload_redirected_navigation(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(
har=assetdir / "har-redirect.har", url=re.compile(r"/.*theverge.*/")
)
page = await context.new_page()
await page.goto("https://theverge.com/")
await expect(page).to_have_url("https://www.theverge.com/")
response = await page.reload()
assert response
await expect(page).to_have_url("https://www.theverge.com/")
assert response.request.url == "https://www.theverge.com/"
assert await page.evaluate("window.location.href") == "https://www.theverge.com/"
async def test_should_fulfill_from_har_with_content_in_a_file(
context: BrowserContext, server: Server, assetdir: Path
) -> None:
await context.route_from_har(har=assetdir / "har-sha1.har")
page = await context.new_page()
await page.goto("http://no.playwright/")
assert await page.content() == "<html><head></head><body>Hello, world</body></html>"
async def test_should_round_trip_har_zip(
browser: Browser, server: Server, assetdir: Path, tmp_path: Path
) -> None:
har_path = tmp_path / "har.zip"
context_1 = await browser.new_context(
record_har_mode="minimal", record_har_path=har_path
)
page_1 = await context_1.new_page()
await page_1.goto(server.PREFIX + "/one-style.html")
await context_1.close()
context_2 = await browser.new_context()
await context_2.route_from_har(har=har_path, not_found="abort")
page_2 = await context_2.new_page()
await page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in await page_2.content()
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
async def test_should_round_trip_har_with_post_data(
browser: Browser, server: Server, assetdir: Path, tmp_path: Path
) -> None:
server.set_route("/echo", lambda req: (req.write(req.post_body), req.finish()))
fetch_function = """
async (body) => {
const response = await fetch('/echo', { method: 'POST', body });
return await response.text();
};
"""
har_path = tmp_path / "har.zip"
context_1 = await browser.new_context(
record_har_mode="minimal", record_har_path=har_path
)
page_1 = await context_1.new_page()
await page_1.goto(server.EMPTY_PAGE)
assert await page_1.evaluate(fetch_function, "1") == "1"
assert await page_1.evaluate(fetch_function, "2") == "2"
assert await page_1.evaluate(fetch_function, "3") == "3"
await context_1.close()
context_2 = await browser.new_context()
await context_2.route_from_har(har=har_path, not_found="abort")
page_2 = await context_2.new_page()
await page_2.goto(server.EMPTY_PAGE)
assert await page_2.evaluate(fetch_function, "1") == "1"
assert await page_2.evaluate(fetch_function, "2") == "2"
assert await page_2.evaluate(fetch_function, "3") == "3"
with pytest.raises(Exception):
await page_2.evaluate(fetch_function, "4")
async def test_should_disambiguate_by_header(
browser: Browser, server: Server, tmp_path: Path
) -> None:
server.set_route(
"/echo",
lambda req: (req.write(cast(str, req.getHeader("baz")).encode()), req.finish()),
)
fetch_function = """
async (bazValue) => {
const response = await fetch('/echo', {
method: 'POST',
body: '',
headers: {
foo: 'foo-value',
bar: 'bar-value',
baz: bazValue,
}
});
return await response.text();
};
"""
har_path = tmp_path / "har.zip"
context_1 = await browser.new_context(
record_har_mode="minimal", record_har_path=har_path
)
page_1 = await context_1.new_page()
await page_1.goto(server.EMPTY_PAGE)
assert await page_1.evaluate(fetch_function, "baz1") == "baz1"
assert await page_1.evaluate(fetch_function, "baz2") == "baz2"
assert await page_1.evaluate(fetch_function, "baz3") == "baz3"
await context_1.close()
context_2 = await browser.new_context()
await context_2.route_from_har(har=har_path)
page_2 = await context_2.new_page()
await page_2.goto(server.EMPTY_PAGE)
assert await page_2.evaluate(fetch_function, "baz1") == "baz1"
assert await page_2.evaluate(fetch_function, "baz2") == "baz2"
assert await page_2.evaluate(fetch_function, "baz3") == "baz3"
assert await page_2.evaluate(fetch_function, "baz4") == "baz1"
async def test_should_produce_extracted_zip(
browser: Browser, server: Server, tmp_path: Path
) -> None:
har_path = tmp_path / "har.har"
context = await browser.new_context(
record_har_mode="minimal", record_har_path=har_path, record_har_content="attach"
)
page_1 = await context.new_page()
await page_1.goto(server.PREFIX + "/one-style.html")
await context.close()
assert har_path.exists()
with har_path.open() as r:
content = r.read()
assert "log" in content
assert "background-color" not in r.read()
context_2 = await browser.new_context()
await context_2.route_from_har(har_path, not_found="abort")
page_2 = await context_2.new_page()
await page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in await page_2.content()
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
async def test_should_update_har_zip_for_context(
browser: Browser, server: Server, tmp_path: Path
) -> None:
har_path = tmp_path / "har.zip"
context = await browser.new_context()
await context.route_from_har(har_path, update=True)
page_1 = await context.new_page()
await page_1.goto(server.PREFIX + "/one-style.html")
await context.close()
assert har_path.exists()
context_2 = await browser.new_context()
await context_2.route_from_har(har_path, not_found="abort")
page_2 = await context_2.new_page()
await page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in await page_2.content()
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
async def test_page_unroute_all_should_stop_page_route_from_har(
context_factory: Callable[[], Awaitable[BrowserContext]],
server: Server,
assetdir: Path,
) -> None:
har_path = assetdir / "har-fulfill.har"
context1 = await context_factory()
page1 = await context1.new_page()
# The har file contains requests for another domain, so the router
# is expected to abort all requests.
await page1.route_from_har(har_path, not_found="abort")
with pytest.raises(Error) as exc_info:
await page1.goto(server.EMPTY_PAGE)
assert exc_info.value
await page1.unroute_all(behavior="wait")
response = must(await page1.goto(server.EMPTY_PAGE))
assert response.ok
async def test_context_unroute_call_should_stop_context_route_from_har(
context_factory: Callable[[], Awaitable[BrowserContext]],
server: Server,
assetdir: Path,
) -> None:
har_path = assetdir / "har-fulfill.har"
context1 = await context_factory()
page1 = await context1.new_page()
# The har file contains requests for another domain, so the router
# is expected to abort all requests.
await context1.route_from_har(har_path, not_found="abort")
with pytest.raises(Error) as exc_info:
await page1.goto(server.EMPTY_PAGE)
assert exc_info.value
await context1.unroute_all(behavior="wait")
response = must(await page1.goto(server.EMPTY_PAGE))
assert must(response).ok
async def test_should_update_har_zip_for_page(
browser: Browser, server: Server, tmp_path: Path
) -> None:
har_path = tmp_path / "har.zip"
context = await browser.new_context()
page_1 = await context.new_page()
await page_1.route_from_har(har_path, update=True)
await page_1.goto(server.PREFIX + "/one-style.html")
await context.close()
assert har_path.exists()
context_2 = await browser.new_context()
page_2 = await context_2.new_page()
await page_2.route_from_har(har_path, not_found="abort")
await page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in await page_2.content()
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
async def test_should_update_har_zip_for_page_with_different_options(
browser: Browser, server: Server, tmp_path: Path
) -> None:
har_path = tmp_path / "har.zip"
context1 = await browser.new_context()
page1 = await context1.new_page()
await page1.route_from_har(
har_path, update=True, update_content="embed", update_mode="full"
)
await page1.goto(server.PREFIX + "/one-style.html")
await context1.close()
context2 = await browser.new_context()
page2 = await context2.new_page()
await page2.route_from_har(har_path, not_found="abort")
await page2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in await page2.content()
await expect(page2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
await context2.close()
async def test_should_update_extracted_har_zip_for_page(
browser: Browser, server: Server, tmp_path: Path
) -> None:
har_path = tmp_path / "har.har"
context = await browser.new_context()
page_1 = await context.new_page()
await page_1.route_from_har(har_path, update=True)
await page_1.goto(server.PREFIX + "/one-style.html")
await context.close()
assert har_path.exists()
with har_path.open() as r:
content = r.read()
assert "log" in content
assert "background-color" not in r.read()
context_2 = await browser.new_context()
page_2 = await context_2.new_page()
await page_2.route_from_har(har_path, not_found="abort")
await page_2.goto(server.PREFIX + "/one-style.html")
assert "hello, world!" in await page_2.content()
await expect(page_2.locator("body")).to_have_css(
"background-color", "rgb(255, 192, 203)"
)
async def test_should_ignore_aborted_requests(
context_factory: Callable[[], Awaitable[BrowserContext]],
server: Server,
tmp_path: Path,
) -> None:
path = tmp_path / "test.har"
server.set_route("/x", lambda request: request.loseConnection())
context1 = await context_factory()
await context1.route_from_har(har=path, update=True)
page1 = await context1.new_page()
await page1.goto(server.EMPTY_PAGE)
req_promise = asyncio.create_task(server.wait_for_request("/x"))
eval_task = asyncio.create_task(
page1.evaluate(
"url => fetch(url).catch(e => 'cancelled')", server.PREFIX + "/x"
)
)
await req_promise
req = await eval_task
assert req == "cancelled"
await context1.close()
server.reset()
def _handle_route(req: TestServerRequest) -> None:
req.setHeader("Content-Type", "text/plain")
req.write(b"test")
req.finish()
server.set_route("/x", _handle_route)
context2 = await context_factory()
await context2.route_from_har(path)
page2 = await context2.new_page()
await page2.goto(server.EMPTY_PAGE)
eval_task = asyncio.create_task(
page2.evaluate(
"url => fetch(url).catch(e => 'cancelled')", server.PREFIX + "/x"
)
)
async def _timeout() -> str:
await asyncio.sleep(1)
return "timeout"
done, _ = await asyncio.wait(
[eval_task, asyncio.create_task(_timeout())],
return_when=asyncio.FIRST_COMPLETED,
)
assert next(iter(done)).result() == "timeout"
eval_task.cancel()
|