From 4e1113c28a5a533e9b6229397b96c8ae41d408a7 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 18 Jan 2022 18:18:44 +0100 Subject: [PATCH] chore: provide driver with the host-lang version (#1107) --- playwright/__main__.py | 11 ++++------- playwright/_impl/_driver.py | 12 ++++++++++++ playwright/_impl/_transport.py | 3 ++- tests/async/test_fetch_global.py | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/playwright/__main__.py b/playwright/__main__.py index aabe69c8c..e012cc449 100644 --- a/playwright/__main__.py +++ b/playwright/__main__.py @@ -12,20 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import subprocess import sys -from playwright._impl._driver import compute_driver_executable -from playwright._repo_version import version +from playwright._impl._driver import compute_driver_executable, get_driver_env def main() -> None: driver_executable = compute_driver_executable() - env = os.environ.copy() - env["PW_CLI_TARGET_LANG"] = "python" - env["PW_CLI_DISPLAY_VERSION"] = version - completed_process = subprocess.run([str(driver_executable), *sys.argv[1:]], env=env) + completed_process = subprocess.run( + [str(driver_executable), *sys.argv[1:]], env=get_driver_env() + ) sys.exit(completed_process.returncode) diff --git a/playwright/_impl/_driver.py b/playwright/_impl/_driver.py index 1b329b53c..bd41c2513 100644 --- a/playwright/_impl/_driver.py +++ b/playwright/_impl/_driver.py @@ -14,10 +14,12 @@ import asyncio import inspect +import os import sys from pathlib import Path import playwright +from playwright._repo_version import version def compute_driver_executable() -> Path: @@ -42,3 +44,13 @@ def compute_driver_executable() -> Path: # uvloop does not support child watcher # see https://github.com/microsoft/playwright-python/issues/582 pass + + +def get_driver_env() -> dict: + env = os.environ.copy() + env["PW_CLI_TARGET_LANG"] = "python" + env[ + "PW_CLI_TARGET_LANG_VERSION" + ] = f"{sys.version_info.major}.{sys.version_info.minor}" + env["PW_CLI_DISPLAY_VERSION"] = version + return env diff --git a/playwright/_impl/_transport.py b/playwright/_impl/_transport.py index beae9569b..9c26e4f06 100644 --- a/playwright/_impl/_transport.py +++ b/playwright/_impl/_transport.py @@ -28,6 +28,7 @@ from websockets.client import connect as websocket_connect from playwright._impl._api_types import Error +from playwright._impl._driver import get_driver_env from playwright._impl._helper import ParsedMessagePayload @@ -116,7 +117,7 @@ async def connect(self) -> None: try: # For pyinstaller - env = os.environ.copy() + env = get_driver_env() if getattr(sys, "frozen", False): env.setdefault("PLAYWRIGHT_BROWSERS_PATH", "0") diff --git a/tests/async/test_fetch_global.py b/tests/async/test_fetch_global.py index 84c481f17..98af083ef 100644 --- a/tests/async/test_fetch_global.py +++ b/tests/async/test_fetch_global.py @@ -14,6 +14,7 @@ import asyncio import json +import sys from pathlib import Path from typing import Any @@ -263,3 +264,16 @@ async def test_should_accept_already_serialized_data_as_bytes_when_content_type_ body = req.post_body assert body == stringified_value await request.dispose() + + +async def test_should_contain_default_user_agent( + playwright: Playwright, server: Server +): + request = await playwright.request.new_context() + [request, _] = await asyncio.gather( + server.wait_for_request("/empty.html"), + request.get(server.EMPTY_PAGE), + ) + user_agent = request.getHeader("user-agent") + assert "python" in user_agent + assert f"{sys.version_info.major}.{sys.version_info.minor}" in user_agent