Skip to content

Commit c2763fe

Browse files
authored
feat: clock.uninstall (microsoft#36856)
1 parent 809ea3c commit c2763fe

File tree

12 files changed

+51
-3
lines changed

12 files changed

+51
-3
lines changed

docs/src/api/class-clock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,8 @@ Time to be set.
340340
- `time` <[string]|[Date]>
341341

342342
Time to be set.
343+
344+
## async method: Clock.uninstall
345+
* since: v1.55
346+
347+
Uninstall fake clock. Note that any currently open page will be still affected by the fake clock, until it navigates away to a new document.

packages/playwright-client/types/types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18798,6 +18798,12 @@ export interface Clock {
1879818798
* @param time Time to be set in milliseconds.
1879918799
*/
1880018800
setSystemTime(time: number|string|Date): Promise<void>;
18801+
18802+
/**
18803+
* Uninstall fake clock. Note that any currently open page will be still affected by the fake clock, until it
18804+
* navigates away to a new document.
18805+
*/
18806+
uninstall(): Promise<void>;
1880118807
}
1880218808

1880318809
/**

packages/playwright-core/src/client/clock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export class Clock implements api.Clock {
5151
async setSystemTime(time: string | number | Date) {
5252
await this._browserContext._channel.clockSetSystemTime(parseTime(time));
5353
}
54+
55+
async uninstall() {
56+
await this._browserContext._channel.clockUninstall();
57+
}
5458
}
5559

5660
function parseTime(time: string | number | Date): { timeNumber?: number, timeString?: string } {

packages/playwright-core/src/protocol/validator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,8 @@ scheme.BrowserContextClockSetSystemTimeParams = tObject({
11581158
timeString: tOptional(tString),
11591159
});
11601160
scheme.BrowserContextClockSetSystemTimeResult = tOptional(tObject({}));
1161+
scheme.BrowserContextClockUninstallParams = tOptional(tObject({}));
1162+
scheme.BrowserContextClockUninstallResult = tOptional(tObject({}));
11611163
scheme.PageInitializer = tObject({
11621164
mainFrame: tChannel(['Frame']),
11631165
viewportSize: tOptional(tObject({

packages/playwright-core/src/server/browserContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export abstract class BrowserContext extends SdkObject {
210210
// Note: we only need to reset properties from the "paramsThatAllowContextReuse" list.
211211
// All other properties force a new context.
212212
await this._resetStorage(progress);
213-
await progress.race(this.clock.resetForReuse());
213+
await this.clock.uninstall(progress);
214214
await progress.race(this.setUserAgent(this._options.userAgent));
215215
await progress.race(this.clearCache());
216216
await progress.race(this.doClearCookies());

packages/playwright-core/src/server/clock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export class Clock {
2828
this._browserContext = browserContext;
2929
}
3030

31-
async resetForReuse() {
32-
await this._browserContext.removeInitScripts(this._initScripts);
31+
async uninstall(progress: Progress) {
32+
await progress.race(this._browserContext.removeInitScripts(this._initScripts));
3333
this._initScripts = [];
3434
}
3535

packages/playwright-core/src/server/dispatchers/browserContextDispatcher.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
400400
await this._context.clock.setSystemTime(progress, params.timeString ?? params.timeNumber ?? 0);
401401
}
402402

403+
async clockUninstall(params: channels.BrowserContextClockUninstallParams, progress: Progress): Promise<channels.BrowserContextClockUninstallResult> {
404+
await this._context.clock.uninstall(progress);
405+
}
406+
403407
async updateSubscription(params: channels.BrowserContextUpdateSubscriptionParams, progress: Progress): Promise<void> {
404408
if (params.enabled)
405409
this._subscriptions.add(params.event);

packages/playwright-core/src/utils/isomorphic/protocolMetainfo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export const methodMetainfo = new Map<string, { internal?: boolean, title?: stri
9797
['BrowserContext.clockRunFor', { title: 'Run clock "{ticksNumber}{ticksString}"', }],
9898
['BrowserContext.clockSetFixedTime', { title: 'Set fixed time "{timeNumber}{timeString}"', }],
9999
['BrowserContext.clockSetSystemTime', { title: 'Set system time "{timeNumber}{timeString}"', }],
100+
['BrowserContext.clockUninstall', { title: 'Uninstall clock', }],
100101
['Page.addInitScript', { }],
101102
['Page.close', { title: 'Close page', }],
102103
['Page.emulateMedia', { title: 'Emulate media', snapshot: true, }],

packages/playwright-core/types/types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18798,6 +18798,12 @@ export interface Clock {
1879818798
* @param time Time to be set in milliseconds.
1879918799
*/
1880018800
setSystemTime(time: number|string|Date): Promise<void>;
18801+
18802+
/**
18803+
* Uninstall fake clock. Note that any currently open page will be still affected by the fake clock, until it
18804+
* navigates away to a new document.
18805+
*/
18806+
uninstall(): Promise<void>;
1880118807
}
1880218808

1880318809
/**

packages/protocol/src/channels.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,7 @@ export interface BrowserContextChannel extends BrowserContextEventTarget, EventT
16271627
clockRunFor(params: BrowserContextClockRunForParams, progress?: Progress): Promise<BrowserContextClockRunForResult>;
16281628
clockSetFixedTime(params: BrowserContextClockSetFixedTimeParams, progress?: Progress): Promise<BrowserContextClockSetFixedTimeResult>;
16291629
clockSetSystemTime(params: BrowserContextClockSetSystemTimeParams, progress?: Progress): Promise<BrowserContextClockSetSystemTimeResult>;
1630+
clockUninstall(params?: BrowserContextClockUninstallParams, progress?: Progress): Promise<BrowserContextClockUninstallResult>;
16301631
}
16311632
export type BrowserContextBindingCallEvent = {
16321633
binding: BindingCallChannel,
@@ -2005,6 +2006,9 @@ export type BrowserContextClockSetSystemTimeOptions = {
20052006
timeString?: string,
20062007
};
20072008
export type BrowserContextClockSetSystemTimeResult = void;
2009+
export type BrowserContextClockUninstallParams = {};
2010+
export type BrowserContextClockUninstallOptions = {};
2011+
export type BrowserContextClockUninstallResult = void;
20082012

20092013
export interface BrowserContextEvents {
20102014
'bindingCall': BrowserContextBindingCallEvent;

0 commit comments

Comments
 (0)