return timedOut bool

This commit is contained in:
Yury Semikhatsky 2024-10-25 10:58:40 -07:00
parent 70ddc7ebe2
commit d2ee0916d2
6 changed files with 15 additions and 14 deletions

View file

@ -63,6 +63,7 @@ type PDFOptions = Omit<channels.PagePdfParams, 'width' | 'height' | 'margin'> &
export type ExpectScreenshotOptions = Omit<channels.PageExpectScreenshotOptions, 'locator' | 'expected' | 'mask'> & { export type ExpectScreenshotOptions = Omit<channels.PageExpectScreenshotOptions, 'locator' | 'expected' | 'mask'> & {
expected?: Buffer, expected?: Buffer,
locator?: api.Locator, locator?: api.Locator,
timeout: number,
isNot: boolean, isNot: boolean,
mask?: api.Locator[], mask?: api.Locator[],
}; };
@ -589,7 +590,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
return result.binary; return result.binary;
} }
async _expectScreenshot(options: ExpectScreenshotOptions): Promise<{ actual?: Buffer, previous?: Buffer, diff?: Buffer, errorMessage?: string, log?: string[], timeout?: number}> { async _expectScreenshot(options: ExpectScreenshotOptions): Promise<{ actual?: Buffer, previous?: Buffer, diff?: Buffer, errorMessage?: string, log?: string[], timedOut?: boolean}> {
const mask = options?.mask ? options?.mask.map(locator => ({ const mask = options?.mask ? options?.mask.map(locator => ({
frame: (locator as Locator)._frame._channel, frame: (locator as Locator)._frame._channel,
selector: (locator as Locator)._selector, selector: (locator as Locator)._selector,

View file

@ -1165,7 +1165,7 @@ scheme.PageReloadResult = tObject({
}); });
scheme.PageExpectScreenshotParams = tObject({ scheme.PageExpectScreenshotParams = tObject({
expected: tOptional(tBinary), expected: tOptional(tBinary),
timeout: tOptional(tNumber), timeout: tNumber,
isNot: tBoolean, isNot: tBoolean,
locator: tOptional(tObject({ locator: tOptional(tObject({
frame: tChannel(['Frame']), frame: tChannel(['Frame']),
@ -1193,7 +1193,7 @@ scheme.PageExpectScreenshotResult = tObject({
errorMessage: tOptional(tString), errorMessage: tOptional(tString),
actual: tOptional(tBinary), actual: tOptional(tBinary),
previous: tOptional(tBinary), previous: tOptional(tBinary),
timeout: tOptional(tNumber), timedOut: tOptional(tBoolean),
log: tOptional(tArray(tString)), log: tOptional(tArray(tString)),
}); });
scheme.PageScreenshotParams = tObject({ scheme.PageScreenshotParams = tObject({

View file

@ -679,7 +679,7 @@ export class Page extends SdkObject {
log: e.message ? [...metadata.log, e.message] : metadata.log, log: e.message ? [...metadata.log, e.message] : metadata.log,
...intermediateResult, ...intermediateResult,
errorMessage, errorMessage,
...((e instanceof TimeoutError) ? { timeout: callTimeout } : {}), timedOut: (e instanceof TimeoutError),
}; };
}); });
} }

View file

@ -370,6 +370,7 @@ export async function toHaveScreenshot(
throw new Error(`Screenshot name "${path.basename(helper.expectedPath)}" must have '.png' extension`); throw new Error(`Screenshot name "${path.basename(helper.expectedPath)}" must have '.png' extension`);
expectTypes(pageOrLocator, ['Page', 'Locator'], 'toHaveScreenshot'); expectTypes(pageOrLocator, ['Page', 'Locator'], 'toHaveScreenshot');
const style = await loadScreenshotStyles(helper.options.stylePath); const style = await loadScreenshotStyles(helper.options.stylePath);
const timeout = helper.options.timeout ?? this.timeout;
const expectScreenshotOptions: ExpectScreenshotOptions = { const expectScreenshotOptions: ExpectScreenshotOptions = {
locator, locator,
animations: helper.options.animations ?? 'disabled', animations: helper.options.animations ?? 'disabled',
@ -382,7 +383,7 @@ export async function toHaveScreenshot(
scale: helper.options.scale ?? 'css', scale: helper.options.scale ?? 'css',
style, style,
isNot: !!this.isNot, isNot: !!this.isNot,
timeout: helper.options.timeout ?? this.timeout, timeout,
comparator: helper.options.comparator, comparator: helper.options.comparator,
maxDiffPixels: helper.options.maxDiffPixels, maxDiffPixels: helper.options.maxDiffPixels,
maxDiffPixelRatio: helper.options.maxDiffPixelRatio, maxDiffPixelRatio: helper.options.maxDiffPixelRatio,
@ -409,11 +410,11 @@ export async function toHaveScreenshot(
const receiver = locator ? 'locator' : 'page'; const receiver = locator ? 'locator' : 'page';
if (!hasSnapshot) { if (!hasSnapshot) {
// Regenerate a new screenshot by waiting until two screenshots are the same. // Regenerate a new screenshot by waiting until two screenshots are the same.
const { actual, previous, diff, errorMessage, log, timeout } = await page._expectScreenshot(expectScreenshotOptions); const { actual, previous, diff, errorMessage, log, timedOut } = await page._expectScreenshot(expectScreenshotOptions);
// We tried re-generating new snapshot but failed. // We tried re-generating new snapshot but failed.
// This can be due to e.g. spinning animation, so we want to show it as a diff. // This can be due to e.g. spinning animation, so we want to show it as a diff.
if (errorMessage) { if (errorMessage) {
const header = matcherHint(this, locator, 'toHaveScreenshot', receiver, undefined, undefined, timeout); const header = matcherHint(this, locator, 'toHaveScreenshot', receiver, undefined, undefined, timedOut ? timeout : undefined);
return helper.handleDifferent(actual, undefined, previous, diff, header, errorMessage, log); return helper.handleDifferent(actual, undefined, previous, diff, header, errorMessage, log);
} }
@ -426,7 +427,7 @@ export async function toHaveScreenshot(
// - regular matcher (i.e. not a `.not`) // - regular matcher (i.e. not a `.not`)
// - perhaps an 'all' flag to update non-matching screenshots // - perhaps an 'all' flag to update non-matching screenshots
expectScreenshotOptions.expected = await fs.promises.readFile(helper.expectedPath); expectScreenshotOptions.expected = await fs.promises.readFile(helper.expectedPath);
const { actual, previous, diff, errorMessage, log, timeout } = await page._expectScreenshot(expectScreenshotOptions); const { actual, previous, diff, errorMessage, log, timedOut } = await page._expectScreenshot(expectScreenshotOptions);
if (!errorMessage) if (!errorMessage)
return helper.handleMatching(); return helper.handleMatching();
@ -439,7 +440,7 @@ export async function toHaveScreenshot(
return helper.createMatcherResult(helper.expectedPath + ' running with --update-snapshots, writing actual.', true); return helper.createMatcherResult(helper.expectedPath + ' running with --update-snapshots, writing actual.', true);
} }
const header = matcherHint(this, undefined, 'toHaveScreenshot', receiver, undefined, undefined, timeout); const header = matcherHint(this, undefined, 'toHaveScreenshot', receiver, undefined, undefined, timedOut ? timeout : undefined);
return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log); return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log);
} }

View file

@ -2141,7 +2141,7 @@ export type PageReloadResult = {
}; };
export type PageExpectScreenshotParams = { export type PageExpectScreenshotParams = {
expected?: Binary, expected?: Binary,
timeout?: number, timeout: number,
isNot: boolean, isNot: boolean,
locator?: { locator?: {
frame: FrameChannel, frame: FrameChannel,
@ -2166,7 +2166,6 @@ export type PageExpectScreenshotParams = {
}; };
export type PageExpectScreenshotOptions = { export type PageExpectScreenshotOptions = {
expected?: Binary, expected?: Binary,
timeout?: number,
locator?: { locator?: {
frame: FrameChannel, frame: FrameChannel,
selector: string, selector: string,
@ -2193,7 +2192,7 @@ export type PageExpectScreenshotResult = {
errorMessage?: string, errorMessage?: string,
actual?: Binary, actual?: Binary,
previous?: Binary, previous?: Binary,
timeout?: number, timedOut?: boolean,
log?: string[], log?: string[],
}; };
export type PageScreenshotParams = { export type PageScreenshotParams = {

View file

@ -1482,7 +1482,7 @@ Page:
expectScreenshot: expectScreenshot:
parameters: parameters:
expected: binary? expected: binary?
timeout: number? timeout: number
isNot: boolean isNot: boolean
locator: locator:
type: object? type: object?
@ -1501,7 +1501,7 @@ Page:
errorMessage: string? errorMessage: string?
actual: binary? actual: binary?
previous: binary? previous: binary?
timeout: number? timedOut: boolean?
log: log:
type: array? type: array?
items: string items: string