How to use test_response_server_addr 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_network.py

Source: test_network.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 pytest
16
17from playwright.sync_api import Browser, Page, Playwright, Route
18from tests.server import Server
19
20
21def test_response_server_addr(page: Page, server: Server) -> None:
22    response = page.goto(server.EMPTY_PAGE)
23    assert response
24    server_addr = response.server_addr()
25    assert server_addr
26    assert server_addr["port"] == server.PORT
27    assert server_addr["ipAddress"] in ["127.0.0.1", "::1"]
28
29
30def test_response_security_details(
31    browser: Browser,
32    https_server: Server,
33    browser_name: str,
34    is_win: bool,
35    is_linux: bool,
36) -> None:
37    if (browser_name == "webkit" and is_linux) or (browser_name == "webkit" and is_win):
38        pytest.skip("https://github.com/microsoft/playwright/issues/6759")
39    page = browser.new_page(ignore_https_errors=True)
40    response = page.goto(https_server.EMPTY_PAGE)
41    assert response
42    response.finished()
43    security_details = response.security_details()
44    assert security_details
45    if browser_name == "webkit" and is_win:
46        assert security_details == {
47            "subjectName": "puppeteer-tests",
48            "validFrom": 1550084863,
49            "validTo": -1,
50        }
51    elif browser_name == "webkit":
52        assert security_details == {
53            "protocol": "TLS 1.3",
54            "subjectName": "puppeteer-tests",
55            "validFrom": 1550084863,
56            "validTo": 33086084863,
57        }
58    else:
59        assert security_details == {
60            "issuer": "puppeteer-tests",
61            "protocol": "TLS 1.3",
62            "subjectName": "puppeteer-tests",
63            "validFrom": 1550084863,
64            "validTo": 33086084863,
65        }
66    page.close()
67
68
69def test_response_security_details_none_without_https(
70    page: Page, server: Server
71) -> None:
72    response = page.goto(server.EMPTY_PAGE)
73    assert response
74    security_details = response.security_details()
75    assert security_details is None
76
77
78def test_should_fulfill_with_global_fetch_result(
79    page: Page, playwright: Playwright, server: Server
80) -> None:
81    def handle_request(route: Route) -> None:
82        request = playwright.request.new_context()
83        response = request.get(server.PREFIX + "/simple.json")
84        route.fulfill(response=response)
85        request.dispose()
86
87    page.route("**/*", handle_request)
88
89    response = page.goto(server.EMPTY_PAGE)
90    assert response
91    assert response.status == 200
92    assert response.json() == {"foo": "bar"}
93
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)