clarify examples

This commit is contained in:
Sparticuz 2022-09-27 12:57:48 -04:00
parent c01f72e741
commit 036a043b88
1 changed files with 29 additions and 34 deletions

View File

@ -41,15 +41,12 @@ The @sparticuz/chromium version schema is as follows:
This package works with all the currently supported AWS Lambda Node.js runtimes out of the box. This package works with all the currently supported AWS Lambda Node.js runtimes out of the box.
```javascript ```javascript
const test = requie("node:test");
const puppeteer = require("puppeteer-core"); const puppeteer = require("puppeteer-core");
const chromium = require('@sparticuz/chromium'); const chromium = require("@sparticuz/chromium");
exports.handler = async (event, context, callback) => { test("Check the page title of example.com", async (t) => {
let result = null; const browser = await puppeteer.launch({
let browser = null;
try {
browser = await puppeteer.launch({
args: chromium.args, args: chromium.args,
defaultViewport: chromium.defaultViewport, defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath, executablePath: await chromium.executablePath,
@ -57,47 +54,45 @@ exports.handler = async (event, context, callback) => {
ignoreHTTPSErrors: true, ignoreHTTPSErrors: true,
}); });
let page = await browser.newPage(); const page = await browser.newPage();
await page.goto("https://example.com");
await page.goto(event.url || 'https://example.com'); const pageTitle = await page.title();
result = await page.title();
} catch (error) {
return callback(error);
} finally {
if (browser !== null) {
await browser.close(); await browser.close();
}
}
return callback(null, result); assert.strictEqual(pageTitle, "Example Domain");
}; });
``` ```
### Usage with Playwright ### Usage with Playwright
```javascript ```javascript
const playwright = require('playwright-core'); const test = require("node:test");
// Need to rename playwright's chromium object to something else
const { chromium: playwright } = require('playwright-core');
const chromium = require('@sparticuz/chromium'); const chromium = require('@sparticuz/chromium');
(async () => { test("Check the page title of example.com", async (t) => {
const browser = await playwright.chromium.launch({ const browser = await playwright.launch({
args: chromium.args, args: chromium.args,
executablePath: await chromium.executablePath, executablePath: await chromium.executablePath,
headless: chromium.headless, headless: chromium.headless,
}); });
// ... const context = await browser.newContext();
const page = await context.newPage();
await page.goto("https://example.com");
const pageTitle = await page.title();
await browser.close(); await browser.close();
})();
assert.strictEqual(pageTitle, "Example Domain");
});
``` ```
You should allocate at least 512 MB of RAM to your Lambda, however 1600 MB (or more) is recommended. You should allocate at least 512 MB of RAM to your Lambda, however 1600 MB (or more) is recommended.
### Running Locally ### Running Locally
Please refer to the [Local Development Wiki page](https://github.com/alixaxel/chrome-aws-lambda/wiki/HOWTO:-Local-Development) for instructions and troubleshooting. This package will run in headless mode when `NODE_ENV = "test"`. If you want to run using your own local binary, set `IS_LOCAL` to anything.
## API ## API