Skip to content

Commit

Permalink
feat(core): add chromium auto detection for BrowserPool
Browse files Browse the repository at this point in the history
  • Loading branch information
wadackel committed Jan 1, 2021
1 parent 3c5aedf commit 21f7228
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
10 changes: 4 additions & 6 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ The following is an example of using the [@acot/acot-preset-wcag](../acot-preset

### Simple

In the simple example, the following package is used.
In the simple example, it depends on the following package.

- [puppeteer](https://github.com/puppeteer/puppeteer)
```bash
$ npm install --save puppeteer
```

The following is an example of specifying all audited pages and their configuration.

```typescript
import puppeteer from 'puppeteer';
import { Acot, PresetLoader } from '@acot/core';

const cwd = process.cwd();
Expand All @@ -35,9 +36,6 @@ const cwd = process.cwd();
parallel: 4,
origin: 'http://localhost:8000',
presets: [loader.load('@acot/wcag')],
launchOptions: {
executablePath: puppeteer.executablePath(),
},
cwd,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"@acot/factory": "0.0.4",
"@acot/find-chrome": "0.0.4",
"@acot/html-pickup": "0.0.4",
"@acot/logger": "0.0.4",
"@acot/module-loader": "0.0.4",
Expand All @@ -49,7 +50,6 @@
"puppeteer-element2selector": "^0.0.3"
},
"devDependencies": {
"@acot/find-chrome": "0.0.4",
"@acot/mock": "0.0.2",
"@types/lodash": "^4.14.165",
"@types/puppeteer-core": "^2.0.0",
Expand Down
54 changes: 42 additions & 12 deletions packages/core/src/__tests__/browser-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ describe('BrowserPool', () => {

return new BrowserPool(...args);
};
class MockBrowser extends Browser {
public launchOptions: LaunchOptions = {} as any;

beforeEach(() => {
class MockBrowser extends Browser {
async launch(_: LaunchOptions) {
const closer = {
close: async () => {},
} as any;
async launch(launchOptions: LaunchOptions) {
this.launchOptions = launchOptions;

const closer = {
close: async () => {},
} as any;

this._browser = closer;
this._page = closer;
this._browser = closer;
this._page = closer;

this.close = jest.fn().mockReturnValue(Promise.resolve());
this.close = jest.fn().mockReturnValue(Promise.resolve());

return this;
}
return this;
}
}

beforeEach(() => {
jest.doMock('../browser', () => ({
Browser: MockBrowser,
}));
Expand Down Expand Up @@ -61,7 +64,7 @@ describe('BrowserPool', () => {
});
});

test('bootstrap', async () => {
test('bootstrap - chrome auto detection', async () => {
pool = await factory({
launchOptions: {},
timeout: 1000,
Expand All @@ -70,6 +73,33 @@ describe('BrowserPool', () => {
await pool.bootstrap(4);

expect(pool['_available'].size).toBe(4);

for (const browser of pool['_available']) {
expect(
(browser as MockBrowser).launchOptions.executablePath,
).not.toBeFalsy();
}
});

test('bootstrap - specify chrome executablePath', async () => {
const executablePath = '/path/to';

pool = await factory({
launchOptions: {
executablePath,
},
timeout: 1000,
});

await pool.bootstrap(4);

expect(pool['_available'].size).toBe(4);

for (const browser of pool['_available']) {
expect((browser as MockBrowser).launchOptions.executablePath).toBe(
executablePath,
);
}
});

/**
Expand Down
22 changes: 20 additions & 2 deletions packages/core/src/browser-pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { LaunchOptions } from '@acot/types';
import { findChrome } from '@acot/find-chrome';
import { Browser } from './browser';
import { Queue } from './queue';
import { debug } from './logging';

const WORK_INTERVAL = 30;

Expand Down Expand Up @@ -54,11 +56,27 @@ export class BrowserPool {
}

public async bootstrap(parallel: number): Promise<void> {
const { launchOptions } = this._config;
let opts = this._config.launchOptions;

if (!opts.executablePath) {
debug('try chrome auto detection...');

const chrome = await findChrome();
if (chrome == null) {
throw new Error('Executable Chromium was not found.');
}

debug('auto detected chrome: %O', chrome);

opts = {
...opts,
executablePath: chrome.executablePath,
};
}

await Promise.all(
Array.from(Array(parallel || 1)).map(async (_, i) => {
this._available.add(await new Browser(i).launch(launchOptions));
this._available.add(await new Browser(i).launch(opts));
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/document/src/doc-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class DocTester {
public async test(project: DocProject): Promise<DocResult> {
const port = this._server.port;

const chromium = await findChrome({});
const chromium = await findChrome();
debug('found chromium: %O', chromium);

await this._server.bootstrap(project);
Expand Down

0 comments on commit 21f7228

Please sign in to comment.