Playwright: Difference between revisions

From NixOS Wiki
imported>Voidless
Created page with "== Installing browsers for playwright under NixOS == Normally at first run, playwright will tell you to run <code>playwright install</code>. The purpose of this is to install..."
 
imported>Voidless
No edit summary
Line 3: Line 3:
<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
export PLAYWRIGHT_BROWSERS_PATH=/path/to/drivers
export PLAYWRIGHT_BROWSERS_PATH=/path/to/drivers
</syntaxHighlight>
=== Python example ===
The following flake may be used with <code>nix develop</code> to develop your playwright script:
<syntaxHighlight lang=nix>
{
  description = "Python shell flake";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
    mach-nix.url = "github:davhau/mach-nix";
  };
  outputs = { self, nixpkgs, mach-nix, flake-utils, ... }:
    let
      pythonVersion = "python39";
    in
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
        mach = mach-nix.lib.${system};
        pythonEnv = mach.mkPython {
          python = pythonVersion;
          requirements = ''
            playwright
          '';
        };
      in
      {
        devShells.default = pkgs.mkShellNoCC {
          packages = [ pythonEnv ];
          shellHook = ''
            export PYTHONPATH="${pythonEnv}/bin/python"
            export PLAYWRIGHT_BROWSERS_PATH="${pkgs.playwright-driver.browsers}";
          '';
        };
      }
    );
}
</syntaxHighlight>
</syntaxHighlight>

Revision as of 09:02, 28 August 2023

Installing browsers for playwright under NixOS

Normally at first run, playwright will tell you to run playwright install. The purpose of this is to install browsers for you that it can than use for testing. The installation itself will technically work. Unfortunately the installed browsers will not be suitable to be used inside NixOS. This is due to the fact that dependencies will not be at places where the browsers expect them to be. To mitigate this problem, nixpkgs has a package called playwright-driver.browsers. Before you start your script make sure to set

export PLAYWRIGHT_BROWSERS_PATH=/path/to/drivers