feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import WebSocket from 'ws';
|
|
|
|
import { Connection } from '../client/connection';
|
|
|
|
import { Playwright } from '../client/playwright';
|
|
|
|
import { getPlaywrightVersion } from '../utils/utils';
|
|
|
|
|
|
|
|
export class GridClient {
|
|
|
|
private _ws: WebSocket;
|
|
|
|
private _playwright: Playwright;
|
|
|
|
|
|
|
|
static async connect(gridURL: string) {
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
params.set('pwVersion', getPlaywrightVersion(true /* majorMinorOnly */));
|
|
|
|
const ws = new WebSocket(`${gridURL}/claimWorker?` + params.toString());
|
|
|
|
const errorText = await Promise.race([
|
|
|
|
new Promise(f => ws.once('message', () => f(undefined))),
|
|
|
|
new Promise(f => ws.once('close', (code, reason) => f(reason))),
|
|
|
|
]);
|
|
|
|
if (errorText)
|
|
|
|
throw errorText;
|
|
|
|
const connection = new Connection();
|
2021-10-14 20:58:09 -07:00
|
|
|
connection.markAsRemote();
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
connection.onmessage = (message: Object) => ws.send(JSON.stringify(message));
|
|
|
|
ws.on('message', message => connection.dispatch(JSON.parse(message.toString())));
|
2021-10-14 20:58:09 -07:00
|
|
|
ws.on('close', (code, reason) => connection.close(reason));
|
feat: introduce experimental general-purpose grid (#8941)
This patch adds a general-purpose grid framework to parallelize
Playwright across multiple agents.
This patch adds two CLI commands to manage grid:
- `npx playwright experimental-grid-server` - to launch grid
- `npx playwrigth experimental-grid-agent` - to launch agent in a host
environment.
Grid server accepts an `--agent-factory` argument. A simple
`factory.js` might look like this:
```js
const child_process = require('child_process');
module.exports = {
name: 'My Simple Factory',
capacity: Infinity, // How many workers launch per agent
timeout: 10_000, // 10 seconds timeout to create agent
launch: ({agentId, gridURL, playwrightVersion}) => child_process.spawn(`npx`, [
'playwright'
'experimental-grid-agent',
'--grid-url', gridURL,
'--agent-id', agentId,
], {
cwd: __dirname,
shell: true,
stdio: 'inherit',
}),
};
```
With this `factory.js`, grid server could be launched like this:
```bash
npx playwright experimental-grid-server --factory=./factory.js
```
Once launched, it could be used with Playwright Test using env variable:
```bash
PW_GRID=http://localhost:3000 npx playwright test
```
2021-09-16 01:20:36 -07:00
|
|
|
const playwright = await connection.initializePlaywright();
|
|
|
|
playwright._enablePortForwarding();
|
|
|
|
return new GridClient(ws, playwright);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(ws: WebSocket, playwright: Playwright) {
|
|
|
|
this._ws = ws;
|
|
|
|
this._playwright = playwright;
|
|
|
|
}
|
|
|
|
|
|
|
|
playwright(): Playwright {
|
|
|
|
return this._playwright;
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this._ws.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|