Skip to content

Commit 4e1113c

Browse files
authored
chore: provide driver with the host-lang version (microsoft#1107)
1 parent 8c5aaf8 commit 4e1113c

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

playwright/__main__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import subprocess
1716
import sys
1817

19-
from playwright._impl._driver import compute_driver_executable
20-
from playwright._repo_version import version
18+
from playwright._impl._driver import compute_driver_executable, get_driver_env
2119

2220

2321
def main() -> None:
2422
driver_executable = compute_driver_executable()
25-
env = os.environ.copy()
26-
env["PW_CLI_TARGET_LANG"] = "python"
27-
env["PW_CLI_DISPLAY_VERSION"] = version
28-
completed_process = subprocess.run([str(driver_executable), *sys.argv[1:]], env=env)
23+
completed_process = subprocess.run(
24+
[str(driver_executable), *sys.argv[1:]], env=get_driver_env()
25+
)
2926
sys.exit(completed_process.returncode)
3027

3128

playwright/_impl/_driver.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
import asyncio
1616
import inspect
17+
import os
1718
import sys
1819
from pathlib import Path
1920

2021
import playwright
22+
from playwright._repo_version import version
2123

2224

2325
def compute_driver_executable() -> Path:
@@ -42,3 +44,13 @@ def compute_driver_executable() -> Path:
4244
# uvloop does not support child watcher
4345
# see https://github.com/microsoft/playwright-python/issues/582
4446
pass
47+
48+
49+
def get_driver_env() -> dict:
50+
env = os.environ.copy()
51+
env["PW_CLI_TARGET_LANG"] = "python"
52+
env[
53+
"PW_CLI_TARGET_LANG_VERSION"
54+
] = f"{sys.version_info.major}.{sys.version_info.minor}"
55+
env["PW_CLI_DISPLAY_VERSION"] = version
56+
return env

playwright/_impl/_transport.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from websockets.client import connect as websocket_connect
2929

3030
from playwright._impl._api_types import Error
31+
from playwright._impl._driver import get_driver_env
3132
from playwright._impl._helper import ParsedMessagePayload
3233

3334

@@ -116,7 +117,7 @@ async def connect(self) -> None:
116117

117118
try:
118119
# For pyinstaller
119-
env = os.environ.copy()
120+
env = get_driver_env()
120121
if getattr(sys, "frozen", False):
121122
env.setdefault("PLAYWRIGHT_BROWSERS_PATH", "0")
122123

tests/async/test_fetch_global.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import asyncio
1616
import json
17+
import sys
1718
from pathlib import Path
1819
from typing import Any
1920

@@ -263,3 +264,16 @@ async def test_should_accept_already_serialized_data_as_bytes_when_content_type_
263264
body = req.post_body
264265
assert body == stringified_value
265266
await request.dispose()
267+
268+
269+
async def test_should_contain_default_user_agent(
270+
playwright: Playwright, server: Server
271+
):
272+
request = await playwright.request.new_context()
273+
[request, _] = await asyncio.gather(
274+
server.wait_for_request("/empty.html"),
275+
request.get(server.EMPTY_PAGE),
276+
)
277+
user_agent = request.getHeader("user-agent")
278+
assert "python" in user_agent
279+
assert f"{sys.version_info.major}.{sys.version_info.minor}" in user_agent

0 commit comments

Comments
 (0)