Skip to content

Commit

Permalink
fix(puppet): chrome 89 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Sep 14, 2021
1 parent b5e541f commit 9ab8bc9
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 72 deletions.
6 changes: 3 additions & 3 deletions core/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import * as http from 'http';
import { Log } from '@ulixee/commons/lib/Logger';
import BrowserEmulator from '@ulixee/default-browser-emulator';
import { DependenciesMissingError } from '@ulixee/chrome-app/lib/DependenciesMissingError';
import { DependencyInstaller } from '@ulixee/chrome-app/lib/DependencyInstaller';
import ChromeApp from '@ulixee/chrome-app/index';
import BrowserEngine from '@ulixee/hero-plugin-utils/lib/BrowserEngine';

const validate = jest.spyOn(DependencyInstaller.prototype, 'validate');
const validate = jest.spyOn(BrowserEngine.prototype, 'verifyLaunchable');
const logError = jest.spyOn(Log.prototype, 'error');

let httpServer: Helpers.ITestHttpServer<http.Server>;
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('basic connection tests', () => {

logError.mockClear();
validate.mockClear();
validate.mockImplementationOnce(() => {
validate.mockImplementationOnce(async () => {
throw new DependenciesMissingError(
`You can resolve this by running the apt dependency installer at:${ChromeApp.aptScriptPath}`,
'Chrome',
Expand Down
17 changes: 10 additions & 7 deletions fullstack/test/emulate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { GlobalPool } from '@ulixee/hero-core';
import { ITestKoaServer } from '@ulixee/hero-testing/helpers';
import Resolvable from '@ulixee/commons/lib/Resolvable';
import Viewports from '@ulixee/default-browser-emulator/lib/Viewports';
import { latestChromeBrowserVersion } from '@ulixee/default-browser-emulator';
import Hero from '../index';

const chromeVersion = latestChromeBrowserVersion.major;

let koaServer: ITestKoaServer;
beforeAll(async () => {
koaServer = await Helpers.runKoaServer(true);
Expand Down Expand Up @@ -190,7 +193,8 @@ describe('geolocation', () => {
Helpers.needsClosing.push(hero);
await hero.goto(koaServer.baseUrl);

const geolocation = await hero.getJsValue(`new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
const geolocation =
await hero.getJsValue(`new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
resolve({ latitude: position.coords.latitude, longitude: position.coords.longitude });
}))`);
expect(geolocation).toEqual({
Expand All @@ -204,8 +208,7 @@ describe('user hero and platform', () => {
const propsToGet = `appVersion, platform, userAgent, deviceMemory`.split(',').map(x => x.trim());

it('should be able to configure a userAgent', async () => {
const userAgent =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4472.124 Safari/537.36';
const userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion}.0.4389.114 Safari/537.36`;
const hero = new Hero({
userAgent,
});
Expand All @@ -217,28 +220,28 @@ describe('user hero and platform', () => {

it('should be able to configure a userAgent with a range', async () => {
const hero = new Hero({
userAgent: '~ chrome >= 88 && chrome < 89',
userAgent: `~ chrome >= ${chromeVersion} && chrome < ${Number(chromeVersion) + 1}`,
});
Helpers.needsClosing.push(hero);

const heroMeta = await hero.meta;
const chromeMatch = heroMeta.userAgentString.match(/Chrome\/(\d+)/);
expect(chromeMatch).toBeTruthy();
const version = Number(chromeMatch[1]);
expect(version).toBe(88);
expect(version).toBe(89);
});

it('should be able to configure a userAgent with a wildcard', async () => {
const hero = new Hero({
userAgent: '~ chrome = 88.x',
userAgent: `~ chrome = ${chromeVersion}.x`,
});
Helpers.needsClosing.push(hero);

const heroMeta = await hero.meta;
const chromeMatch = heroMeta.userAgentString.match(/Chrome\/(\d+)/);
expect(chromeMatch).toBeTruthy();
const version = Number(chromeMatch[1]);
expect(version).toBe(88);
expect(version).toBe(89);
});

it('should add user hero and platform to window & frames', async () => {
Expand Down
27 changes: 21 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ const Fs = require('fs');
const Path = require('path');
const pkg = require('./package.json');

const workspaces = pkg.workspaces.packages
.filter(x => {
if (x.startsWith('../')) return false;
return (Fs.existsSync(Path.resolve(__dirname, x)));
})
.map(x => x.replace('/*', ''));
const workspaces = [];
for (const packageGlob of pkg.workspaces.packages) {
if (packageGlob.startsWith('../')) continue;

let workspacePath = packageGlob;
// if we're not in build already, need to add build
if (!__dirname.includes('build') && !workspacePath.includes('build')) {
workspacePath = `build/${workspacePath}`;
}
if (workspacePath.endsWith('/*')) {
workspacePath = workspacePath.replace('/*', '');
for (const subdir of Fs.readdirSync(Path.resolve(__dirname, workspacePath))) {
if (subdir === 'node_modules') continue;
if (!Fs.statSync(Path.resolve(__dirname, workspacePath, subdir)).isDirectory()) continue;
if (!Fs.existsSync(Path.resolve(__dirname, workspacePath, subdir, 'package.json'))) continue;
workspaces.push(`${workspacePath}/${subdir}`);
}
} else {
workspaces.push(workspacePath);
}
}

module.exports = {
verbose: false,
Expand Down
17 changes: 12 additions & 5 deletions puppet/lib/launchProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ export default function launchProcess(
stdio,
});

const dataDir = processArguments
.find(x => x.startsWith('--user-data-dir='))
?.split('--user-data-dir=')
?.pop();
// Prevent Unhandled 'error' event.
launchedProcess.on('error', () => {});

Expand Down Expand Up @@ -83,6 +79,11 @@ export default function launchProcess(
if (onClose) onClose();
hasRunOnClose = true;
};
const dataDir = processArguments
.find(x => x.startsWith('--user-data-dir='))
?.split('--user-data-dir=')
?.pop();

let processKilled = false;
let transport: PipeTransport;
launchedProcess.once('exit', (exitCode, signal) => {
Expand All @@ -100,7 +101,8 @@ export default function launchProcess(
if (dataDir) cleanDataDir(dataDir);
});

process.on('uncaughtExceptionMonitor', () => close());
process.once('exit', close);
process.once('uncaughtExceptionMonitor', close);
ShutdownHandler.register(close);
const { 3: pipeWrite, 4: pipeRead } = launchedProcess.stdio;
transport = new PipeTransport(
Expand All @@ -120,6 +122,11 @@ export default function launchProcess(
if (transport) {
transport.end(JSON.stringify({ method: 'Browser.close', id: -1 }));
}
} catch (error) {
// this might fail, we want to keep going
}

try {
if (!launchedProcess.killed && !processKilled) {
const closed = new Promise<void>(resolve => launchedProcess.once('exit', resolve));
if (process.platform === 'win32') {
Expand Down
103 changes: 55 additions & 48 deletions puppet/test/BrowserContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ describe('BrowserContext', () => {
await Promise.all([context.close(), context.close()]);
await expect(context.close()).resolves.toBe(undefined);
});

it('can create a page with the default context', async () => {
const plugins = new CorePlugins({ browserEmulatorId }, log as IBoundLog);
const context = await puppet.newContext(plugins, log, null, true);
needsClosing.push(context);
const page = await context.newPage();
needsClosing.push(page);
await expect(page.navigate(server.emptyPage)).resolves.toBeTruthy();
await expect(context.close()).resolves.toBe(undefined);
});
});

describe('emulator', () => {
Expand Down Expand Up @@ -297,18 +307,16 @@ describe('BrowserContext', () => {
value: 'GRID',
},
]);
expect(await context.getCookies()).toStrictEqual([
{
name: 'gridcookie',
value: 'GRID',
domain: 'localhost',
path: '/grid.html',
expires: '-1',
secure: false,
httpOnly: false,
sameSite: 'None',
},
]);
expect((await context.getCookies())[0]).toMatchObject({
name: 'gridcookie',
value: 'GRID',
domain: 'localhost',
path: '/grid.html',
expires: '-1',
secure: false,
httpOnly: false,
sameSite: 'None',
});
expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID');
await page.goto(server.emptyPage);
expect(await page.evaluate('document.cookie')).toBe('');
Expand Down Expand Up @@ -370,18 +378,18 @@ describe('BrowserContext', () => {
return document.cookie;
})()`);
expect(documentCookie).toBe('username=John Doe');
expect(await context.getCookies()).toEqual([
{
name: 'username',
value: 'John Doe',
domain: 'localhost',
path: '/',
expires: '-1',
httpOnly: false,
secure: false,
sameSite: 'None',
},
]);
const cookies = await context.getCookies();
expect(cookies).toHaveLength(1);
expect(cookies[0]).toMatchObject({
name: 'username',
value: 'John Doe',
domain: 'localhost',
path: '/',
expires: '-1',
httpOnly: false,
secure: false,
sameSite: 'None',
});
});

it('should get a non-session cookie', async () => {
Expand All @@ -394,18 +402,18 @@ describe('BrowserContext', () => {
return document.cookie;
})()`);
expect(documentCookie).toBe('username=John Doe');
expect(await context.getCookies()).toEqual([
{
name: 'username',
value: 'John Doe',
domain: 'localhost',
path: '/',
expires: String(date / 1000),
httpOnly: false,
secure: false,
sameSite: 'None',
},
]);
const cookies = await context.getCookies();
expect(cookies).toHaveLength(1);
expect(cookies[0]).toMatchObject({
name: 'username',
value: 'John Doe',
domain: 'localhost',
path: '/',
expires: String(date / 1000),
httpOnly: false,
secure: false,
sameSite: 'None',
});
});

it('should properly report "Strict" sameSite cookie', async () => {
Expand Down Expand Up @@ -450,18 +458,17 @@ describe('BrowserContext', () => {
]);
const cookies = await context.getCookies(new URL('https://foo.com'));
cookies.sort((a, b) => a.name.localeCompare(b.name));
expect(cookies).toEqual([
{
name: 'doggo',
value: 'woofs',
domain: 'foo.com',
path: '/',
expires: '-1',
httpOnly: false,
secure: true,
sameSite: 'None',
},
]);
expect(cookies).toHaveLength(1);
expect(cookies[0]).toMatchObject({
name: 'doggo',
value: 'woofs',
domain: 'foo.com',
path: '/',
expires: '-1',
httpOnly: false,
secure: true,
sameSite: 'None',
});
});
});
});
6 changes: 3 additions & 3 deletions puppet/test/Page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Pages', () => {

beforeAll(async () => {
Core.use(CustomBrowserEmulator);
const { browserEngine } = CustomBrowserEmulator.selectBrowserMeta();
const { browserEngine } = CustomBrowserEmulator.selectBrowserMeta();
server = await TestServer.create(0);
puppet = new Puppet(browserEngine);
await puppet.start();
Expand Down Expand Up @@ -78,14 +78,14 @@ describe('Pages', () => {
await expect(message.frameId).toBe(page.mainFrame.id);
});

it('should set the page close state', async () => {
it('page.close should set the page close state', async () => {
const newPage = await context.newPage();
expect(newPage.isClosed).toBe(false);
await newPage.close();
expect(newPage.isClosed).toBe(true);
});

it('should be callable twice', async () => {
it('page.close should be callable twice', async () => {
const newPage = await context.newPage();
await Promise.all([newPage.close(), newPage.close()]);
await expect(newPage.close()).resolves.not.toBeTruthy();
Expand Down

0 comments on commit 9ab8bc9

Please sign in to comment.