microsoft / playwright-python

Python version of the Playwright testing and automation library.
https://playwright.dev/python/
Apache License 2.0
11.41k stars 867 forks source link

[Question] How to run playwright server with headful browser? #1705

Closed vyahello closed 1 year ago

vyahello commented 1 year ago

Your question

Hey, I'm trying to launch playwright remote server in docker container with headful chromium browser.

By default, chromium browser is launched with headless: true option and according to docs it is managed in launch browser method. In my case, i'm connecting to the playwright server via websocket endpoint using connect method and as per docs there is no headless option to pass.

Here is my setup:

  1. Start playwright server via playwright run-server CLI command in docker container

    docker run --rm -it -p 3000:3000 mcr.microsoft.com/playwright/python:v1.27.1 playwright run-server --port=3000
    Listening on ws://127.0.0.1:3000
  2. Connect to the playwright server via websocket endpoint

    import playwright.sync_api
    
    with playwright.sync_api.sync_playwright() as playwright_session:
        browser = playwright_session.chromium.connect("ws://127.0.0.1:3000")
  3. I can see chromium is started in docker container with --headless flag

    docker exec -it <container-id> /bin/bash
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.2  19632 14284 ?        Ss   19:49   0:00 /usr/bin/python /usr/local/bin/playwright run-server --port=3000
    root         6  0.0  0.0   2060   468 ?        S    19:49   0:00 /bin/sh /usr/local/lib/python3.8/dist-packages/playwright/driver/playwright.sh run-server --port=3000
    root         9  0.2  1.0 706020 52360 ?        Sl   19:49   0:00 /usr/local/lib/python3.8/dist-packages/playwright/driver/node /usr/local/lib/python3.8/dist-packages/playwright/driver/package/lib/cli/cli.js run-server --port=3000
    root       123  0.0  1.0 33826704 51324 ?      S    19:50   0:00 /ms-playwright/chromium-1028/chrome-linux/chrome --type=zygote --no-zygote-sandbox --no-sandbox --headless
    root       124  0.0  0.9 33826692 50004 ?      S    19:50   0:00 /ms-playwright/chromium-1028/chrome-linux/chrome --type=zygote --no-sandbox --headless
    root       136  0.1  1.0 33935900 53332 ?      Sl   19:50   0:00 /ms-playwright/chromium-1028/chrome-linux/chrome --type=gpu-process --no-sandbox --disable-dev-shm-usage --disable-breakpad --headless --ozone-platform=headless
    root       174  4.0  1.5 1184451124 78792 ?    Rl   20:23   0:00 /ms-playwright/chromium-1028/chrome-linux/chrome --type=renderer --headless --first-renderer-process --no-sandbox --disable-dev-shm-usage --disable-back-forward-cache --disable-bac
    root       180  3.0  1.5 1184459320 79728 ?    Sl   20:23   0:00 /ms-playwright/chromium-1028/chrome-linux/chrome --type=renderer --headless --no-sandbox --disable-dev-shm-usage --disable-back-forward-cache --disable-background-timer-throttling

I'm trying to figure out how to force playwright server to start chromium browser session without --headless flag. Can we somehow manage browser args (like --start-maximized etc.) while/after connecting to the playwright server via websocket?

I've found global browser configuration, but not sure if we can use it with playwright run-server CLI command. Also i've checked running headed section and xvfb-run command. As a result, tried to use xvfb-run playwright run-server command with no luck, the websocket endpoint ws://127.0.0.1:3000 is unreachable.

@aslushnikov @yury-s @pavelfeldman @mxschmitt Is there a way how we can start headed browser using playwright run-server CLI command?

mxschmitt commented 1 year ago

The run-server CLI command is not a command which should be publicly used, its hidden and only used internally. We recommend to use Node.js to launch a server, see here. This gives you also the option to pass extra CLI arguments and make it headful.

Closing as per above.

dezoito commented 11 months ago

The run-server CLI command is not a command which should be publicly used, its hidden and only used internally. We recommend to use Node.js to launch a server, see here.

@mxschmitt , Could you give me an example of how to launch the server from a docker run command?

Thank you very much!

mxschmitt commented 11 months ago

We have some experimental support now see here: https://github.com/microsoft/playwright/issues/26482