Running playwright tests

Hello,

I am unable to get playwright to run tests.

I am able to get the playwright cli command to run using the following commands:

export PLAYWRIGHT_BROWSERS_PATH=$(nix build --print-out-paths nixpkgs#playwright.browsers)
nix shell nixpkgs#playwright
playwright open nixos.org

However the playwright cli is missing the test command

Using playwright through npm/yarn also has problems:

yarn playwright install-deps expects apt, so it does not work

yarn playwright install
yarn playright test

produces errors like:

browserType.launch: 
╔══════════════════════════════════════════════════════╗
β•‘ Host system is missing dependencies to run browsers. 
β•‘ Missing libraries:                                   
β•‘     libgobject-2.0.so.0                              
β•‘     libglib-2.0.so.0                                 
export PLAYWRIGHT_BROWSERS_PATH=$(nix build --print-out-paths nixpkgs#playwright
.browsers)
yarn playwright test

produces errors like:

Executable doesn’t exist at /nix/store/21isr8iwrdkhdhdyjw4jfxmwv0cn5zja-playwright-browsers/chromium-1045/chrome-linux/chrome

Short of a fix, is there a common workaround for problems like this? I often run into these kinds of problems when using npm/yarn and beating my head against nix is hurting my productivity.

Any help would be much appreciated.

Sources:

2 Likes

I’m facing same issues and after checking source code of the following packages : python39Packages.playwright , playwright and python310Packages.playwright I found all of them pointing into same source code nixpkgs/default.nix at a3d745e701c337e65ef467d5a9400d9336a303a1 Β· NixOS/nixpkgs Β· GitHub and it’s related to python only.

Also it still show the missed libs after including them:

  buildInputs = with nixpkgs; [
   ###### Playwright deps
    # libgobject-2.0.so.0
    # libglib-2.0.so.0
    # libgio-2.0.so.0
    glib
    # libnss3.so
    # libnssutil3.so
    # libsmime3.so
    nss
    # libnspr4.so
    nspr
    # libatk-1.0.so.0
    # libatk-bridge-2.0.so.0
    # libatspi.so.0
    at-spi2-core
    # libcups.so.2
    cups
    # libdrm.so.2
    libdrm
    # libdbus-1.so.3
    dbus
    # libX11.so.6
    xorg.libX11
    # libXcomposite.so.1
    xorg.libXcomposite
    # libXdamage.so.1
    xorg.libXdamage
    # libXext.so.6
    xorg.libXext
    # libXfixes.so.3
    xorg.libXfixes
    # libXrandr.so.2
    xorg.libXrandr
    # libgbm.so.1
    mesa
    # libexpat.so.1
    expat
    # libxcb.so.1
    xorg.libxcb
    # libxkbcommon.so.0
    libxkbcommon
    # libpango-1.0.so.0
    pango
    # libcairo.so.2
    cairo
    # libasound.so.2
    alsa-lib
    ######
   ....

It would be incredibly cool to be able to run playwright tests as part of nix flake check. The testScript attribute in pkgs.nixosTest is python code and playwright has python driver so it should be somehow possible no :slight_smile: ?

I am new to playwright so I might miss something but here is what I found Package request: playwright test Β· Issue #217693 Β· NixOS/nixpkgs Β· GitHub , ie., for the tests I think you must use the typescript version of playwright ?

At work we’re using python with playwright, though not in NixOS tests (yet). No problems.

but there is no playwright test command in hte python package:

➜ result/bin/playwright 
Usage: playwright [options] [command]

Options:
  -V, --version                          output the version number
  -h, --help                             display help for command

Commands:
  open [options] [url]                   open page in browser specified via -b, --browser
  codegen [options] [url]                open page and generate code for user actions
  install [options] [browser...]         ensure browsers necessary for this version of Playwright are installed
  install-deps [options] [browser...]    install dependencies necessary to run browsers (will ask for sudo permissions)
  cr [options] [url]                     open page in Chromium
  ff [options] [url]                     open page in Firefox
  wk [options] [url]                     open page in WebKit
  screenshot [options] <url> <filename>  capture a page screenshot
  pdf [options] <url> <filename>         save page as pdf
  show-trace [options] [trace...]        show trace viewer
  help [command]                         display help for command

what do you use it for ? do you write tests in python ? how do you launch them then ?

playwright is used as a library in python with pytest and will spawn a browser based on its python API.

We have a shell.nix that looks like this:

let
  python' = python3.withPackages (p: with p; [
    lxml
    mixins
    playwright
    pytest
    pytest-playwright
    pytest-pytestrail
  ]);
in
mkShell {
  name = "pythonEnv";

  packages = [
    python'
  ];

  shellHook = ''
    export PLAYWRIGHT_BROWSERS_PATH=${playwright.browsers}
  '';
}

From the source directory you enter the shell and run pytest with some arguments and the python code runs.

I currently use this shell.nix for JS:

{ pkgs ? import <nixpkgs> {} }:
  pkgs.mkShell {
    buildInputs = [
      pkgs.nodejs-18_x
      pkgs.playwright
    ];

    PLAYWRIGHT_BROWSERS_PATH="${pkgs.playwright-driver.browsers}";
  }

Note that there is no playwright.browsers anymore, now it’s playwright-driver.browsers (see Build failure: playwright Β· Issue #215450 Β· NixOS/nixpkgs Β· GitHub).

I can then do a playwright open nixos.org, but I can’t run tests with npm or run a visual studio code instance for testing with the playwright extension. It always tells me to install the browsers with npx playwright install.

@pbek what if you try playwright-test: init at 1.31.1 by teto Β· Pull Request #227071 Β· NixOS/nixpkgs Β· GitHub ?

1 Like

Thank you, I’m on it!