Skip to content

Commit

Permalink
feat(test-runner): allow setting concurrency per browser
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsDenBakker committed Oct 8, 2020
1 parent 15a3ad2 commit f6afd79
Show file tree
Hide file tree
Showing 28 changed files with 355 additions and 8 deletions.
12 changes: 12 additions & 0 deletions docs/docs/test-runner/browser-launchers/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ export default {
],
};
```

## Concurrency

You can override the concurrency of this specific browser launcher

```js
import { chromeLauncher } from '@web/test-runner-chrome';

export default {
browsers: [chromeLauncher({ concurrency: 1 })],
};
```
12 changes: 12 additions & 0 deletions docs/docs/test-runner/browser-launchers/playwright.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ export default {
],
};
```

## Concurrency

You can override the concurrency of this specific browser launcher

```js
import { playwrightLauncher } from '@web/test-runner-playwright';

export default {
browsers: [playwrightLauncher({ product: 'firefox', concurrency: 1 })],
};
```
16 changes: 16 additions & 0 deletions docs/docs/test-runner/browser-launchers/puppeteer.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ export default {
};
```

## Concurrency

You can override the concurrency of this specific browser launcher

```js
import { puppeteerLauncher } from '@web/test-runner-puppeteer';

export default {
browsers: [
puppeteerLauncher({
concurrency: 1,
}),
],
};
```

## Testing Firefox

Testing Firefox with Puppeteer is still experimental. There is currently no official way to install both chromium and firefox, but you can set this up for your repository by adding a post-install step to your package scripts:
Expand Down
15 changes: 12 additions & 3 deletions packages/test-runner-chrome/src/ChromeLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type CreatePageFunction = (args: {
export class ChromeLauncher implements BrowserLauncher {
public name: string;
public type = 'puppeteer';
public concurrency?: number;
private launchOptions: LaunchOptions;
private customPuppeteer?: typeof puppeteerCore;
private createPageFunction?: CreatePageFunction;
private config?: TestRunnerCoreConfig;
private testFiles?: string[];
private browser?: Browser;
Expand All @@ -32,10 +36,15 @@ export class ChromeLauncher implements BrowserLauncher {
private __launchBrowserPromise?: Promise<Browser>;

constructor(
private launchOptions: LaunchOptions,
private customPuppeteer?: typeof puppeteerCore,
private createPageFunction?: CreatePageFunction,
launchOptions: LaunchOptions,
customPuppeteer?: typeof puppeteerCore,
createPageFunction?: CreatePageFunction,
concurrency?: number,
) {
this.launchOptions = launchOptions;
this.customPuppeteer = customPuppeteer;
this.createPageFunction = createPageFunction;
this.concurrency = concurrency;
if (!customPuppeteer) {
// without a custom puppeteer, we use the locally installed chrome
this.name = 'Chrome';
Expand Down
8 changes: 7 additions & 1 deletion packages/test-runner-chrome/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ export interface ChromeLauncherArgs {
puppeteer?: typeof puppeteerCore;
launchOptions?: LaunchOptions;
createPage?: (args: { config: TestRunnerCoreConfig; browser: Browser }) => Promise<Page>;
concurrency?: number;
}

export { ChromeLauncher };

export function chromeLauncher(args: ChromeLauncherArgs = {}) {
return new ChromeLauncher(args.launchOptions ?? {}, args.puppeteer, args.createPage);
return new ChromeLauncher(
args.launchOptions ?? {},
args.puppeteer,
args.createPage,
args.concurrency,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export interface BrowserLauncher {
*/
type: string;

/**
* Optional concurrency for this browser launcher only. Overwrites a globally
* configured concurrency option.
*/
concurrency?: number;

__experimentalWindowFocus__?: boolean;

/**
Expand Down
9 changes: 8 additions & 1 deletion packages/test-runner-core/src/runner/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ export class TestScheduler {
runningBrowsers += 1;

const runningCount = this.getRunningSessions(browser).length;
const maxBudget = browser.__experimentalWindowFocus__ ? 1 : this.config.concurrency;
let maxBudget;

if (browser.__experimentalWindowFocus__) {
maxBudget = 1;
} else {
maxBudget = browser.concurrency ?? this.config.concurrency;
}

const runBudget = Math.max(0, maxBudget - runningCount);
if (runBudget !== 0) {
// we have budget to schedule new sessions for this browser
Expand Down
15 changes: 12 additions & 3 deletions packages/test-runner-playwright/src/PlaywrightLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export type CreatePageFunction = (args: {
export class PlaywrightLauncher implements BrowserLauncher {
public name: string;
public type = 'playwright';
public concurrency?: number;
private product: ProductType;
private launchOptions: LaunchOptions;
private createPageFunction?: CreatePageFunction;
private config?: TestRunnerCoreConfig;
private testFiles?: string[];
private browser?: Browser;
Expand All @@ -28,11 +32,16 @@ export class PlaywrightLauncher implements BrowserLauncher {
public __experimentalWindowFocus__: boolean;

constructor(
private product: ProductType,
private launchOptions: LaunchOptions,
private createPageFunction?: CreatePageFunction,
product: ProductType,
launchOptions: LaunchOptions,
createPageFunction?: CreatePageFunction,
__experimentalWindowFocus__?: boolean,
concurrency?: number,
) {
this.product = product;
this.launchOptions = launchOptions;
this.createPageFunction = createPageFunction;
this.concurrency = concurrency;
this.name = capitalize(product);
this.__experimentalWindowFocus__ = !!__experimentalWindowFocus__;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/test-runner-playwright/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface PlaywrightLauncherArgs {
launchOptions?: LaunchOptions;
createPage?: (args: { config: TestRunnerCoreConfig; browser: Browser }) => Promise<Page>;
__experimentalWindowFocus__?: boolean;
concurrency?: number;
}

export { PlaywrightLauncher };
Expand All @@ -28,5 +29,6 @@ export function playwrightLauncher(args: PlaywrightLauncherArgs = {}) {
args.launchOptions ?? {},
args.createPage,
!!args.__experimentalWindowFocus__,
args.concurrency,
);
}
3 changes: 3 additions & 0 deletions packages/test-runner-puppeteer/src/puppeteerLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ export interface PuppeteerLauncherConfig {
config: TestRunnerCoreConfig;
browser: puppeteer.Browser;
}) => Promise<puppeteer.Page>;
concurrency?: number;
}

export function puppeteerLauncher({
launchOptions,
createPage,
concurrency,
}: PuppeteerLauncherConfig = {}): BrowserLauncher {
return chromeLauncher({
launchOptions,
puppeteer: (puppeteer as any).default as typeof puppeteer,
createPage,
concurrency,
});
}
24 changes: 24 additions & 0 deletions packages/test-runner/demo/focus.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { playwrightLauncher } from '@web/test-runner-playwright';

export default /** @type {import('@web/test-runner').TestRunnerConfig} */ ({
nodeResolve: true,
rootDir: '../../',

groups: [
{
name: 'firefox',
files: 'demo/test/focus/focus-*.test.js',
browsers: [playwrightLauncher({ product: 'firefox', concurrency: 1 })],
},
{
name: 'chromium',
files: 'demo/test/focus/focus-*.test.js',
browsers: [playwrightLauncher({ product: 'chromium' })],
},
{
name: 'webkit',
files: 'demo/test/focus/focus-*.test.js',
browsers: [playwrightLauncher({ product: 'webkit' })],
},
],
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-a.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus a', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-b.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-c.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-d.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-e.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-f.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-g.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-h.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-i.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-j.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
15 changes: 15 additions & 0 deletions packages/test-runner/demo/test/focus/focus-k.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from '@esm-bundle/chai';

it('can run a test with focus b', async () => {
const input = document.createElement('input');
document.body.appendChild(input);

let firedEvent = false;
input.addEventListener('focus', () => {
firedEvent = true;
});
input.focus();

await Promise.resolve();
expect(firedEvent).to.be.true;
});
Loading

0 comments on commit f6afd79

Please sign in to comment.