Running playwright tests

Hi!

I use this config and everything works well :

# shell.nix
{
  pkgs ? import <nixpkgs> {},
  unstable ? import <unstable> { }
}:
  pkgs.mkShell {
    # nativeBuildInputs is usually what you want -- tools you need to run
    # Check last version of playwright https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=playwright-driver
    nativeBuildInputs = with pkgs; [
      unstable.playwright-driver.browsers
    ];

    shellHook = ''
      export PLAYWRIGHT_BROWSERS_PATH=${unstable.playwright-driver.browsers}
      export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=true
    '';

}

The version of playwright in unstable is 1.40.0. But I need to use the last version of playwright : 1.46.1
I wonder how to use the last version with my shell.nix ?

I have prepared a flake that provides a bunch of versions of playwright so that it will be possible to choose it independently of nixpkgs: GitHub - voidus/nix-playwright-browsers: Nix derivations for Playwright browsers in various versions

Itā€™s very early stage but I think the idea is solid.

Since nixpkgs is not meant to contain lots of version of the same package, I put it in a separate flake.

4 Likes

I have tried the examples here, but non are working. Does anyone have a working flake example to work in the cli or vscode?

Iā€™ve beeing using this setup with devenv.sh flake and using playwright as a nodejs dependency in a nextjs project:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
    systems.url = "github:nix-systems/default";
    devenv.url = "github:cachix/devenv";
  };

  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };

  outputs = { self, nixpkgs, devenv, systems, ... } @ inputs:
    let
      forEachSystem = nixpkgs.lib.genAttrs (import systems);
    in
    {
      devShells = forEachSystem
        (system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            yarnVersion = "4.0.1";
          in
          {
            default = devenv.lib.mkShell {
              inherit inputs pkgs;
              modules = [
                {
                  # https://devenv.sh/reference/options/
                  packages = [
                    pkgs.postgresql
                    pkgs.playwright-driver.browsers
                    pkgs.nodejs
                  ];

                  env.YARN_VERSION = yarnVersion;
                  env.PLAYWRIGHT_BROWSERS_PATH = "${pkgs.playwright-driver.browsers}";
                  env.PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS = true;
                  env.PLAYWRIGHT_NODEJS_PATH = "${pkgs.nodejs}/bin/node";
                  env.PLAYWRIGHT_LAUNCH_OPTIONS_EXECUTABLE_PATH = "${pkgs.playwright-driver.browsers}/chromium-1048/chrome-linux/chrome";


                  languages.javascript = {
                    enable = true;
                    corepack.enable = true;
                    package = pkgs.nodejs;
                  };
                  enterShell = ''
                    corepack prepare yarn@${yarnVersion}
                  '';

                }
              ];
            };
          });
    };
}

So you are using devenv.nix and a flake.nix file together? How does that work? It looks like what youā€™ve shared is the flakeā€¦ is it possible to share the devenv.nix file too?

We are having some issues using just devenv.nix with our Playwright installation (pnpm dependency). Itā€™s a TypeScript project, but when we run pnpm exec playwright test we are seeing this error:

Error: browserType.launch: Executable doesn't exist at /nix/store/lrrc9yfclwvck9q8ng1b3c7560nlv51a-playwright-browsers/chromium_headless_shell-1148/chrome-linux/headless_shell

That location doesnā€™t exist, but /nix/store/lrrc9yfclwvck9q8ng1b3c7560nlv51a-playwright-browsers/chromium_headless_shell-1148/chrome-linux/chrome does exist.

Yes, thatā€™s a plain flake.nix file, but it is using the devenv repository. You should be able to use it just by running nix develop --impure. In this setup you donā€™t have a devenv.nix file.

Iā€™m just not sure why you are getting this error with pnpm, for me using npm works just fine. Are you also using devenv? Could you share the flake file you are using?

We werenā€™t using a flake at all, was using a devenv.nix file. But Iā€™ve converted it to a flake and get the exact same error as when I use the devenv.nix file.

Here is the flake:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable/";
    systems.url = "github:nix-systems/default";
    devenv.url = "github:cachix/devenv";
    node.url = "github:NixOS/nixpkgs/42754a724d486c2bb4888c7602fb4f7d90772931";
  };

  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };

  outputs = { self, nixpkgs, devenv, systems, node, ... } @ inputs:
    let
      forEachSystem = nixpkgs.lib.genAttrs (import systems);
    in
    {
      devShells = forEachSystem
        (system:
          let
            pkgs = nixpkgs.legacyPackages.${system};
            node_pkgs = import node { system = pkgs.stdenv.system; };
          in
          {
            default = devenv.lib.mkShell {
              inherit inputs pkgs;
              modules = [
                {
                  # https://devenv.sh/reference/options/
                  packages = [
                    node_pkgs.nodejs_20
                    pkgs.git
                    pkgs.playwright-driver.browsers
                    pkgs.pnpm
                  ];

                  env.PLAYWRIGHT_BROWSERS_PATH = "${pkgs.playwright-driver.browsers}";
                  env.PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS = true;
                  env.PLAYWRIGHT_NODEJS_PATH = "${node_pkgs.nodejs_20}/bin/node";
                  env.PLAYWRIGHT_LAUNCH_OPTIONS_EXECUTABLE_PATH = "${pkgs.playwright-driver.browsers}/chromium-1134/chrome-linux/chrome";
                }
              ];
            };
          });
    };
}

Iā€™ve tried your flake file and got the same error.
After looking into it, it seems that the path is actually ā€œl/nix/store/x2ih0irl5zby5wgyfrcfjkjfkqfgzg28-playwright-browsers/chromium-1134/chrome-linux/chromeā€

Itā€™s in chromium-1134 but for some reason playwright is expecting at chromium-1148.

I just do not know why.

OK, I got past playwright expecting the wrong version of chromium. Looking about the internet, I found a similar issue discussed here [Bug] BrowserType.launch: Executable doesn't exist at /root/.cache/ms-playwright/chromium-1140/chrome-linux/chrome Ā· Issue #33673 Ā· microsoft/playwright Ā· GitHub.

Looking at that made me realize it was a mismatch between the playwright version requested in my package.json file (v1.49.0) and the related driver version supplied by nixpkgs-unstable (v1.47.0). So updating the package.json file resolved the browserType.launch: Executable doesn't exist error.

Unfortunately, the same tests are still failing. But now itā€™s due to:

Test timeout of 30000ms exceeded.

Another puzzle to solve!

1 Like