11import { chromium , firefox , Locator , webkit , type Browser , type BrowserContext , type Page } from 'playwright' ;
2- import { normalizeTags , QuickPickleWorld , QuickPickleWorldInterface } from 'quickpickle' ;
2+ import { normalizeTags , VisualWorld , VisualWorldInterface , ScreenshotComparisonOptions , AriaRoleExtended } from 'quickpickle' ;
33import { After } from 'quickpickle' ;
44import type { TestContext } from 'vitest' ;
55import { defaultsDeep } from 'lodash-es'
66import { InfoConstructor } from 'quickpickle/dist/world' ;
7+ import { Buffer } from 'buffer' ;
78
89import { expect } from '@playwright/test' ;
910import { ScreenshotSetting } from './snapshotMatcher' ;
@@ -56,7 +57,7 @@ export type PlaywrightWorldConfig = typeof defaultPlaywrightWorldConfig & {
5657 browserSizes : Record < string , string >
5758}
5859
59- export class PlaywrightWorld extends QuickPickleWorld {
60+ export class PlaywrightWorld extends VisualWorld implements VisualWorldInterface {
6061 browser ! : Browser
6162 browserContext ! : BrowserContext
6263 page ! : Page
@@ -161,25 +162,6 @@ export class PlaywrightWorld extends QuickPickleWorld {
161162 return this . worldConfig
162163 }
163164
164- get screenshotDir ( ) {
165- return this . sanitizePath ( this . worldConfig . screenshotDir )
166- }
167-
168- get screenshotPath ( ) {
169- return this . fullPath ( `${ this . worldConfig . screenshotDir } /${ this . screenshotFilename } ` )
170- }
171-
172- get screenshotFilename ( ) {
173- return `${ this . toString ( ) . replace ( / ^ .+ ?F e a t u r e : / , 'Feature: ' ) . replace ( ' ' + this . info . step , '' ) } .png`
174- }
175-
176- /**
177- * @deprecated Use `screenshotPath` instead
178- */
179- get fullScreenshotPath ( ) {
180- return this . screenshotPath
181- }
182-
183165 /**
184166 * Gets a locator based on a certain logic
185167 * @example getLocator(page, 'Cancel', 'button') => page.getByRole('button', { name: 'Cancel' })
@@ -192,7 +174,7 @@ export class PlaywrightWorld extends QuickPickleWorld {
192174 * @param text Optional text to match inside the locator
193175 * @returns Promise<void>
194176 */
195- getLocator ( el :Locator | Page , identifier :string , role :string | 'element' | 'input' , text :string | null = null ) {
177+ getLocator ( el :Locator | Page , identifier :string , role :AriaRoleExtended , text :string | null = null ) {
196178 let locator :Locator
197179 if ( role === 'element' ) locator = el . locator ( identifier )
198180 else if ( role === 'input' ) locator = el . getByLabel ( identifier ) . or ( el . getByPlaceholder ( identifier ) )
@@ -240,7 +222,7 @@ export class PlaywrightWorld extends QuickPickleWorld {
240222 * @param px The number of pixels to scroll (defaults to 100)
241223 * @returns Promise<void>
242224 */
243- async scroll ( direction :"up" | "down" | "left" | "right" , px = 100 ) {
225+ async scroll ( locator : Locator | Page , direction :"up" | "down" | "left" | "right" , px = 100 ) {
244226 let horiz = direction . includes ( 't' )
245227 if ( horiz ) await this . page . mouse . wheel ( direction === 'right' ? px : - px , 0 )
246228 else await this . page . mouse . wheel ( 0 , direction === 'down' ? px : - px )
@@ -322,14 +304,15 @@ export class PlaywrightWorld extends QuickPickleWorld {
322304 }
323305 }
324306
325- async screenshot ( opts ?:{
326- name ?:string
327- locator ?:Locator
328- } ) {
307+ async screenshot ( opts ?:{ name ?:string , locator ?:any } ) :Promise < Buffer > {
329308 let explodedTags = this . info . explodedIdx ? `_(${ this . info . tags . join ( ',' ) } )` : ''
330309 let path = opts ?. name ? this . fullPath ( `${ this . screenshotDir } /${ opts . name } ${ explodedTags } .png` ) : this . screenshotPath
331310 let locator = opts ?. locator ?? this . page
332- return await locator . screenshot ( { path, ...this . worldConfig . screenshotOpts } )
311+ return await locator . screenshot ( { path, ...this . screenshotOptions } )
312+ }
313+
314+ async expectScreenshotMatch ( locator :Locator | Page , screenshotName :string , options ?:Partial < ScreenshotComparisonOptions > ) :Promise < void > {
315+ await expect ( locator ) . toMatchScreenshot ( screenshotName , defaultsDeep ( options || { } , this . screenshotOptions ) )
333316 }
334317
335318}
0 commit comments