MCPcopy
hub / github.com/microsoft/playwright / Session

Class Session

packages/playwright-core/src/tools/cli-client/session.ts:30–200  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28import type { MinimistArgs } from './minimist';
29
30export class Session {
31 readonly name: string;
32 readonly config: SessionConfig;
33 private _sessionFile: SessionFile;
34
35 constructor(sessionFile: SessionFile) {
36 this.config = sessionFile.config;
37 this.name = this.config.name;
38 this._sessionFile = sessionFile;
39 }
40
41 isCompatible(clientInfo: ClientInfo): boolean {
42 return compareSemver(clientInfo.version, this.config.version) >= 0;
43 }
44
45 async run(clientInfo: ClientInfo, args: MinimistArgs, options?: { raw?: boolean, json?: boolean }): Promise<{ text: string }> {
46 if (!this.isCompatible(clientInfo))
47 throw new Error(`Client is v${clientInfo.version}, session '${this.name}' is v${this.config.version}. Run\n\n playwright-cli${this.name !== 'default' ? ` -s=${this.name}` : ''} open\n\nto restart the browser session.`);
48
49 const { socket } = await this._connect();
50 if (!socket)
51 throw new Error(`Browser '${this.name}' is not open. Run\n\n playwright-cli${this.name !== 'default' ? ` -s=${this.name}` : ''} open\n\nto start the browser session.`);
52 return await SocketConnectionClient.sendAndClose(socket, 'run', { args, cwd: process.cwd(), raw: options?.raw, json: options?.json });
53 }
54
55 async stop(): Promise<{ wasOpen: boolean }> {
56 if (!await this.canConnect())
57 return { wasOpen: false };
58 await this._stopDaemon();
59 return { wasOpen: true };
60 }
61
62 async deleteData(): Promise<{ existed: boolean, deletedUserDataDir: boolean }> {
63 await this.stop();
64
65 const dataDirs = await fs.promises.readdir(this._sessionFile.daemonDir).catch(() => []);
66 const matchingEntries = dataDirs.filter(file => file === `${this.name}.session` || file.startsWith(`ud-${this.name}-`));
67 if (matchingEntries.length === 0)
68 return { existed: false, deletedUserDataDir: false };
69
70 let deletedUserDataDir = false;
71 for (const entry of matchingEntries) {
72 const userDataDir = path.resolve(this._sessionFile.daemonDir, entry);
73 for (let i = 0; i < 5; i++) {
74 try {
75 await fs.promises.rm(userDataDir, { recursive: true });
76 if (entry.startsWith('ud-'))
77 deletedUserDataDir = true;
78 break;
79 } catch (e: any) {
80 if (e.code === 'ENOENT')
81 break;
82 await new Promise(resolve => setTimeout(resolve, 1000));
83 if (i === 4)
84 throw e;
85 }
86 }
87 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…