How to use test_goto_should_fail_when_server_returns_204 method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_navigation.py

Source: test_navigation.py Github

copy
1# Copyright (c) Microsoft Corporation.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import asyncio
16import re
17import sys
18from typing import Any
19
20import pytest
21
22from playwright.async_api import Error, Request, TimeoutError
23
24
25async def test_goto_should_work(page, server):
26    await page.goto(server.EMPTY_PAGE)
27    assert page.url == server.EMPTY_PAGE
28
29
30async def test_goto_should_work_with_file_URL(page, server, assetdir):
31    fileurl = (assetdir / "frames" / "two-frames.html").as_uri()
32    await page.goto(fileurl)
33    assert page.url.lower() == fileurl.lower()
34    assert len(page.frames) == 3
35
36
37async def test_goto_should_use_http_for_no_protocol(page, server):
38    await page.goto(server.EMPTY_PAGE[7:])
39    assert page.url == server.EMPTY_PAGE
40
41
42async def test_goto_should_work_cross_process(page, server):
43    await page.goto(server.EMPTY_PAGE)
44    assert page.url == server.EMPTY_PAGE
45
46    url = server.CROSS_PROCESS_PREFIX + "/empty.html"
47    request_frames = []
48
49    def on_request(r: Request) -> None:
50        if r.url == url:
51            request_frames.append(r.frame)
52
53    page.on("request", on_request)
54
55    response = await page.goto(url)
56    assert page.url == url
57    assert response.frame == page.main_frame
58    assert request_frames[0] == page.main_frame
59    assert response.url == url
60
61
62async def test_goto_should_capture_iframe_navigation_request(page, server):
63    await page.goto(server.EMPTY_PAGE)
64    assert page.url == server.EMPTY_PAGE
65
66    request_frames = []
67
68    def on_request(r: Request) -> None:
69        if r.url == server.PREFIX + "/frames/frame.html":
70            request_frames.append(r.frame)
71
72    page.on("request", on_request)
73
74    response = await page.goto(server.PREFIX + "/frames/one-frame.html")
75    assert page.url == server.PREFIX + "/frames/one-frame.html"
76    assert response.frame == page.main_frame
77    assert response.url == server.PREFIX + "/frames/one-frame.html"
78
79    assert len(page.frames) == 2
80    assert request_frames[0] == page.frames[1]
81
82
83async def test_goto_should_capture_cross_process_iframe_navigation_request(
84    page, server
85):
86    await page.goto(server.EMPTY_PAGE)
87    assert page.url == server.EMPTY_PAGE
88
89    request_frames = []
90
91    def on_request(r: Request) -> None:
92        if r.url == server.CROSS_PROCESS_PREFIX + "/frames/frame.html":
93            request_frames.append(r.frame)
94
95    page.on("request", on_request)
96
97    response = await page.goto(server.CROSS_PROCESS_PREFIX + "/frames/one-frame.html")
98    assert page.url == server.CROSS_PROCESS_PREFIX + "/frames/one-frame.html"
99    assert response.frame == page.main_frame
100    assert response.url == server.CROSS_PROCESS_PREFIX + "/frames/one-frame.html"
101
102    assert len(page.frames) == 2
103    assert request_frames[0] == page.frames[1]
104
105
106async def test_goto_should_work_with_anchor_navigation(page, server):
107    await page.goto(server.EMPTY_PAGE)
108    assert page.url == server.EMPTY_PAGE
109    await page.goto(server.EMPTY_PAGE + "#foo")
110    assert page.url == server.EMPTY_PAGE + "#foo"
111    await page.goto(server.EMPTY_PAGE + "#bar")
112    assert page.url == server.EMPTY_PAGE + "#bar"
113
114
115async def test_goto_should_work_with_redirects(page, server):
116    server.set_redirect("/redirect/1.html", "/redirect/2.html")
117    server.set_redirect("/redirect/2.html", "/empty.html")
118    response = await page.goto(server.PREFIX + "/redirect/1.html")
119    assert response.status == 200
120    assert page.url == server.EMPTY_PAGE
121
122
123async def test_goto_should_navigate_to_about_blank(page, server):
124    response = await page.goto("about:blank")
125    assert response is None
126
127
128async def test_goto_should_return_response_when_page_changes_its_url_after_load(
129    page, server
130):
131    response = await page.goto(server.PREFIX + "/historyapi.html")
132    assert response.status == 200
133
134
135@pytest.mark.skip_browser("firefox")
136async def test_goto_should_work_with_subframes_return_204(page, server):
137    def handle(request):
138        request.setResponseCode(204)
139        request.finish()
140
141    server.set_route("/frames/frame.html", handle)
142
143    await page.goto(server.PREFIX + "/frames/one-frame.html")
144
145
146async def test_goto_should_fail_when_server_returns_204(
147    page, server, is_chromium, is_webkit
148):
149    # WebKit just loads an empty page.
150    def handle(request):
151        request.setResponseCode(204)
152        request.finish()
153
154    server.set_route("/empty.html", handle)
155
156    with pytest.raises(Error) as exc_info:
157        await page.goto(server.EMPTY_PAGE)
158    assert exc_info.value
159    if is_chromium:
160        assert "net::ERR_ABORTED" in exc_info.value.message
161    elif is_webkit:
162        assert "Aborted: 204 No Content" in exc_info.value.message
163    else:
164        assert "NS_BINDING_ABORTED" in exc_info.value.message
165
166
167async def test_goto_should_navigate_to_empty_page_with_domcontentloaded(page, server):
168    response = await page.goto(server.EMPTY_PAGE, wait_until="domcontentloaded")
169    assert response.status == 200
170
171
172async def test_goto_should_work_when_page_calls_history_api_in_beforeunload(
173    page, server
174):
175    await page.goto(server.EMPTY_PAGE)
176    await page.evaluate(
177        """() => {
178        window.addEventListener('beforeunload', () => history.replaceState(null, 'initial', window.location.href), false)
179    }"""
180    )
181
182    response = await page.goto(server.PREFIX + "/grid.html")
183    assert response.status == 200
184
185
186async def test_goto_should_fail_when_navigating_to_bad_url(
187    page, server, is_chromium, is_webkit
188):
189    with pytest.raises(Error) as exc_info:
190        await page.goto("asdfasdf")
191    if is_chromium or is_webkit:
192        assert "Cannot navigate to invalid URL" in exc_info.value.message
193    else:
194        assert "Invalid url" in exc_info.value.message
195
196
197async def test_goto_should_fail_when_navigating_to_bad_ssl(
198    page, https_server, browser_name
199):
200    with pytest.raises(Error) as exc_info:
201        await page.goto(https_server.EMPTY_PAGE)
202    expect_ssl_error(exc_info.value.message, browser_name)
203
204
205async def test_goto_should_fail_when_navigating_to_bad_ssl_after_redirects(
206    page, server, https_server, browser_name
207):
208    server.set_redirect("/redirect/1.html", "/redirect/2.html")
209    server.set_redirect("/redirect/2.html", "/empty.html")
210    with pytest.raises(Error) as exc_info:
211        await page.goto(https_server.PREFIX + "/redirect/1.html")
212    expect_ssl_error(exc_info.value.message, browser_name)
213
214
215async def test_goto_should_not_crash_when_navigating_to_bad_ssl_after_a_cross_origin_navigation(
216    page, server, https_server, browser_name
217):
218    await page.goto(server.CROSS_PROCESS_PREFIX + "/empty.html")
219    with pytest.raises(Error):
220        await page.goto(https_server.EMPTY_PAGE)
221
222
223async def test_goto_should_throw_if_networkidle2_is_passed_as_an_option(page, server):
224    with pytest.raises(Error) as exc_info:
225        await page.goto(server.EMPTY_PAGE, wait_until="networkidle2")
226    assert (
227        "wait_until: expected one of (load|domcontentloaded|networkidle)"
228        in exc_info.value.message
229    )
230
231
232async def test_goto_should_fail_when_main_resources_failed_to_load(
233    page, server, is_chromium, is_webkit, is_win
234):
235    with pytest.raises(Error) as exc_info:
236        await page.goto("http://localhost:44123/non-existing-url")
237    if is_chromium:
238        assert "net::ERR_CONNECTION_REFUSED" in exc_info.value.message
239    elif is_webkit and is_win:
240        assert "Couldn't connect to server" in exc_info.value.message
241    elif is_webkit:
242        assert "Could not connect" in exc_info.value.message
243    else:
244        assert "NS_ERROR_CONNECTION_REFUSED" in exc_info.value.message
245
246
247async def test_goto_should_fail_when_exceeding_maximum_navigation_timeout(page, server):
248    # Hang for request to the empty.html
249    server.set_route("/empty.html", lambda request: None)
250    with pytest.raises(Error) as exc_info:
251        await page.goto(server.PREFIX + "/empty.html", timeout=1)
252    assert "Timeout 1ms exceeded" in exc_info.value.message
253    assert server.PREFIX + "/empty.html" in exc_info.value.message
254    assert isinstance(exc_info.value, TimeoutError)
255
256
257async def test_goto_should_fail_when_exceeding_default_maximum_navigation_timeout(
258    page, server
259):
260    # Hang for request to the empty.html
261    server.set_route("/empty.html", lambda request: None)
262    page.context.set_default_navigation_timeout(2)
263    page.set_default_navigation_timeout(1)
264    with pytest.raises(Error) as exc_info:
265        await page.goto(server.PREFIX + "/empty.html")
266    assert "Timeout 1ms exceeded" in exc_info.value.message
267    assert server.PREFIX + "/empty.html" in exc_info.value.message
268    assert isinstance(exc_info.value, TimeoutError)
269
270
271async def test_goto_should_fail_when_exceeding_browser_context_navigation_timeout(
272    page, server
273):
274    # Hang for request to the empty.html
275    server.set_route("/empty.html", lambda request: None)
276    page.context.set_default_navigation_timeout(2)
277    with pytest.raises(Error) as exc_info:
278        await page.goto(server.PREFIX + "/empty.html")
279    assert "Timeout 2ms exceeded" in exc_info.value.message
280    assert server.PREFIX + "/empty.html" in exc_info.value.message
281    assert isinstance(exc_info.value, TimeoutError)
282
283
284async def test_goto_should_fail_when_exceeding_default_maximum_timeout(page, server):
285    # Hang for request to the empty.html
286    server.set_route("/empty.html", lambda request: None)
287    page.context.set_default_timeout(2)
288    page.set_default_timeout(1)
289    with pytest.raises(Error) as exc_info:
290        await page.goto(server.PREFIX + "/empty.html")
291    assert "Timeout 1ms exceeded" in exc_info.value.message
292    assert server.PREFIX + "/empty.html" in exc_info.value.message
293    assert isinstance(exc_info.value, TimeoutError)
294
295
296async def test_goto_should_fail_when_exceeding_browser_context_timeout(page, server):
297    # Hang for request to the empty.html
298    server.set_route("/empty.html", lambda request: None)
299    page.context.set_default_timeout(2)
300    with pytest.raises(Error) as exc_info:
301        await page.goto(server.PREFIX + "/empty.html")
302    assert "Timeout 2ms exceeded" in exc_info.value.message
303    assert server.PREFIX + "/empty.html" in exc_info.value.message
304    assert isinstance(exc_info.value, TimeoutError)
305
306
307async def test_goto_should_prioritize_default_navigation_timeout_over_default_timeout(
308    page, server
309):
310    # Hang for request to the empty.html
311    server.set_route("/empty.html", lambda request: None)
312    page.set_default_timeout(0)
313    page.set_default_navigation_timeout(1)
314    with pytest.raises(Error) as exc_info:
315        await page.goto(server.PREFIX + "/empty.html")
316    assert "Timeout 1ms exceeded" in exc_info.value.message
317    assert server.PREFIX + "/empty.html" in exc_info.value.message
318    assert isinstance(exc_info.value, TimeoutError)
319
320
321async def test_goto_should_disable_timeout_when_its_set_to_0(page, server):
322    loaded = []
323    page.once("load", lambda: loaded.append(True))
324    await page.goto(server.PREFIX + "/grid.html", timeout=0, wait_until="load")
325    assert loaded == [True]
326
327
328async def test_goto_should_work_when_navigating_to_valid_url(page, server):
329    response = await page.goto(server.EMPTY_PAGE)
330    assert response.ok
331
332
333async def test_goto_should_work_when_navigating_to_data_url(page, server):
334    response = await page.goto("data:text/html,hello")
335    assert response is None
336
337
338async def test_goto_should_work_when_navigating_to_404(page, server):
339    response = await page.goto(server.PREFIX + "/not-found")
340    assert response.ok is False
341    assert response.status == 404
342
343
344async def test_goto_should_return_last_response_in_redirect_chain(page, server):
345    server.set_redirect("/redirect/1.html", "/redirect/2.html")
346    server.set_redirect("/redirect/2.html", "/redirect/3.html")
347    server.set_redirect("/redirect/3.html", server.EMPTY_PAGE)
348    response = await page.goto(server.PREFIX + "/redirect/1.html")
349    assert response.ok
350    assert response.url == server.EMPTY_PAGE
351
352
353async def test_goto_should_navigate_to_data_url_and_not_fire_dataURL_requests(
354    page, server
355):
356    requests = []
357    page.on("request", lambda request: requests.append(request))
358    dataURL = "data:text/html,<div>yo</div>"
359    response = await page.goto(dataURL)
360    assert response is None
361    assert requests == []
362
363
364async def test_goto_should_navigate_to_url_with_hash_and_fire_requests_without_hash(
365    page, server
366):
367    requests = []
368    page.on("request", lambda request: requests.append(request))
369    response = await page.goto(server.EMPTY_PAGE + "#hash")
370    assert response.status == 200
371    assert response.url == server.EMPTY_PAGE
372    assert len(requests) == 1
373    assert requests[0].url == server.EMPTY_PAGE
374
375
376async def test_goto_should_work_with_self_requesting_page(page, server):
377    response = await page.goto(server.PREFIX + "/self-request.html")
378    assert response.status == 200
379    assert "self-request.html" in response.url
380
381
382async def test_goto_should_fail_when_navigating_and_show_the_url_at_the_error_message(
383    page, server, https_server
384):
385    url = https_server.PREFIX + "/redirect/1.html"
386    with pytest.raises(Error) as exc_info:
387        await page.goto(url)
388    assert url in exc_info.value.message
389
390
391async def test_goto_should_be_able_to_navigate_to_a_page_controlled_by_service_worker(
392    page, server
393):
394    await page.goto(server.PREFIX + "/serviceworkers/fetch/sw.html")
395    await page.evaluate("window.activationPromise")
396    await page.goto(server.PREFIX + "/serviceworkers/fetch/sw.html")
397
398
399async def test_goto_should_send_referer(page, server):
400    [request1, request2, _] = await asyncio.gather(
401        server.wait_for_request("/grid.html"),
402        server.wait_for_request("/digits/1.png"),
403        page.goto(server.PREFIX + "/grid.html", referer="http://google.com/"),
404    )
405    assert request1.getHeader("referer") == "http://google.com/"
406    # Make sure subresources do not inherit referer.
407    assert request2.getHeader("referer") == server.PREFIX + "/grid.html"
408    assert page.url == server.PREFIX + "/grid.html"
409
410
411async def test_goto_should_reject_referer_option_when_set_extra_http_headers_provides_referer(
412    page, server
413):
414    await page.set_extra_http_headers({"referer": "http://microsoft.com/"})
415    with pytest.raises(Error) as exc_info:
416        await page.goto(server.PREFIX + "/grid.html", referer="http://google.com/")
417    assert (
418        '"referer" is already specified as extra HTTP header' in exc_info.value.message
419    )
420    assert server.PREFIX + "/grid.html" in exc_info.value.message
421
422
423async def test_network_idle_should_navigate_to_empty_page_with_networkidle(
424    page, server
425):
426    response = await page.goto(server.EMPTY_PAGE, wait_until="networkidle")
427    assert response.status == 200
428
429
430async def test_wait_for_nav_should_work(page, server):
431    await page.goto(server.EMPTY_PAGE)
432    async with page.expect_navigation() as response_info:
433        await page.evaluate(
434            "url => window.location.href = url", server.PREFIX + "/grid.html"
435        )
436    response = await response_info.value
437    assert response.ok
438    assert "grid.html" in response.url
439
440
441async def test_wait_for_nav_should_respect_timeout(page, server):
442    with pytest.raises(Error) as exc_info:
443        async with page.expect_navigation(url="**/frame.html", timeout=5000):
444            await page.goto(server.EMPTY_PAGE)
445    assert "Timeout 5000ms exceeded" in exc_info.value.message
446
447
448async def test_wait_for_nav_should_work_with_both_domcontentloaded_and_load(
449    page, server
450):
451    async with page.expect_navigation(
452        wait_until="domcontentloaded"
453    ), page.expect_navigation(wait_until="load"):
454        await page.goto(server.PREFIX + "/one-style.html")
455
456
457async def test_wait_for_nav_should_work_with_clicking_on_anchor_links(page, server):
458    await page.goto(server.EMPTY_PAGE)
459    await page.set_content('<a href="#foobar">foobar</a>')
460    async with page.expect_navigation() as response_info:
461        await page.click("a"),
462    response = await response_info.value
463    assert response is None
464    assert page.url == server.EMPTY_PAGE + "#foobar"
465
466
467async def test_wait_for_nav_should_work_with_clicking_on_links_which_do_not_commit_navigation(
468    page, server, https_server, browser_name
469):
470    await page.goto(server.EMPTY_PAGE)
471    await page.set_content(f"<a href='{https_server.EMPTY_PAGE}'>foobar</a>")
472    with pytest.raises(Error) as exc_info:
473        async with page.expect_navigation():
474            await page.click("a"),
475    expect_ssl_error(exc_info.value.message, browser_name)
476
477
478async def test_wait_for_nav_should_work_with_history_push_state(page, server):
479    await page.goto(server.EMPTY_PAGE)
480    await page.set_content(
481        """
482        <a onclick='javascript:pushState()'>SPA</a>
483        <script>
484            function pushState() { history.pushState({}, '', 'wow.html') }
485        </script>
486    """
487    )
488    async with page.expect_navigation() as response_info:
489        await page.click("a"),
490    response = await response_info.value
491    assert response is None
492    assert page.url == server.PREFIX + "/wow.html"
493
494
495async def test_wait_for_nav_should_work_with_history_replace_state(page, server):
496    await page.goto(server.EMPTY_PAGE)
497    await page.set_content(
498        """
499        <a onclick='javascript:replaceState()'>SPA</a>
500        <script>
501            function replaceState() { history.replaceState({}, '', '/replaced.html') }
502        </script>
503    """
504    )
505    async with page.expect_navigation() as response_info:
506        await page.click("a"),
507    response = await response_info.value
508    assert response is None
509    assert page.url == server.PREFIX + "/replaced.html"
510
511
512async def test_wait_for_nav_should_work_with_dom_history_back_forward(page, server):
513    await page.goto(server.EMPTY_PAGE)
514    await page.set_content(
515        """
516      <a id=back onclick='javascript:go_back()'>back</a>
517      <a id=forward onclick='javascript:go_forward()'>forward</a>
518      <script>
519        function go_back() { history.back(); }
520        function go_forward() { history.forward(); }
521        history.pushState({}, '', '/first.html')
522        history.pushState({}, '', '/second.html')
523      </script>
524    """
525    )
526    assert page.url == server.PREFIX + "/second.html"
527    async with page.expect_navigation() as back_response_info:
528        await page.click("a#back"),
529    back_response = await back_response_info.value
530    assert back_response is None
531    assert page.url == server.PREFIX + "/first.html"
532    async with page.expect_navigation() as forward_response_info:
533        await page.click("a#forward"),
534    forward_response = await forward_response_info.value
535    assert forward_response is None
536    assert page.url == server.PREFIX + "/second.html"
537
538
539@pytest.mark.skip_browser("firefox")
540async def test_wait_for_nav_should_work_when_subframe_issues_window_stop(page, server):
541    server.set_route("/frames/style.css", lambda _: None)
542    navigation_promise = asyncio.create_task(
543        page.goto(server.PREFIX + "/frames/one-frame.html")
544    )
545    await asyncio.sleep(0)
546    async with page.expect_event("frameattached") as frame_info:
547        pass
548    frame = await frame_info.value
549
550    async with page.expect_event("framenavigated", lambda f: f == frame):
551        pass
552    await asyncio.gather(frame.evaluate("() => window.stop()"), navigation_promise)
553
554
555async def test_wait_for_nav_should_work_with_url_match(page, server):
556    responses = [None, None, None]
557
558    async def wait_for_nav(url: Any, index: int) -> None:
559        async with page.expect_navigation(url=url) as response_info:
560            pass
561        responses[index] = await response_info.value
562
563    response0_promise = asyncio.create_task(
564        wait_for_nav(re.compile(r"one-style\.html"), 0)
565    )
566    response1_promise = asyncio.create_task(
567        wait_for_nav(re.compile(r"\/frame.html"), 1)
568    )
569    response2_promise = asyncio.create_task(
570        wait_for_nav(lambda url: "foo=bar" in url, 2)
571    )
572    assert responses == [None, None, None]
573    await page.goto(server.EMPTY_PAGE)
574    assert responses == [None, None, None]
575    await page.goto(server.PREFIX + "/frame.html")
576    assert responses[0] is None
577    await response1_promise
578    assert responses[1] is not None
579    assert responses[2] is None
580    await page.goto(server.PREFIX + "/one-style.html")
581    await response0_promise
582    assert responses[0] is not None
583    assert responses[1] is not None
584    assert responses[2] is None
585    await page.goto(server.PREFIX + "/frame.html?foo=bar")
586    await response2_promise
587    assert responses[0] is not None
588    assert responses[1] is not None
589    assert responses[2] is not None
590    await page.goto(server.PREFIX + "/empty.html")
591    assert responses[0].url == server.PREFIX + "/one-style.html"
592    assert responses[1].url == server.PREFIX + "/frame.html"
593    assert responses[2].url == server.PREFIX + "/frame.html?foo=bar"
594
595
596async def test_wait_for_nav_should_work_with_url_match_for_same_document_navigations(
597    page, server
598):
599    await page.goto(server.EMPTY_PAGE)
600    async with page.expect_navigation(url=re.compile(r"third\.html")) as response_info:
601        assert not response_info.is_done()
602        await page.evaluate("history.pushState({}, '', '/first.html')")
603        assert not response_info.is_done()
604        await page.evaluate("history.pushState({}, '', '/second.html')")
605        assert not response_info.is_done()
606        await page.evaluate("history.pushState({}, '', '/third.html')")
607    assert response_info.is_done()
608
609
610async def test_wait_for_nav_should_work_for_cross_process_navigations(page, server):
611    await page.goto(server.EMPTY_PAGE)
612    url = server.CROSS_PROCESS_PREFIX + "/empty.html"
613    async with page.expect_navigation(wait_until="domcontentloaded") as response_info:
614        await page.goto(url)
615    response = await response_info.value
616    assert response.url == url
617    assert page.url == url
618    assert await page.evaluate("document.location.href") == url
619
620
621async def test_expect_navigation_should_work_for_cross_process_navigations(
622    page, server
623):
624    await page.goto(server.EMPTY_PAGE)
625    url = server.CROSS_PROCESS_PREFIX + "/empty.html"
626    async with page.expect_navigation(wait_until="domcontentloaded") as response_info:
627        goto_task = asyncio.create_task(page.goto(url))
628    response = await response_info.value
629    assert response.url == url
630    assert page.url == url
631    assert await page.evaluate("document.location.href") == url
632    await goto_task
633
634
635async def test_wait_for_load_state_should_respect_timeout(page, server):
636    requests = []
637
638    def handler(request: Any):
639        requests.append(request)
640
641    server.set_route("/one-style.css", handler)
642
643    await page.goto(server.PREFIX + "/one-style.html", wait_until="domcontentloaded")
644    with pytest.raises(Error) as exc_info:
645        await page.wait_for_load_state("load", timeout=1)
646    assert "Timeout 1ms exceeded." in exc_info.value.message
647
648
649async def test_wait_for_load_state_should_resolve_immediately_if_loaded(page, server):
650    await page.goto(server.PREFIX + "/one-style.html")
651    await page.wait_for_load_state()
652
653
654async def test_wait_for_load_state_should_throw_for_bad_state(page, server):
655    await page.goto(server.PREFIX + "/one-style.html")
656    with pytest.raises(Error) as exc_info:
657        await page.wait_for_load_state("bad")
658    assert (
659        "state: expected one of (load|domcontentloaded|networkidle)"
660        in exc_info.value.message
661    )
662
663
664async def test_wait_for_load_state_should_resolve_immediately_if_load_state_matches(
665    page, server
666):
667    await page.goto(server.EMPTY_PAGE)
668
669    requests = []
670
671    def handler(request: Any):
672        requests.append(request)
673
674    server.set_route("/one-style.css", handler)
675
676    await page.goto(server.PREFIX + "/one-style.html", wait_until="domcontentloaded")
677    await page.wait_for_load_state("domcontentloaded")
678
679
680async def test_wait_for_load_state_should_work_with_pages_that_have_loaded_before_being_connected_to(
681    page, context, server
682):
683    await page.goto(server.EMPTY_PAGE)
684    async with page.expect_popup() as popup_info:
685        await page.evaluate("window._popup = window.open(document.location.href)")
686
687    # The url is about:blank in FF.
688    popup = await popup_info.value
689    assert popup.url == server.EMPTY_PAGE
690    await popup.wait_for_load_state()
691    assert popup.url == server.EMPTY_PAGE
692
693
694async def test_wait_for_load_state_should_wait_for_load_state_of_empty_url_popup(
695    browser, page, is_firefox
696):
697    ready_state = []
698    async with page.expect_popup() as popup_info:
699        ready_state.append(
700            await page.evaluate(
701                """() => {
702            popup = window.open('')
703            return popup.document.readyState
704        }"""
705            )
706        )
707
708    popup = await popup_info.value
709    await popup.wait_for_load_state()
710    assert ready_state == ["uninitialized"] if is_firefox else ["complete"]
711    assert await popup.evaluate("() => document.readyState") == ready_state[0]
712
713
714async def test_wait_for_load_state_should_wait_for_load_state_of_about_blank_popup_(
715    browser, page
716):
717    async with page.expect_popup() as popup_info:
718        await page.evaluate("window.open('about:blank') && 1")
719    popup = await popup_info.value
720    await popup.wait_for_load_state()
721    assert await popup.evaluate("document.readyState") == "complete"
722
723
724async def test_wait_for_load_state_should_wait_for_load_state_of_about_blank_popup_with_noopener(
725    browser, page
726):
727    async with page.expect_popup() as popup_info:
728        await page.evaluate("window.open('about:blank', null, 'noopener') && 1")
729
730    popup = await popup_info.value
731    await popup.wait_for_load_state()
732    assert await popup.evaluate("document.readyState") == "complete"
733
734
735async def test_wait_for_load_state_should_wait_for_load_state_of_popup_with_network_url_(
736    browser, page, server
737):
738    await page.goto(server.EMPTY_PAGE)
739    async with page.expect_popup() as popup_info:
740        await page.evaluate("url => window.open(url) && 1", server.EMPTY_PAGE)
741
742    popup = await popup_info.value
743    await popup.wait_for_load_state()
744    assert await popup.evaluate("document.readyState") == "complete"
745
746
747async def test_wait_for_load_state_should_wait_for_load_state_of_popup_with_network_url_and_noopener_(
748    browser, page, server
749):
750    await page.goto(server.EMPTY_PAGE)
751    async with page.expect_popup() as popup_info:
752        await page.evaluate(
753            "url => window.open(url, null, 'noopener') && 1", server.EMPTY_PAGE
754        )
755
756    popup = await popup_info.value
757    await popup.wait_for_load_state()
758    assert await popup.evaluate("document.readyState") == "complete"
759
760
761async def test_wait_for_load_state_should_work_with_clicking_target__blank(
762    browser, page, server
763):
764    await page.goto(server.EMPTY_PAGE)
765    await page.set_content(
766        '<a target=_blank rel="opener" href="/one-style.html">yo</a>'
767    )
768    async with page.expect_popup() as popup_info:
769        await page.click("a")
770    popup = await popup_info.value
771    await popup.wait_for_load_state()
772    assert await popup.evaluate("document.readyState") == "complete"
773
774
775async def test_wait_for_load_state_should_wait_for_load_state_of_new_page(
776    context, page, server
777):
778    async with context.expect_page() as page_info:
779        await context.new_page()
780    new_page = await page_info.value
781    await new_page.wait_for_load_state()
782    assert await new_page.evaluate("document.readyState") == "complete"
783
784
785async def test_wait_for_load_state_in_popup(context, server):
786    page = await context.new_page()
787    await page.goto(server.EMPTY_PAGE)
788    css_requests = []
789
790    def handle_request(request):
791        css_requests.append(request)
792        request.write(b"body {}")
793        request.finish()
794
795    server.set_route("/one-style.css", handle_request)
796
797    async with page.expect_popup() as popup_info:
798        await page.evaluate(
799            "url => window.popup = window.open(url)", server.PREFIX + "/one-style.html"
800        )
801
802    popup = await popup_info.value
803    await popup.wait_for_load_state()
804    assert len(css_requests)
805
806
807async def test_go_back_should_work(page, server):
808    assert await page.go_back() is None
809
810    await page.goto(server.EMPTY_PAGE)
811    await page.goto(server.PREFIX + "/grid.html")
812
813    response = await page.go_back()
814    assert response.ok
815    assert server.EMPTY_PAGE in response.url
816
817    response = await page.go_forward()
818    assert response.ok
819    assert "/grid.html" in response.url
820
821    response = await page.go_forward()
822    assert response is None
823
824
825async def test_go_back_should_work_with_history_api(page, server):
826    await page.goto(server.EMPTY_PAGE)
827    await page.evaluate(
828        """() => {
829        history.pushState({}, '', '/first.html')
830        history.pushState({}, '', '/second.html')
831    }"""
832    )
833    assert page.url == server.PREFIX + "/second.html"
834
835    await page.go_back()
836    assert page.url == server.PREFIX + "/first.html"
837    await page.go_back()
838    assert page.url == server.EMPTY_PAGE
839    await page.go_forward()
840    assert page.url == server.PREFIX + "/first.html"
841
842
843async def test_frame_goto_should_navigate_subframes(page, server):
844    await page.goto(server.PREFIX + "/frames/one-frame.html")
845    assert "/frames/one-frame.html" in page.frames[0].url
846    assert "/frames/frame.html" in page.frames[1].url
847
848    response = await page.frames[1].goto(server.EMPTY_PAGE)
849    assert response.ok
850    assert response.frame == page.frames[1]
851
852
853async def test_frame_goto_should_reject_when_frame_detaches(page, server):
854    await page.goto(server.PREFIX + "/frames/one-frame.html")
855
856    await page.route("**/empty.html", lambda route, request: None)
857    navigation_task = asyncio.create_task(page.frames[1].goto(server.EMPTY_PAGE))
858    asyncio.create_task(page.eval_on_selector("iframe", "frame => frame.remove()"))
859    with pytest.raises(Error) as exc_info:
860        await navigation_task
861    assert "frame was detached" in exc_info.value.message
862
863
864async def test_frame_goto_should_continue_after_client_redirect(page, server):
865    server.set_route("/frames/script.js", lambda _: None)
866    url = server.PREFIX + "/frames/child-redirect.html"
867
868    with pytest.raises(Error) as exc_info:
869        await page.goto(url, timeout=5000, wait_until="networkidle")
870
871    assert "Timeout 5000ms exceeded." in exc_info.value.message
872    assert (
873        f'navigating to "{url}", waiting until "networkidle"' in exc_info.value.message
874    )
875
876
877async def test_frame_wait_for_nav_should_work(page, server):
878    await page.goto(server.PREFIX + "/frames/one-frame.html")
879    frame = page.frames[1]
880    async with frame.expect_navigation() as response_info:
881        await frame.evaluate(
882            "url => window.location.href = url", server.PREFIX + "/grid.html"
883        )
884    response = await response_info.value
885    assert response.ok
886    assert "grid.html" in response.url
887    assert response.frame == frame
888    assert "/frames/one-frame.html" in page.url
889
890
891async def test_frame_wait_for_nav_should_fail_when_frame_detaches(page, server):
892    await page.goto(server.PREFIX + "/frames/one-frame.html")
893    frame = page.frames[1]
894    server.set_route("/empty.html", lambda _: None)
895    with pytest.raises(Error) as exc_info:
896        async with frame.expect_navigation():
897            await asyncio.gather(
898                frame.evaluate('window.location = "/empty.html"'),
899                page.evaluate(
900                    'setTimeout(() => document.querySelector("iframe").remove())'
901                ),
902            )
903    assert "frame was detached" in exc_info.value.message
904
905
906async def test_frame_wait_for_load_state_should_work(page, server):
907    await page.goto(server.PREFIX + "/frames/one-frame.html")
908    frame = page.frames[1]
909
910    request_future = asyncio.Future()
911    await page.route(
912        server.PREFIX + "/one-style.css",
913        lambda route, request: request_future.set_result(route),
914    )
915
916    await frame.goto(server.PREFIX + "/one-style.html", wait_until="domcontentloaded")
917    request = await request_future
918    load_task = asyncio.create_task(frame.wait_for_load_state())
919    # give the promise a chance to resolve, even though it shouldn't
920    await page.evaluate("1")
921    assert not load_task.done()
922    asyncio.create_task(request.continue_())
923    await load_task
924
925
926async def test_reload_should_work(page, server):
927    await page.goto(server.EMPTY_PAGE)
928    await page.evaluate("window._foo = 10")
929    await page.reload()
930    assert await page.evaluate("window._foo") is None
931
932
933async def test_reload_should_work_with_data_url(page, server):
934    await page.goto("data:text/html,hello")
935    assert "hello" in await page.content()
936    assert await page.reload() is None
937    assert "hello" in await page.content()
938
939
940async def test_should_work_with__blank_target(page, server):
941    def handler(request):
942        request.write(
943            f'<a href="{server.EMPTY_PAGE}" target="_blank">Click me</a>'.encode()
944        )
945        request.finish()
946
947    server.set_route("/empty.html", handler)
948
949    await page.goto(server.EMPTY_PAGE)
950    await page.click('"Click me"')
951
952
953async def test_should_work_with_cross_process__blank_target(page, server):
954    def handler(request):
955        request.write(
956            f'<a href="{server.CROSS_PROCESS_PREFIX}/empty.html" target="_blank">Click me</a>'.encode()
957        )
958        request.finish()
959
960    server.set_route("/empty.html", handler)
961
962    await page.goto(server.EMPTY_PAGE)
963    await page.click('"Click me"')
964
965
966def expect_ssl_error(error_message: str, browser_name: str) -> None:
967    if browser_name == "chromium":
968        assert "net::ERR_CERT_AUTHORITY_INVALID" in error_message
969    elif browser_name == "webkit":
970        if sys.platform == "darwin":
971            assert "The certificate for this server is invalid" in error_message
972        elif sys.platform == "win32":
973            assert "SSL connect error" in error_message
974        else:
975            assert "Unacceptable TLS certificate" in error_message
976    else:
977        assert "SSL_ERROR_UNKNOWN" in error_message
978
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)