Skip to content
Open
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
22 changes: 21 additions & 1 deletion packages/injected/src/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,11 @@ function platformOriginals(globalObject: WindowOrWorkerGlobalScope): { raw: Buil
Date: (globalObject as any).Date,
performance: globalObject.performance,
Intl: (globalObject as any).Intl,
AbortSignal: (globalObject as any).AbortSignal,
};
const bound = { ...raw };
for (const key of Object.keys(bound) as (keyof Builtins)[]) {
if (key !== 'Date' && typeof bound[key] === 'function')
if (key !== 'Date' && key !== 'AbortSignal' && typeof bound[key] === 'function')
bound[key] = (bound[key] as any).bind(globalObject);
}
return { raw, bound };
Expand Down Expand Up @@ -688,6 +689,7 @@ function createApi(clock: ClockController, originals: Builtins): Builtins {
Intl: originals.Intl ? createIntl(clock, originals.Intl) : (undefined as unknown as Builtins['Intl']),
Date: createDate(clock, originals.Date),
performance: originals.performance ? fakePerformance(clock, originals.performance) : (undefined as unknown as Builtins['performance']),
AbortSignal: originals.AbortSignal ? fakeAbortSignal(clock, originals.AbortSignal) : (undefined as unknown as Builtins['AbortSignal']),
};
}

Expand Down Expand Up @@ -715,6 +717,22 @@ function fakePerformance(clock: ClockController, performance: Builtins['performa
return result;
}

function fakeAbortSignal(clock: ClockController, abortSignal: Builtins['AbortSignal']): Builtins['AbortSignal'] {
const result: any = {
...abortSignal,
timeout: (ms: number) => {
const controller = new AbortController();
clock.addTimer({
delay: ms,
type: TimerType.Timeout,
func: () => controller.abort(new DOMException('This operation was aborted', 'AbortError')),
});
return controller.signal;
},
};
return result;
}

export function createClock(globalObject: WindowOrWorkerGlobalScope): { clock: ClockController, api: Builtins, originals: Builtins } {
const originals = platformOriginals(globalObject);
const embedder: Embedder = {
Expand Down Expand Up @@ -750,6 +768,8 @@ export function install(globalObject: WindowOrWorkerGlobalScope, config: Install
(globalObject as any).Date = mirrorDateProperties(api.Date, (globalObject as any).Date);
} else if (method === 'Intl') {
(globalObject as any).Intl = api[method]!;
} else if (method === 'AbortSignal') {
(globalObject as any).AbortSignal = api[method]!;
} else if (method === 'performance') {
(globalObject as any).performance = api[method]!;
const kEventTimeStamp = Symbol('playwrightEventTimeStamp');
Expand Down
2 changes: 2 additions & 0 deletions packages/injected/src/utilityScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type Builtins = {
performance: Window['performance'],
Intl: typeof window['Intl'],
Date: typeof window['Date'],
AbortSignal: typeof window['AbortSignal'],
};

export class UtilityScript {
Expand All @@ -55,6 +56,7 @@ export class UtilityScript {
performance: global.performance,
Intl: global.Intl,
Date: global.Date,
AbortSignal: global.AbortSignal,
} satisfies Builtins;
}
if (this.isUnderTest)
Expand Down
37 changes: 37 additions & 0 deletions tests/library/page-clock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,43 @@ it.describe('Date.now', () => {
});
});

it('AbortSignal.timeout', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/39293' } }, async ({ page }) => {
await page.clock.install({ time: 0 });
const controller = await page.evaluateHandle(() => {
const signal = AbortSignal.any([
AbortSignal.timeout(100)
]);
const handle = {
signal,
event: false,
handler: false,
};
signal.addEventListener('abort', () => handle.event = true);
signal.onabort = () => handle.handler = true;
return handle;
});
expect(await controller.evaluate(handle => ({
signal: handle.signal.aborted,
event: handle.event,
handler: handle.handler,
}))).toEqual({
signal: false,
event: false,
handler: false,
});
await page.clock.runFor(200);
expect(await controller.evaluate(handle => ({
signal: handle.signal.aborted,
event: handle.event,
handler: handle.handler,
}))).toEqual({
signal: true,
event: true,
handler: true,
});
expect(await page.evaluate(() => AbortSignal.abort().aborted)).toBe(true);
});

it('correctly increments Date.now()/performance.now() during blocking execution', {
annotation: {
type: 'issue',
Expand Down
Loading