11import { test , expect , Page } from '@playwright/test' ;
22
3+ const DIR = './public/' ;
4+
35// Upload a sample epub or pdf
46async function uploadFile ( page : Page , filePath : string ) {
5- const fileChooserPromise = page . waitForEvent ( 'filechooser' ) ;
6- await page . getByText ( 'Drop your file here, or click to select' ) . click ( ) ;
7- const fileChooser = await fileChooserPromise ;
8- await fileChooser . setFiles ( filePath ) ;
7+ await page . waitForSelector ( 'input[type=file]' , { timeout : 10000 } ) ;
8+ await page . setInputFiles ( 'input[type=file]' , `${ DIR } ${ filePath } ` ) ;
9+ }
10+
11+ async function uploadAndDisplay ( page : Page , fileName : string ) {
12+ // Upload the file
13+ await uploadFile ( page , fileName ) ;
14+
15+ // Wait for link with document-link class
16+ await page . waitForSelector ( '.document-link' , { timeout : 20000 } ) ;
17+ await page . getByText ( fileName ) . click ( ) ;
18+
19+ // Wait for the document to load
20+ if ( fileName . endsWith ( '.pdf' ) ) {
21+ await page . waitForSelector ( '.react-pdf__Document' , { timeout : 10000 } ) ;
22+ }
23+ else if ( fileName . endsWith ( '.epub' ) ) {
24+ await page . waitForSelector ( '.epub-container' , { timeout : 10000 } ) ;
25+ }
26+ }
27+
28+ async function waitAndClickPlay ( page : Page ) {
29+ // Wait for play button selector without disabled attribute
30+ await page . waitForSelector ( 'button[aria-label="Play"]:not([disabled])' , { timeout : 20000 } ) ;
31+ // Play the TTS by aria-label play button
32+ await page . locator ( 'button[aria-label="Play"]' ) . click ( ) ;
33+
34+ // Wait for buttons to be disabled
35+ await Promise . all ( [
36+ page . waitForSelector ( 'button[aria-label="Skip forward"][disabled]' ) ,
37+ page . waitForSelector ( 'button[aria-label="Skip backward"][disabled]' ) ,
38+ ] ) ;
39+
40+ // Wait for the TTS to stop processing
41+ await Promise . all ( [
42+ page . waitForSelector ( 'button[aria-label="Skip forward"]:not([disabled])' , { timeout : 30000 } ) ,
43+ page . waitForSelector ( 'button[aria-label="Skip backward"]:not([disabled])' , { timeout : 30000 } ) ,
44+ ] ) ;
45+
46+ await page . waitForFunction ( ( ) => {
47+ return navigator . mediaSession ?. playbackState === 'playing' ;
48+ } , { timeout : 30000 } )
49+ }
50+
51+ async function playTTSAndWaitForASecond ( page : Page , fileName : string ) {
52+ // Upload and display the document
53+ await uploadAndDisplay ( page , fileName ) ;
54+ // Wait for play button selector without disabled attribute
55+ await waitAndClickPlay ( page ) ;
56+ // play for 1s
57+ await page . waitForTimeout ( 1000 ) ;
958}
1059
11- test . describe ( 'Document Upload and Display ' , ( ) => {
60+ test . describe ( 'Document flow ' , ( ) => {
1261 test . beforeEach ( async ( { page } ) => {
1362 // Navigate to the home page before each test
1463 await page . goto ( '/' ) ;
@@ -17,46 +66,56 @@ test.describe('Document Upload and Display', () => {
1766 await page . getByText ( 'Done' ) . click ( ) ;
1867 } ) ;
1968
20- test . describe ( 'PDF handling ' , ( ) => {
21- test ( 'should upload a PDF document' , async ( { page } ) => {
69+ test . describe ( 'PDF check ' , ( ) => {
70+ test ( 'upload a PDF document' , async ( { page } ) => {
2271 // Upload the file
23- await uploadFile ( page , './public/ sample.pdf' ) ;
72+ await uploadFile ( page , 'sample.pdf' ) ;
2473
2574 // Verify upload success
2675 await expect ( page . getByText ( 'sample.pdf' ) ) . toBeVisible ( { timeout : 10000 } ) ;
2776 } ) ;
2877
29- test ( 'should display a PDF document' , async ( { page } ) => {
30- // Upload the file
31- await uploadFile ( page , './public/sample.pdf' ) ;
32-
33- // Click on the uploaded document
34- await page . getByText ( 'sample.pdf' ) . click ( { timeout : 10000 } ) ;
78+ test ( 'display a PDF document' , async ( { page } ) => {
79+ // Upload and display the PDF document
80+ await uploadAndDisplay ( page , 'sample.pdf' ) ;
3581
3682 // Verify PDF viewer is displayed
37- await expect ( page . locator ( '.react-pdf__Document' ) ) . toBeVisible ( { timeout : 10000 } ) ;
38- await expect ( page . locator ( '.react-pdf__Page' ) ) . toBeVisible ( { timeout : 10000 } ) ;
83+ await expect ( page . locator ( '.react-pdf__Document' ) ) . toBeVisible ( ) ;
84+ await expect ( page . locator ( '.react-pdf__Page' ) ) . toBeVisible ( ) ;
85+ } ) ;
86+
87+ test ( 'play and pause TTS for a PDF document (naive)' , async ( { page } ) => {
88+ // Play TTS for the PDF document
89+ await playTTSAndWaitForASecond ( page , 'sample.pdf' ) ;
90+ // Click pause to stop playback
91+ await page . locator ( 'button[aria-label="Pause"]' ) . click ( ) ;
92+ // Check for play button to be visible
93+ await expect ( page . locator ( 'button[aria-label="Play"]' ) ) . toBeVisible ( { timeout : 10000 } ) ;
3994 } ) ;
4095 } ) ;
4196
42- test . describe ( 'EPUB handling ' , ( ) => {
43- test ( 'should upload and display EPUB document' , async ( { page } ) => {
97+ test . describe ( 'EPUB check ' , ( ) => {
98+ test ( 'upload a EPUB document' , async ( { page } ) => {
4499 // Upload the file
45- await uploadFile ( page , './public/sample.epub' ) ;
46-
100+ await uploadFile ( page , 'sample.epub' ) ;
47101 // Verify upload success
48102 await expect ( page . getByText ( 'sample.epub' ) ) . toBeVisible ( { timeout : 10000 } ) ;
49103 } ) ;
50104
51- test ( 'should display an EPUB document' , async ( { page } ) => {
52- // Upload the file
53- await uploadFile ( page , './public/sample.epub' ) ;
54-
55- // Click on the uploaded document
56- await page . getByText ( 'sample.epub' ) . click ( { timeout : 10000 } ) ;
57-
105+ test ( 'display an EPUB document' , async ( { page } ) => {
106+ // Upload and display the EPUB document
107+ await uploadAndDisplay ( page , 'sample.epub' ) ;
58108 // Verify EPUB viewer is displayed
59109 await expect ( page . locator ( '.epub-container' ) ) . toBeVisible ( { timeout : 10000 } ) ;
60110 } ) ;
111+
112+ test ( 'play and pause TTS for an EPUB document (naive)' , async ( { page } ) => {
113+ // Play TTS for the EPUB document
114+ await playTTSAndWaitForASecond ( page , 'sample.epub' ) ;
115+ // Click pause to stop playback
116+ await page . locator ( 'button[aria-label="Pause"]' ) . click ( ) ;
117+ // Check for play button to be visible
118+ await expect ( page . locator ( 'button[aria-label="Play"]' ) ) . toBeVisible ( { timeout : 10000 } ) ;
119+ } ) ;
61120 } ) ;
62121} ) ;
0 commit comments