microsoft / playwright-python

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

[BUG] Can't install playwright inside Dev Container #1842

Closed pamelafox closed 1 year ago

pamelafox commented 1 year ago

Context:

Describe the bug

When I run playwright install in the Dev Container, I get an error:

Downloading Webkit 16.4 (playwright build v1811) from https://playwright.azureedge.net/builds/webkit/1811/webkit-debian-11.zip
92.9 Mb [====================] 100% 0.0s
Failed to install browsers
Error: Failed to download Webkit 16.4 (playwright build v1811), caused by
Error: ENOENT: no such file or directory, mkdir '/home/vscode/.cache/ms-playwright/webkit-1811/minibrowser-wpe/bin/'

This is the Dockerfile that I'm using for the Dev Container:

FROM --platform=amd64 mcr.microsoft.com/devcontainers/python:3.11
RUN export DEBIAN_FRONTEND=noninteractive \
     && apt-get update && apt-get install -y xdg-utils \
     && apt-get clean -y && rm -rf /var/lib/apt/lists/*
RUN curl -fsSL https://aka.ms/install-azd.sh | bash
mxschmitt commented 1 year ago

I tried to reproduce it, but was not able to. See here my attempt: https://github.com/mxschmitt/python-vscode-dev-containers-demo

What I found is that you pass --platform=amd64 which will emulate amd64 on arm64. We strongly discourage this and this will also break Chromium support, since it does not like to get emulated via Docker. Will be also faster if you remove it and it automatically picks the right architecture.

pamelafox commented 1 year ago

Thanks, I was able to get farther with yours once I added the missing apt-get deps, but then, as you say, I ran into an error when it actually tried to run the test in Chromium (something about the page closing).

Your example does work when I remove the emulation.

Unfortunately, I have to keep the emulation for now in order to support the Azure Developer CLI (https://github.com/Azure/azure-dev/issues/1314). For now, I will run the tests outside of the Dev Container and get them running on CI as well.

Feel free to close this issue, thanks for the response!

mxschmitt commented 1 year ago

Not sure if there is a difference between Azure Developer CLI and Azure CLI, but we at Playwright are using Azure CLI inside linux-arm64 since a while and it works like charm. We install it like that:

RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash

Closing then for now.

pamelafox commented 1 year ago

Thanks to your help, I'm successfully running Playwright inside dev containers. However, I'd now like to get headed mode working. I added xvfb to my container and ran:

xvfb-run python3 -m pytest --headed

The tests run and pass, but no browser pops up, like it does outside of a dev container. Is there a way to get the browser to pop up?

This is my repo:

https://github.com/pamelafox/flask-db-quiz-example

mxschmitt commented 1 year ago

There is no way of accessing the desktop environment of your GitHub Codespace without running a VNC server and accessing it by that. This will work but the user experience will be slow, since there is a lot of delay etc. and the machines are not made for that. We usually recommend to develop locally.

See e.g. https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/desktop-lite.md

pamelafox commented 1 year ago

I'm trying to run in local VS Code Dev Container, not Codespaces - would that also require VNC? I always use a Dev Container for my web apps as it sets up the app database as well, very convenient.

mxschmitt commented 1 year ago

I'm trying to run in local VS Code Dev Container, not Codespaces - would that also require VNC? I always use a Dev Container for my web apps as it sets up the app database as well, very convenient.

This is about the same limitations in the end. Yes, developing web apps and writing playwright tests are significant different. One involves a GUI and one does not.

You can expose your web app port tho to your host system, what you most likely already doing, and run the pwtests against them from your host.

pamelafox commented 1 year ago

Okay, I was hoping to be able to do it all inside a VS Code Dev Container for a VS Code live stream today, but sounds like headed won't be possible. I'll start off by showing playwright headed in a non-DevContainer environment, then I'll show headless tests running on a more complex web app in a Dev Container environment.

I see what you mean that I could run my tests (and codegen) outside the Dev Container by making sure the tests navigate to the page at the exposed host port instead of the randomly generated internal port that my tests currently use, so will keep that in mind if anyone asks.

Thanks!