2025-12-03 15:51:44 +01:00
import { test , expect } from './utils/fixtures' ;
2026-07-07 10:50:24 +02:00
import { clickByLabel , waitForWxApp , stableShot } from './utils/element-tracker' ;
2025-12-03 15:51:44 +01:00
/ * *
* wxPrinting Tests
*
* KiCad uses wxPrinting for :
* - Schematic printing
* - PCB printing
* - Export to PDF
*
* Layout ( from button finder ) :
* - Description text at top
* - Buttons at y = 95 :
* - Print Preview : x = 425
* - Print... : x = 550
* - Browser Print : x = 685
* - Page Setup : x = 755
* - Document preview panel
* - Event log at bottom
* /
test . describe ( 'wxPrinting Tests' , ( ) = > {
test . beforeEach ( async ( { page } ) = > {
await page . goto ( '/standalone/print/print_test.html' ) ;
2026-07-07 10:50:24 +02:00
// Deterministic app-readiness: canvas visible + element registry populated
await waitForWxApp ( page ) ;
2025-12-03 15:51:44 +01:00
} ) ;
test ( 'Print test app loads successfully' , async ( { page , testLogger } ) = > {
const hasStartupLog = testLogger . consoleLogs . some ( log = >
log . includes ( 'PRINT_TEST' ) && log . includes ( 'started successfully' )
) ;
2026-07-07 10:50:24 +02:00
await stableShot ( page , 'print-01-loaded.png' ) ;
2025-12-03 15:51:44 +01:00
expect ( testLogger . errors . filter ( e = > ! e . includes ( 'favicon' ) ) ) . toHaveLength ( 0 ) ;
} ) ;
test ( 'Document preview panel renders' , async ( { page } ) = > {
// Take screenshot to verify preview panel shows document content
2026-07-07 10:50:24 +02:00
await stableShot ( page , 'print-02-preview-panel.png' ) ;
2025-12-03 15:51:44 +01:00
// Visual verification - preview panel should show shapes and text
} ) ;
test ( 'Browser Print button triggers window.print()' , async ( { page , testLogger } ) = > {
// Mock window.print to track calls
await page . evaluate ( ( ) = > {
( window as any ) . originalPrint = window . print ;
( window as any ) . printWasCalled = false ;
window . print = ( ) = > {
console . log ( '[TEST] window.print() was called' ) ;
( window as any ) . printWasCalled = true ;
} ;
} ) ;
2025-12-30 14:17:51 +01:00
// Click Browser Print button using element registry
await clickByLabel ( page , 'Browser Print' ) ;
2025-12-03 15:51:44 +01:00
2026-07-07 10:50:24 +02:00
// Either the browser-print log appears or window.print was called
await expect . poll ( async ( ) = > {
const printWasCalled = await page . evaluate ( ( ) = > ( window as any ) . printWasCalled ) ;
const hasBrowserPrintLog = testLogger . consoleLogs . some ( log = >
log . includes ( 'window.print' ) || log . includes ( 'browser print' )
) ;
return hasBrowserPrintLog || printWasCalled ;
} , { message : 'Browser Print should trigger window.print() or log it' } ) . toBe ( true ) ;
2025-12-03 15:51:44 +01:00
2026-07-07 10:50:24 +02:00
await stableShot ( page , 'print-03-browser-print-clicked.png' ) ;
2025-12-03 15:51:44 +01:00
// Restore original print function
await page . evaluate ( ( ) = > {
window . print = ( window as any ) . originalPrint ;
} ) ;
} ) ;
test ( 'Print Preview button works' , async ( { page , testLogger } ) = > {
2025-12-30 14:17:51 +01:00
// Click Print Preview button using element registry
await clickByLabel ( page , 'Print Preview' ) ;
2025-12-03 15:51:44 +01:00
2026-07-07 10:50:24 +02:00
await stableShot ( page , 'print-04-preview-clicked.png' ) ;
2025-12-03 15:51:44 +01:00
// Check for print preview events
const hasPreviewLog = testLogger . consoleLogs . some ( log = >
log . includes ( 'Print Preview' ) || log . includes ( 'PRINTOUT_CALLBACK' )
) ;
// Print preview may open a new frame or log messages
// Either preview opens successfully or we get an error logged
} ) ;
test ( 'Page Setup button works' , async ( { page , testLogger } ) = > {
2025-12-30 14:17:51 +01:00
// Click Page Setup button using element registry
await clickByLabel ( page , 'Page Setup' ) ;
2025-12-03 15:51:44 +01:00
2026-07-07 10:50:24 +02:00
await stableShot ( page , 'print-05-page-setup-clicked.png' ) ;
2025-12-03 15:51:44 +01:00
// Check for page setup events
const hasPageSetupLog = testLogger . consoleLogs . some ( log = >
log . includes ( 'Page Setup' ) || log . includes ( 'page setup' )
) ;
} ) ;
test ( 'Print button works' , async ( { page , testLogger } ) = > {
2025-12-30 14:17:51 +01:00
// Click Print... button using element registry
await clickByLabel ( page , 'Print...' ) ;
2025-12-03 15:51:44 +01:00
2026-07-07 10:50:24 +02:00
await stableShot ( page , 'print-06-print-clicked.png' ) ;
2025-12-03 15:51:44 +01:00
// Check for print dialog events
const hasPrintLog = testLogger . consoleLogs . some ( log = >
log . includes ( 'Print dialog' ) || log . includes ( 'Opening Print' )
) ;
} ) ;
test ( 'Printout callbacks are triggered' , async ( { page , testLogger } ) = > {
2025-12-30 14:17:51 +01:00
// Try to trigger print preview to see callbacks
await clickByLabel ( page , 'Print Preview' ) ;
2025-12-03 15:51:44 +01:00
2026-01-01 20:18:19 +01:00
// Verify printout callbacks were triggered
2026-07-07 10:50:24 +02:00
await expect . poll ( ( ) = >
testLogger . consoleLogs . filter ( log = > log . includes ( 'PRINTOUT_CALLBACK' ) ) . length ,
{ message : 'Print Preview should trigger PRINTOUT_CALLBACK logs' }
) . toBeGreaterThan ( 0 ) ;
await stableShot ( page , 'print-07-callbacks.png' ) ;
2025-12-03 15:51:44 +01:00
} ) ;
test ( 'No JavaScript errors during print operations' , async ( { page , testLogger } ) = > {
2025-12-30 14:17:51 +01:00
// Button labels to click
const buttonLabels = [ 'Print Preview' , 'Print...' , 'Browser Print' , 'Page Setup' ] ;
2025-12-03 15:51:44 +01:00
// Mock window.print to prevent actual print dialog
await page . evaluate ( ( ) = > {
( window as any ) . originalPrint = window . print ;
window . print = ( ) = > console . log ( '[MOCK] window.print() called' ) ;
} ) ;
2025-12-30 14:17:51 +01:00
// Click each button using element registry
for ( const label of buttonLabels ) {
await clickByLabel ( page , label ) ;
2026-07-07 10:50:24 +02:00
await page . waitForTimeout ( 500 ) ; // eslint-disable-line -- documented interaction dwell: let each button's dialog/preview settle before clicking the next; no single uniform per-button observable to poll
2025-12-03 15:51:44 +01:00
}
2026-07-07 10:50:24 +02:00
await stableShot ( page , 'print-08-all-buttons.png' ) ;
2025-12-03 15:51:44 +01:00
// Restore window.print
await page . evaluate ( ( ) = > {
window . print = ( window as any ) . originalPrint ;
} ) ;
// Filter out favicon and any expected errors
const realErrors = testLogger . errors . filter ( e = >
! e . includes ( 'favicon' ) &&
! e . includes ( 'printer error' ) && // Expected if no printer configured
! e . includes ( 'not available' )
) ;
expect ( realErrors ) . toHaveLength ( 0 ) ;
} ) ;
} ) ;