Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Run headless Firefox with non-default executable path #4075

Closed
johnnyhuynh-dev opened this issue Oct 7, 2020 · 10 comments
Closed

Comments

@johnnyhuynh-dev
Copy link

I followed the "Advanced Installation" guide and found a way to download the binaries browsers into the node_modules folder of my project instead of the default path.

# Linux/macOS
$ PLAYWRIGHT_BROWSERS_PATH=0 npm i -D playwright

# Windows
$ set PLAYWRIGHT_BROWSERS_PATH=0
$ npm i -D playwright

However, when I run the script it returns an error:

browserType.launch: Failed to launch firefox because executable doesn't exist at C:\Users\johnnyhuynh007\AppData\Local\ms-playwright\firefox-1171\firefox\firefox.exe
Try re-installing playwright with "npm install playwright"

I tried to modify the option 'executablePath' in the browserType.launch([options]) but got a similar error:

browserType.launch: Failed to launch firefox because executable doesn't exist at ../node_modules/playwright/.local-browsers/firefox-1171/firefox/firefox.exe

How could I specify the path of the executable for Playwright to run correctly?

*Note: the reason I want this specific behavior is I need to upload my functions to Firebase Cloud functions, which doesn't seem to run normally with the default path option (seems like it doesn't install the browsers when it npm install package.json). By attaching the browsers to the playwright folder in node_modules and upload the whole folder, I hope it could solve the problem.

@mxschmitt
Copy link
Member

Hi, the PLAYWRIGHT_BROWSERS_PATH=0 environment variables needs to be set during installation (npm i) and during running your actual script (node foo.js). Seems like you run your script without the required environment variable.

@johnnyhuynh-dev
Copy link
Author

Hi, the PLAYWRIGHT_BROWSERS_PATH=0 environment variables needs to be set during installation (npm i) and during running your actual script (node foo.js). Seems like you run your script without the required environment variable.

Hi, is there any way that I can override this environment variable in the code? The Cloud Functions I am using doesn't have an option to specify an environment variable when running the script.

@mxschmitt
Copy link
Member

mxschmitt commented Oct 7, 2020

// @ts-check
const playwright = require("playwright");

(async () => {
    // needs to be before the launch of the browser.
    process.env.PLAYWRIGHT_BROWSERS_PATH = "0";
    const browser = await playwright.firefox.launch();
    const context = await browser.newContext();
    const page = await context.newPage();
    await page.goto('http://whatsmyuseragent.org/');
    await page.screenshot({ path: `example.png` });
    await browser.close();
})();

Something like that should work.

@johnnyhuynh-dev
Copy link
Author

johnnyhuynh-dev commented Oct 7, 2020

I tried calling PLAYWRIGHT_BROWSERS_PATH=0 node theScript.js as you said and it worked perfectly. However, this process.env.PLAYWRIGHT_BROWSERS_PATH = "0"; doesn't work. Any kind of npm config or .env setting that I can set it for once and don't need to call it again?
Sorry I am not too familiar with environmental variables.

@mxschmitt
Copy link
Member

Are you running it on Google Cloud Functions?

@johnnyhuynh-dev
Copy link
Author

johnnyhuynh-dev commented Oct 7, 2020

Are you running it on Google Cloud Functions?

I haven't tested on Google Cloud Functions yet, I am trying to test it on my local environment first. Do you have any suggestions for Playwright to work with GCF? All of what I am trying to do is to get it runs on GCF, and the problem with it is I can't execute something like this PLAYWRIGHT_BROWSERS_PATH=0 node foo.js on there.
As I said, GCF doesn't install the browser, and there I am trying to get around it.

@johnnyhuynh-dev
Copy link
Author

After some more research I found that you wrote a buildpack for Heroku.

https://playwright.tech/blog/running-playwright-on-heroku

I think Firebase has a very similar issue with that, which involved the setting of environmental variables or that sort of things.

@sunaram
Copy link

sunaram commented Oct 12, 2020

Just make sure you have set the environment variable before you require the "playwright" module.
e.g. if app.js is your entry script, define
process.env.PLAYWRIGHT_BROWSERS_PATH = 0;
at the top app.js

@dgozman
Copy link
Contributor

dgozman commented Nov 23, 2020

Thanks @sunaram - that should do the trick!

@rknikode
Copy link

Windows

$ set PLAYWRIGHT_BROWSERS_PATH=0
$ npm i playwright-firefox
playwright issue

after doing this thing still browsers installations download in shared location in userDir directory any one have idea how to resolve
OS : Windows 10

PFA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants