Skip to content

Commit

Permalink
pre-8.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
alixaxel committed Mar 31, 2021
1 parent 1bd49b1 commit 3f94a48
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ clean:
%.zip:
npm install --no-fund --no-package-lock --no-shrinkwrap --only=dev
mkdir -p nodejs/
npm install --prefix nodejs/ lambdafs@~2.0.3 puppeteer-core@~8.0.0 --no-bin-links --no-fund --no-optional --no-package-lock --no-save --no-shrinkwrap
npm install --prefix nodejs/ lambdafs@~2.0.3 puppeteer-core@~8.0.1 --no-bin-links --no-fund --no-optional --no-package-lock --no-save --no-shrinkwrap
npm pack
mkdir -p nodejs/node_modules/chrome-aws-lambda/
tar --directory nodejs/node_modules/chrome-aws-lambda/ --extract --file chrome-aws-lambda-*.tgz --strip-components=1
Expand Down
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ exports.handler = async (event, context, callback) => {
};
```

### Usage with Playwright

```javascript
const chromium = require('chrome-aws-lambda');
const playwright = require('playwright-core');

(async () => {
const browser = await playwright.chromium.launch({
args: chromium.args,
executablePath: await chromium.executablePath,
headless: chromium.headless,
});

// ...

await browser.close();
})();
```

You should allocate at least 512 MB of RAM to your Lambda, however 1600 MB (or more) is recommended.

### Running Locally
Expand Down Expand Up @@ -139,8 +158,10 @@ interface BrowserContext {
interface Page {
block(patterns: string[])
clickAndWaitForNavigation(selector: string, options?: WaitForOptions)
clickAndWaitForRequest(selector: string, pattern: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, pattern: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(selector: string, predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(selector: string, predicate: ((request: HTTPRequest) => boolean), options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, predicate: ((request: HTTPResponse) => boolean), options?: WaitTimeoutOptions)
count(selector: string)
exists(selector: string)
fillFormByLabel(selector: string, data: Record<string, boolean | string | string[]>)
Expand All @@ -150,15 +171,18 @@ interface Page {
number(selector: string, decimal?: string, property?: string)
selectByLabel(selector: string, ...values: string[])
string(selector: string, property?: string)
waitForText(predicate: string, options?: WaitTimeoutOptions)
waitUntilVisible(selector: string, options?: WaitTimeoutOptions)
waitWhileVisible(selector: string, options?: WaitTimeoutOptions)
withTracing(options: TracingOptions, callback: (page: Page) => Promise<any>)
}

interface Frame {
clickAndWaitForNavigation(selector: string, options?: WaitForOptions)
clickAndWaitForRequest(selector: string, pattern: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, pattern: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(selector: string, predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(selector: string, predicate: ((request: HTTPRequest) => boolean), options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(selector: string, predicate: ((request: HTTPResponse) => boolean), options?: WaitTimeoutOptions)
count(selector: string)
exists(selector: string)
fillFormByLabel(selector: string, data: Record<string, boolean | string | string[]>)
Expand All @@ -168,14 +192,17 @@ interface Frame {
number(selector: string, decimal?: string, property?: string)
selectByLabel(selector: string, ...values: string[])
string(selector: string, property?: string)
waitForText(predicate: string, options?: WaitTimeoutOptions)
waitUntilVisible(selector: string, options?: WaitTimeoutOptions)
waitWhileVisible(selector: string, options?: WaitTimeoutOptions)
}

interface ElementHandle {
clickAndWaitForNavigation(options?: WaitForOptions)
clickAndWaitForRequest(pattern: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(pattern: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForRequest(predicate: ((request: HTTPRequest) => boolean), options?: WaitTimeoutOptions)
clickAndWaitForResponse(predicate: string | RegExp, options?: WaitTimeoutOptions)
clickAndWaitForResponse(predicate: ((request: HTTPResponse) => boolean), options?: WaitTimeoutOptions)
fillFormByLabel(data: Record<string, boolean | string | string[]>)
fillFormByName(data: Record<string, boolean | string | string[]>)
fillFormBySelector(data: Record<string, boolean | string | string[]>)
Expand Down Expand Up @@ -224,7 +251,7 @@ This package is versioned based on the underlying `puppeteer` minor version:

| `puppeteer` Version | `chrome-aws-lambda` Version | Chromium Revision |
| ------------------- | --------------------------------- | ---------------------------------------------------- |
| `8.0.*` | `npm i chrome-aws-lambda@~8.0.0` | [`856583`](https://crrev.com/856583) (`90.0.4427.0`) |
| `8.0.*` | `npm i chrome-aws-lambda@~8.0.1` | [`856583`](https://crrev.com/856583) (`90.0.4427.0`) |
| `7.0.*` | `npm i chrome-aws-lambda@~7.0.0` | [`848005`](https://crrev.com/848005) (`90.0.4403.0`) |
| `6.0.*` | `npm i chrome-aws-lambda@~6.0.0` | [`843427`](https://crrev.com/843427) (`89.0.4389.0`) |
| `5.5.*` | `npm i chrome-aws-lambda@~5.5.0` | [`818858`](https://crrev.com/818858) (`88.0.4298.0`) |
Expand Down Expand Up @@ -276,7 +303,6 @@ The following set of (Linux) commands will create a layer of this package alongs
```shell
git clone --depth=1 https://github.com/alixaxel/chrome-aws-lambda.git && \
cd chrome-aws-lambda && \
npm install --only=dev && \
make chrome_aws_lambda.zip
```

Expand All @@ -294,8 +320,8 @@ The Chromium binary is compressed using the Brotli algorithm.

This allows us to get the best compression ratio and faster decompression times.

| File | Algorithm | Level | Bytes | MiB | % | Inflation |
| ----------- | --------- | ----- | --------- | --------- | ---------- | ---------- |
| File | Algorithm | Level | Bytes | MiB | % | Inflation |
| ------------- | --------- | ----- | --------- | --------- | ---------- | ---------- |
| `chromium` | - | - | 136964856 | 130.62 | - | - |
| `chromium.gz` | Gzip | 1 | 51662087 | 49.27 | 62.28% | 1.035s |
| `chromium.gz` | Gzip | 2 | 50438352 | 48.10 | 63.17% | 1.016s |
Expand Down
28 changes: 14 additions & 14 deletions source/puppeteer/lib/ElementHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,46 @@ Super.prototype.clickAndWaitForNavigation = function (options?: WaitForOptions)
return Promise.all(promises).then((value) => value.shift() as HTTPResponse);
};

Super.prototype.clickAndWaitForRequest = function (pattern: string | RegExp, options?: WaitTimeoutOptions) {
Super.prototype.clickAndWaitForRequest = function (predicate: string | RegExp | ((request: HTTPRequest) => boolean), options?: WaitTimeoutOptions) {
let callback = (request: HTTPRequest) => {
let url = request.url();

if (typeof pattern === 'string' && pattern.includes('*') === true) {
pattern = new RegExp(pattern.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
if (typeof predicate === 'string' && predicate.includes('*') === true) {
predicate = new RegExp(predicate.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
}

if (pattern instanceof RegExp) {
return pattern.test(url);
if (predicate instanceof RegExp) {
return predicate.test(url);
}

return pattern === url;
return predicate === url;
};

let promises: [Promise<HTTPRequest>, Promise<void>] = [
((this as any)._page as Page).waitForRequest(callback, options),
((this as any)._page as Page).waitForRequest((typeof predicate === 'function') ? predicate : callback, options),
this.click(),
];

return Promise.all(promises).then((value) => value.shift() as HTTPRequest);
};

Super.prototype.clickAndWaitForResponse = function (pattern: string | RegExp, options?: WaitTimeoutOptions) {
Super.prototype.clickAndWaitForResponse = function (predicate: string | RegExp | ((request: HTTPResponse) => boolean), options?: WaitTimeoutOptions) {
let callback = (request: HTTPResponse) => {
let url = request.url();

if (typeof pattern === 'string' && pattern.includes('*') === true) {
pattern = new RegExp(pattern.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
if (typeof predicate === 'string' && predicate.includes('*') === true) {
predicate = new RegExp(predicate.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
}

if (pattern instanceof RegExp) {
return pattern.test(url);
if (predicate instanceof RegExp) {
return predicate.test(url);
}

return pattern === url;
return predicate === url;
};

let promises: [Promise<HTTPResponse>, Promise<void>] = [
((this as any)._page as Page).waitForResponse(callback, options),
((this as any)._page as Page).waitForResponse((typeof predicate === 'function') ? predicate : callback, options),
this.click(),
];

Expand Down
43 changes: 29 additions & 14 deletions source/puppeteer/lib/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,46 @@ Super.prototype.clickAndWaitForNavigation = function (selector: string, options?
return Promise.all(promises).then((value) => value.shift() as HTTPResponse);
};

Super.prototype.clickAndWaitForRequest = function (selector: string, pattern: string | RegExp, options?: WaitTimeoutOptions) {
Super.prototype.clickAndWaitForRequest = function (selector: string, predicate: string | RegExp | ((request: HTTPRequest) => boolean), options?: WaitTimeoutOptions) {
let callback = (request: HTTPRequest) => {
let url = request.url();

if (typeof pattern === 'string' && pattern.includes('*') === true) {
pattern = new RegExp(pattern.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
if (typeof predicate === 'string' && predicate.includes('*') === true) {
predicate = new RegExp(predicate.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
}

if (pattern instanceof RegExp) {
return pattern.test(url);
if (predicate instanceof RegExp) {
return predicate.test(url);
}

return pattern === url;
return predicate === url;
};

let promises: [Promise<HTTPRequest>, Promise<void>] = [
((this._frameManager as any)._page as Page).waitForRequest(callback, options),
((this._frameManager as any)._page as Page).waitForRequest((typeof predicate === 'function') ? predicate : callback, options),
this.click(selector),
];

return Promise.all(promises).then((value) => value.shift() as HTTPRequest);
};

Super.prototype.clickAndWaitForResponse = function (selector: string, pattern: string | RegExp, options?: WaitTimeoutOptions) {
Super.prototype.clickAndWaitForResponse = function (selector: string, predicate: string | RegExp | ((request: HTTPResponse) => boolean), options?: WaitTimeoutOptions) {
let callback = (request: HTTPResponse) => {
let url = request.url();

if (typeof pattern === 'string' && pattern.includes('*') === true) {
pattern = new RegExp(pattern.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
if (typeof predicate === 'string' && predicate.includes('*') === true) {
predicate = new RegExp(predicate.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&').replace(/[*]+/g, '.*?'), 'g');
}

if (pattern instanceof RegExp) {
return pattern.test(url);
if (predicate instanceof RegExp) {
return predicate.test(url);
}

return pattern === url;
return predicate === url;
};

let promises: [Promise<HTTPResponse>, Promise<void>] = [
((this._frameManager as any)._page as Page).waitForResponse(callback, options),
((this._frameManager as any)._page as Page).waitForResponse((typeof predicate === 'function') ? predicate : callback, options),
this.click(selector),
];

Expand Down Expand Up @@ -114,6 +114,21 @@ Super.prototype.string = function <T = HTMLElement>(selector: string, property:
return this.$(selector).then((element) => element?.string(property) ?? null);
};

Super.prototype.waitForText = function (predicate: string, options?: WaitTimeoutOptions) {
if (predicate.includes(`"`) !== true) {
predicate = `"${predicate}"`;
} else if (predicate.includes(`'`) !== true) {
predicate = `'${predicate}'`;
} else {
throw new Error('Predicate cannot include both single and double quotes.');
}

return this.waitForXPath(`//*[contains(concat(' ', normalize-space(text()), ' '), ${predicate})]`, {
...options,
visible: true,
});
};

Super.prototype.waitUntilVisible = function (selector: string, options?: WaitTimeoutOptions) {
return this.waitForSelector(selector, {
...options,
Expand Down
15 changes: 9 additions & 6 deletions source/puppeteer/lib/Page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CDPSession, Page, TracingOptions, WaitForOptions, WaitTimeoutOptions } from 'puppeteer-core';
import { CDPSession, HTTPRequest, HTTPResponse, Page, TracingOptions, WaitForOptions, WaitTimeoutOptions } from 'puppeteer-core';
import { KeysOfType, Prototype } from '../../../typings/chrome-aws-lambda';

let Super: Prototype<Page> = null;
Expand All @@ -19,12 +19,12 @@ Super.prototype.clickAndWaitForNavigation = function (selector: string, options?
return this.mainFrame().clickAndWaitForNavigation(selector, options);
};

Super.prototype.clickAndWaitForRequest = function (selector: string, pattern: string | RegExp, options?: WaitTimeoutOptions) {
return this.mainFrame().clickAndWaitForRequest(selector, pattern, options);
Super.prototype.clickAndWaitForRequest = function (selector: string, predicate: string | RegExp | ((request: HTTPRequest) => boolean), options?: WaitTimeoutOptions) {
return this.mainFrame().clickAndWaitForRequest(selector, predicate as any, options);
};

Super.prototype.clickAndWaitForResponse = function (selector: string, pattern: string | RegExp, options?: WaitTimeoutOptions) {
return this.mainFrame().clickAndWaitForResponse(selector, pattern, options);
Super.prototype.clickAndWaitForResponse = function (selector: string, predicate: string | RegExp | ((request: HTTPResponse) => boolean), options?: WaitTimeoutOptions) {
return this.mainFrame().clickAndWaitForResponse(selector, predicate as any, options);
};

Super.prototype.count = function (selector: string) {
Expand Down Expand Up @@ -63,12 +63,15 @@ Super.prototype.string = function <T = HTMLElement>(selector: string, property:
return this.mainFrame().string(selector, property);
};

Super.prototype.waitForText = function (predicate: string, options?: WaitTimeoutOptions) {
return this.mainFrame().waitForText(predicate, options);
};

Super.prototype.waitUntilVisible = function (selector: string, options?: WaitTimeoutOptions) {
return this.mainFrame().waitUntilVisible(selector, options);
};

Super.prototype.waitWhileVisible = function (selector: string, options?: WaitTimeoutOptions) {
this.coverage.startCSSCoverage()
return this.mainFrame().waitWhileVisible(selector, options);
};

Expand Down
Loading

0 comments on commit 3f94a48

Please sign in to comment.