Skip to content

Commit

Permalink
Test getBrowserType
Browse files Browse the repository at this point in the history
  • Loading branch information
flaurida authored and jperl committed Apr 14, 2020
1 parent 71c05ec commit b5687b2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -83,6 +83,7 @@
"jest": "^25.3.0",
"jest-mock-process": "^1.3.2",
"playwright": "^0.13.0",
"playwright-webkit": "^0.13.0",
"prettier": "^2.0.4",
"rimraf": "^3.0.2",
"rollup": "^2.6.0",
Expand Down
17 changes: 14 additions & 3 deletions src/utils/launch.ts
Expand Up @@ -43,13 +43,24 @@ export const parseBrowserName = (name?: string): BrowserName => {
return 'chromium';
};

export const getBrowser = (browserName: BrowserName): BrowserType<Browser> => {
export const getBrowserType = (
browserName: BrowserName,
): BrowserType<Browser> => {
// We must use the browser type from the installed `playwright` or `playwright-browser` package,
// and not `playwright-core` since they store different browser binaries.
// See https://github.com/microsoft/playwright/issues/1191 for more details.
let playwright: typeof playwrightCore;

try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
playwright = require('playwright');
} catch (error) {
playwright = require(`playwright-${browserName}`);
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
playwright = require(`playwright-${browserName}`);
} catch (error) {
throw new Error('qawolf requires playwright to be installed');
}
}

return playwright[browserName];
Expand Down Expand Up @@ -89,7 +100,7 @@ export const launch = async (options: LaunchOptions = {}): Promise<Browser> => {
const launchOptions = getLaunchOptions(options);
debug('launch %j', launchOptions);

const browser = await getBrowser(launchOptions.browserName).launch(
const browser = await getBrowserType(launchOptions.browserName).launch(
launchOptions,
);

Expand Down
34 changes: 34 additions & 0 deletions test/utils/launch.test.ts
@@ -1,5 +1,39 @@
import { platform } from 'os';
import playwright from 'playwright';
import { getLaunchOptions } from '../../src/utils';
import { getBrowserType } from '../../src/utils/launch';

describe('getBrowserType', () => {
afterEach(() => jest.resetModules());

it('returns browser type from playwright if possible', () => {
const browserType = getBrowserType('webkit');
expect(browserType).toEqual(playwright.webkit);
});

it('returns browser type from flavored package', () => {
jest.mock('playwright', () => {
throw new Error("Cannot find module 'playwright'");
});

const browserType = getBrowserType('webkit');
expect(typeof browserType.launch).toEqual('function');
});

it('throws an error if cannot import browser type', () => {
jest.mock('playwright', () => {
throw new Error("Cannot find module 'playwright'");
});

jest.mock('playwright-webkit', () => {
throw new Error("Cannot find module 'playwright-webkit'");
});

expect(() => getBrowserType('webkit')).toThrowError(
'qawolf requires playwright to be installed',
);
});
});

describe('getLaunchOptions', () => {
it('chooses a browser based on the name', () => {
Expand Down

0 comments on commit b5687b2

Please sign in to comment.