playwright: [BUG] Google Cloud Function error (deploy)

Context:

  • Operating System: Linux
  • Node version: Node.js 10
  • Browser: Webkit
  • Extra: [any specific details about your environment]

Code Snippet

const {chromium, webkit, firefox} = require('playwright');

(async () => {
      const browser = await webkit.launch({ headless: true });
      const context = await browser.newContext();
      const page = await context.newPage();
})();

Describe the bug

I’m trying to deploy my playwright application in Google Cloud Functions. It seems like there is a problem to spawn the browser.

Error: function terminated. Recommended action: inspect logs for termination reason. Details:
Failed to launch browser: Error: spawn /root/.cache/ms-playwright/chromium-764964/chrome-linux/chrome ENOENT

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16

Most upvoted comments

I’m currently using:

* playwright 1.6.2 (chromium 88.0.4324.0)

* chrome-aws-lambda 5.5.0 (chromium 88.0.4298.0)

Without issues (I had issues when the major version was different).

Pseudo code:

import { chromium } from 'playwright-core';
import * as bundledChromium from 'chrome-aws-lambda';

....
  const browser = await Promise.resolve(bundledChromium.executablePath).then(
      (executablePath) => {
        if (!executablePath) {
          // local execution
          return chromium.launch({});
        }
        return chromium.launch({ executablePath });
      }
    );

Your approach seems promising as well, not sure why the shared library is missing.

I can confirm that the above solution works well. Thank you for sharing this !

Here is a complete example that returns a screenshot of google when queried. (Using NodeJS 14, 2GB RAM, latest chrome-aws-lambda version)

package.json

{
  "dependencies": {
        "chrome-aws-lambda": "10.1.0",
        "playwright-core": "1.14.1"
  }
}

index.js

const { chromium } = require('playwright-core');
const bundledChromium = require('chrome-aws-lambda');

module.exports.screenshot = async function (req, res) {
    const url = "https://google.com/";
    
  const browser = await Promise.resolve(bundledChromium.executablePath).then(
      (executablePath) => {
        if (!executablePath) {
          // local execution
          return chromium.launch({});
        }
        return chromium.launch({ executablePath });
      }
    );
    const page = await browser.newPage();
    await page.goto(url);
    const screenshotBuffer = await page.screenshot({ fullPage: true });
    await browser.close();
    res.set("Content-Type", "image/png");
    res.status(500).send(screenshotBuffer)        
};

I’m currently using:

  • playwright 1.6.2 (chromium 88.0.4324.0)
  • chrome-aws-lambda 5.5.0 (chromium 88.0.4298.0)

Without issues (I had issues when the major version was different).

Pseudo code:

import { chromium } from 'playwright-core';
import * as bundledChromium from 'chrome-aws-lambda';

....
  const browser = await Promise.resolve(bundledChromium.executablePath).then(
      (executablePath) => {
        if (!executablePath) {
          // local execution
          return chromium.launch({});
        }
        return chromium.launch({ executablePath });
      }
    );

Your approach seems promising as well, not sure why the shared library is missing.

This seems to work well now (I have only tested with playwright-chromium).

Only initial issue was that the launch method did not find the executable:

browserType.launch: Failed to launch chromium because executable doesn’t exist at /root/.cache/ms-playwright/chromium-888113/chrome-linux/chrome

Setting the environment variable PLAYWRIGHT_BROWSERS_PATH=0 resolved this, and I can now use playwright-chromium in Google Cloud Function to e.g. generate pdf 😃

Cheers -jo

BTW - had to raise memory limit to 1 GB to avoid running out of memory…