1+ import { Page , expect } from '@playwright/test' ;
2+
3+ const DIR = './public/' ;
4+
5+ /**
6+ * Upload a sample epub or pdf
7+ */
8+ export async function uploadFile ( page : Page , filePath : string ) {
9+ await page . waitForSelector ( 'input[type=file]' , { timeout : 10000 } ) ;
10+ await page . setInputFiles ( 'input[type=file]' , `${ DIR } ${ filePath } ` ) ;
11+ }
12+
13+ /**
14+ * Upload and display a document
15+ */
16+ export async function uploadAndDisplay ( page : Page , fileName : string ) {
17+ // Upload the file
18+ await uploadFile ( page , fileName ) ;
19+
20+ // Wait for link with document-link class
21+ const size = fileName . endsWith ( '.pdf' ) ? '0.02 MB' : '0.33 MB' ;
22+ await page . getByRole ( 'link' , { name : `${ fileName } ${ size } ` } ) . click ( ) ;
23+
24+ // Wait for the document to load
25+ if ( fileName . endsWith ( '.pdf' ) ) {
26+ await page . waitForSelector ( '.react-pdf__Document' , { timeout : 10000 } ) ;
27+ }
28+ else if ( fileName . endsWith ( '.epub' ) ) {
29+ await page . waitForSelector ( '.epub-container' , { timeout : 10000 } ) ;
30+ }
31+ }
32+
33+ /**
34+ * Wait for the play button to be clickable and click it
35+ */
36+ export async function waitAndClickPlay ( page : Page ) {
37+ // Wait for play button selector without disabled attribute
38+ await expect ( page . getByRole ( 'button' , { name : 'Play' } ) ) . toBeVisible ( ) ;
39+ // Play the TTS by clicking the button
40+ await page . getByRole ( 'button' , { name : 'Play' } ) . click ( ) ;
41+
42+ // Expect for buttons to be disabled
43+ await expect ( page . locator ( 'button[aria-label="Skip forward"][disabled]' ) ) . toBeVisible ( ) ;
44+ await expect ( page . locator ( 'button[aria-label="Skip backward"][disabled]' ) ) . toBeVisible ( ) ;
45+
46+ // Wait for the TTS to stop processing
47+ await Promise . all ( [
48+ page . waitForSelector ( 'button[aria-label="Skip forward"]:not([disabled])' , { timeout : 45000 } ) ,
49+ page . waitForSelector ( 'button[aria-label="Skip backward"]:not([disabled])' , { timeout : 45000 } ) ,
50+ ] ) ;
51+
52+ await page . waitForFunction ( ( ) => {
53+ return navigator . mediaSession ?. playbackState === 'playing' ;
54+ } ) ;
55+ }
56+
57+ /**
58+ * Setup function for TTS playback tests
59+ */
60+ export async function playTTSAndWaitForASecond ( page : Page , fileName : string ) {
61+ // Upload and display the document
62+ await uploadAndDisplay ( page , fileName ) ;
63+ // Wait for play button selector without disabled attribute
64+ await waitAndClickPlay ( page ) ;
65+ // play for 1s
66+ await page . waitForTimeout ( 1000 ) ;
67+ }
68+
69+ /**
70+ * Common test setup function
71+ */
72+ export async function setupTest ( page : Page ) {
73+ // Navigate to the home page before each test
74+ await page . goto ( '/' ) ;
75+ await page . waitForLoadState ( 'networkidle' ) ;
76+
77+ // Click the "done" button to dismiss the welcome message
78+ await page . getByText ( 'Done' ) . click ( ) ;
79+ }
0 commit comments