@serenity-js/playwright
Version:
Adapter that integrates @serenity-js/web with Playwright, enabling Serenity/JS reporting and using the Screenplay Pattern to write component and end-to-end test scenarios
41 lines • 1.51 kB
JavaScript
import { Timestamp } from '@serenity-js/core';
import { Cookie, CookieMissingError } from '@serenity-js/web';
import { ensure, isDefined } from 'tiny-types';
/**
* Playwright-specific implementation of [`Cookie`](https://serenity-js.org/api/web/class/Cookie/).
*
* @group Models
*/
export class PlaywrightCookie extends Cookie {
context;
constructor(context, cookieName) {
super(cookieName);
this.context = context;
ensure('context', context, isDefined());
}
async delete() {
// see https://github.com/microsoft/playwright/issues/10143
const cookies = await this.context.cookies();
await this.context.clearCookies();
await this.context.addCookies(cookies.filter(cookie => cookie.name !== this.cookieName));
}
async read() {
const cookies = await this.context.cookies();
const found = cookies.find(cookie => cookie.name === this.cookieName);
if (!found) {
throw new CookieMissingError(`Cookie '${this.cookieName}' not set`);
}
return {
name: found.name,
value: found.value,
domain: found.domain,
path: found.path,
expiry: typeof found.expires === 'number' && found.expires >= 0
? Timestamp.fromTimestampInSeconds(Math.round(found.expires))
: undefined,
httpOnly: found.httpOnly,
secure: found.secure,
};
}
}
//# sourceMappingURL=PlaywrightCookie.js.map