Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/plugins/plugin-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { PluginManager } from '../../src/plugins';
jest.mock('../../src/plugins/network');

describe('plugin manager', () => {
const pluginManager = new PluginManager({} as any);
const pluginManager = new PluginManager({ context: {} } as any);

it('register plugin by type', async () => {
await pluginManager.register('network', {});
Expand Down
23 changes: 23 additions & 0 deletions src/common_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export type NetworkConditions = {
latency: number;
};

export type APIDriver = {
request: APIRequestContext;
};

export type Driver = {
browser: ChromiumBrowser;
context: ChromiumBrowserContext;
Expand Down Expand Up @@ -252,6 +256,13 @@ export type RunOptions = BaseArgs & {
grepOpts?: GrepOptions;
};

export type APIRunOptions = BaseArgs & {
network?: boolean;
environment?: string;
reporter?: BuiltInReporterName | ReporterInstance;
grepOpts?: GrepOptions;
};

export type PushOptions = Partial<ProjectSettings> &
Partial<BaseArgs> & {
auth: string;
Expand Down Expand Up @@ -285,6 +296,12 @@ export type SyntheticsConfig = {
project?: ProjectSettings;
};

/** Runner Payload types */
export type APIJourneyResult = Partial<Journey> & {
networkinfo?: PluginOutput['networkinfo'];
stepsresults?: Array<StepResult>;
};

/** Runner Payload types */
export type JourneyResult = Partial<Journey> & {
networkinfo?: PluginOutput['networkinfo'];
Expand Down Expand Up @@ -324,4 +341,10 @@ export type JourneyEndResult = JourneyStartResult &
options: RunOptions;
};

export type APIJourneyEndResult = JourneyStartResult &
APIJourneyResult & {
browserDelay: number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

browserDelay is not appropriate for apijourneys?

options: RunOptions;
};

export type StepEndResult = StepResult;
91 changes: 60 additions & 31 deletions src/core/gatherer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ import {
} from 'playwright-core';
import { PluginManager } from '../plugins';
import { log } from './logger';
import { Driver, NetworkConditions, RunOptions } from '../common_types';
import {
APIDriver,
Driver,
NetworkConditions,
RunOptions,
} from '../common_types';

// Default timeout for Playwright actions and Navigations
const DEFAULT_TIMEOUT = 50000;
Expand Down Expand Up @@ -71,32 +76,48 @@ export class Gatherer {
});
}

static async setupDriver(options: RunOptions): Promise<Driver> {
await Gatherer.launchBrowser(options);
static async setupDriver<T extends 'browser' | 'api' = 'browser'>(
options: RunOptions,
journeyType: T = 'browser' as T
): Promise<T extends 'browser' ? Driver : APIDriver> {
const { playwrightOptions } = options;
const context = await Gatherer.browser.newContext({
...playwrightOptions,
userAgent: await Gatherer.getUserAgent(playwrightOptions?.userAgent),
});
// Set timeouts for actions and navigations
context.setDefaultTimeout(
playwrightOptions?.actionTimeout ?? DEFAULT_TIMEOUT
);
context.setDefaultNavigationTimeout(
playwrightOptions?.navigationTimeout ?? DEFAULT_TIMEOUT
);

// TODO: Network throttling via chrome devtools emulation is disabled for now.
// See docs/throttling.md for more details.
// Gatherer.setNetworkConditions(context, networkConditions);
if (playwrightOptions?.testIdAttribute) {
selectors.setTestIdAttribute(playwrightOptions.testIdAttribute);
}

const page = await context.newPage();
const client = await context.newCDPSession(page);
const request = await apiRequest.newContext({ ...playwrightOptions });
return { browser: Gatherer.browser, context, page, client, request };
if (journeyType === 'browser') {
await Gatherer.launchBrowser(options);

const context = await Gatherer.browser.newContext({
...playwrightOptions,
userAgent: await Gatherer.getUserAgent(playwrightOptions?.userAgent),
});
// Set timeouts for actions and navigations
context.setDefaultTimeout(
playwrightOptions?.actionTimeout ?? DEFAULT_TIMEOUT
);
context.setDefaultNavigationTimeout(
playwrightOptions?.navigationTimeout ?? DEFAULT_TIMEOUT
);

// TODO: Network throttling via chrome devtools emulation is disabled for now.
// See docs/throttling.md for more details.
// Gatherer.setNetworkConditions(context, networkConditions);
if (playwrightOptions?.testIdAttribute) {
selectors.setTestIdAttribute(playwrightOptions.testIdAttribute);
}

const page = await context.newPage();
const client = await context.newCDPSession(page);
const request = await apiRequest.newContext({ ...playwrightOptions });
return {
browser: Gatherer.browser,
context,
page,
client,
request,
} as Driver;
} else {
const request = await apiRequest.newContext({ ...playwrightOptions });
return { request } as T extends 'browser' ? Driver : APIDriver;
}
}

static async getUserAgent(userAgent?: string) {
Expand Down Expand Up @@ -136,14 +157,20 @@ export class Gatherer {
* Starts recording all events related to the v8 devtools protocol
* https://chromedevtools.github.io/devtools-protocol/v8/
*/
static async beginRecording(driver: Driver, options: RunOptions) {
static async beginRecording(driver: Driver | APIDriver, options: RunOptions) {
log('Gatherer: started recording');
const { network, metrics } = options;
Gatherer.pluginManager = new PluginManager(driver);
Gatherer.pluginManager.registerAll(options);
const plugins = [await Gatherer.pluginManager.start('browserconsole')];
network && plugins.push(await Gatherer.pluginManager.start('network'));
metrics && plugins.push(await Gatherer.pluginManager.start('performance'));
const plugins = [];
if ('browser' in driver) {
plugins.push(await Gatherer.pluginManager.start('browserconsole'));
network && plugins.push(await Gatherer.pluginManager.start('network'));
metrics &&
plugins.push(await Gatherer.pluginManager.start('performance'));
} else {
plugins.push(await Gatherer.pluginManager.start('network'));
}
await Promise.all(plugins);
return Gatherer.pluginManager;
}
Expand All @@ -153,10 +180,12 @@ export class Gatherer {
await Gatherer.pluginManager.unregisterAll();
}

static async dispose(driver: Driver) {
static async dispose(driver: Driver | APIDriver) {
log(`Gatherer: closing all contexts`);
await driver.request.dispose();
await driver.context.close();
if ('context' in driver) {
await driver.context.close();
}
}

static async stop() {
Expand Down
2 changes: 1 addition & 1 deletion src/core/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import Runner from './runner';

/**
* Use a gloabl Runner which would be accessed by the runtime and
* Use a global Runner which would be accessed by the runtime and
* required to handle the local vs global invocation through CLI
*/
const SYNTHETICS_RUNNER = Symbol.for('SYNTHETICS_RUNNER');
Expand Down
26 changes: 26 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/

import {
APIJourney,
APIJourneyCallback,
APIJourneyOptions,
APIJourneyWithAnnotations,
Journey,
JourneyCallback,
JourneyOptions,
Expand Down Expand Up @@ -60,6 +64,28 @@ export const journey = createJourney() as JourneyWithAnnotations;
journey.skip = createJourney('skip');
journey.only = createJourney('only');

const createAPIJourney = (type?: 'skip' | 'only') =>
wrapFnWithLocation(
(
location: Location,
options: APIJourneyOptions | string,
callback: APIJourneyCallback
) => {
log(`API Journey register: ${JSON.stringify(options)}`);
if (typeof options === 'string') {
options = { name: options, id: options };
}
const j = new APIJourney({ ...options, type: 'api' }, callback, location);
if (type) {
j[type] = true;
}
runner._addJourney(j);
return j;
}
);

export const apiJourney = createAPIJourney() as APIJourneyWithAnnotations;

const createStep = (type?: 'skip' | 'soft' | 'only') =>
wrapFnWithLocation(
(location: Location, name: string, callback: VoidCallback) => {
Expand Down
Loading
Loading