microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.
https://playwright.dev/dotnet/
MIT License
2.46k stars 233 forks source link

Cannot use headful mode inside Docker #1271

Closed pablopioli closed 3 years ago

pablopioli commented 3 years ago

I have successfully made Playwright work in Docker. But a page requires to go in the headful mode.

Playwright image states that you can run in headful mode, but PlaywrightSharp cannot. On starting the browser you get:

PlaywrightSharp.TargetClosedException: Protocol error (Browser.getVersion): Browser closed.
==================== Browser output: ====================
<launching> /ms-playwright/chromium-854489/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=TranslateUI,BlinkGenPropertyTrees,ImprovedCookieControls,SameSiteByDefaultCookies,LazyFrameLoading --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --user-data-dir=/tmp/playwright_chromiumdev_profile-zfkCd9 --remote-debugging-pipe --no-sandbox --no-startup-window
<launched> pid=130
[err] [130:130:0309/181333.414336:ERROR:browser_main_loop.cc(1386)] Unable to open X display.
=========================== logs ===========================
<launching> /ms-playwright/chromium-854489/chrome-linux/chrome --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=TranslateUI,BlinkGenPropertyTrees,ImprovedCookieControls,SameSiteByDefaultCookies,LazyFrameLoading --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --user-data-dir=/tmp/playwright_chromiumdev_profile-zfkCd9 --remote-debugging-pipe --no-sandbox --no-startup-window
<launched> pid=130
[err] [130:130:0309/181333.414336:ERROR:browser_main_loop.cc(1386)] Unable to open X display.
============================================================
Note: use DEBUG=pw:api environment variable to capture Playwright logs.
   at PlaywrightSharp.Transport.Connection.SendMessageToServerAsync[T](String guid, String method, Object args, Boolean ignoreNullValues, JsonSerializerOptions serializerOptions, Boolean treatErrorPropertyAsError) in /home/runner/work/playwright-sharp/playwright-sharp/src/PlaywrightSharp/Transport/Connection.cs:line 164
   at PlaywrightSharp.BrowserType.LaunchAsync(LaunchOptions options) in /home/runner/work/playwright-sharp/playwright-sharp/src/PlaywrightSharp/BrowserType.cs:line 102
   at PlaywrightSharp.Chromium.ChromiumBrowserType.LaunchAsync(LaunchOptions options) in /home/runner/work/playwright-sharp/playwright-sharp/src/PlaywrightSharp/Chromium/ChromiumBrowserType.cs:line 18
   at MyScrapper.Browser.StartBrowser(BrowserType browserType, Boolean showBrowser)
pablopioli commented 3 years ago

So, after a lot of trial and error and learning a lot about Linux and how Playwright works I found a workaround.

The problem is in playwright.sh. The current contents are

SCRIPT_PATH="$(cd "$(dirname "$0")" ; pwd -P)"
$SCRIPT_PATH/node $SCRIPT_PATH/package/lib/cli/cli.js "$@"

xvfb is already installed in the base image of Playwright but PlaywrightSharp does not use it. To enable the restful mode I developed this workaround. I modified the playwright.sh file and inject the updated file in the image on build.

SCRIPT_PATH="$(cd "$(dirname "$0")" ; pwd -P)"
xvfb-run -a $SCRIPT_PATH/node $SCRIPT_PATH/package/lib/cli/cli.js "$@"

Obviously the image is an all or nothing now, running all workloads with X window enabled.

I suppose this can be fixed in PlaywrightSharp. I can build a pull request of it. But, I would like to hear from the maintainers how to implement it without interfering with the current architecture.

pablopioli commented 3 years ago

Options I came with:

Those are options that can impact the current design, so I take it as a design decision mainly.

avodovnik commented 3 years ago

First, the .sh file comes from upstream (https://github.com/microsoft/playwright/blob/master/utils/build/run-driver-posix.sh), so I don't think changing it will be the right approach - but I could be wrong.

That said, I remember @stuartleeks had a working approach (granted not for the .NET port, but it should still be fine). Stuart, if this resonates, can you chime in with what you did?

stuartleeks commented 3 years ago

I'm running on Windows with playwright (JS) in a VS Code dev container. I run an X Server on my windows host (such as vcxsrv) and in my dev container I set the DISPLAY to the IP address of the host (I'm running my containers in the WSL backend for Docker Desktop). Without this, I saw similar Unable to open X display errors as in your original post.

With this setup, the code runs in the dev container and the browser UI is shown via the X server.

Don't know whether that helps at all with your scenario?

pavelfeldman commented 3 years ago

One needs X server for headed mode, xvfb-run would be an option.