Skip to content

Commit 2f2442e

Browse files
committed
Refactor tests: update Play/Pause functionality and document upload tests; replace e2e.spec.ts with helper functions
1 parent 54dea5d commit 2f2442e

File tree

5 files changed

+144
-127
lines changed

5 files changed

+144
-127
lines changed

playwright.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default defineConfig({
3131
baseURL: 'http://localhost:3003',
3232

3333
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
34-
trace: 'on-first-retry',
34+
trace: 'retain-on-first-failure',
3535
screenshot: 'only-on-failure',
3636
},
3737

tests/e2e.spec.ts

Lines changed: 0 additions & 126 deletions
This file was deleted.

tests/helpers.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

tests/play.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { test, expect } from '@playwright/test';
2+
import { setupTest, playTTSAndWaitForASecond } from './helpers';
3+
4+
test.describe('Play/Pause Tests', () => {
5+
test.beforeEach(async ({ page }) => {
6+
await setupTest(page);
7+
});
8+
9+
test.describe.configure({ mode: 'serial' });
10+
11+
test('plays and pauses TTS for a PDF document', async ({ page }) => {
12+
// Play TTS for the PDF document
13+
await playTTSAndWaitForASecond(page, 'sample.pdf');
14+
15+
// Click pause to stop playback
16+
await page.getByRole('button', { name: 'Pause' }).click();
17+
18+
// Check for play button to be visible
19+
await expect(page.getByRole('button', { name: 'Play' })).toBeVisible({ timeout: 10000 });
20+
});
21+
22+
test('plays and pauses TTS for an EPUB document', async ({ page }) => {
23+
// Play TTS for the EPUB document
24+
await playTTSAndWaitForASecond(page, 'sample.epub');
25+
26+
// Click pause to stop playback
27+
await page.getByRole('button', { name: 'Pause' }).click();
28+
29+
// Check for play button to be visible
30+
await expect(page.getByRole('button', { name: 'Play' })).toBeVisible({ timeout: 10000 });
31+
});
32+
});

tests/upload.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { test, expect } from '@playwright/test';
2+
import { uploadFile, uploadAndDisplay, setupTest } from './helpers';
3+
4+
test.describe('Document Upload Tests', () => {
5+
test.beforeEach(async ({ page }) => {
6+
await setupTest(page);
7+
});
8+
9+
test('uploads a PDF document', async ({ page }) => {
10+
await uploadFile(page, 'sample.pdf');
11+
await expect(page.getByText('sample.pdf')).toBeVisible({ timeout: 10000 });
12+
});
13+
14+
test('uploads an EPUB document', async ({ page }) => {
15+
await uploadFile(page, 'sample.epub');
16+
await expect(page.getByText('sample.epub')).toBeVisible({ timeout: 10000 });
17+
});
18+
19+
test('displays a PDF document', async ({ page }) => {
20+
await uploadAndDisplay(page, 'sample.pdf');
21+
await expect(page.locator('.react-pdf__Document')).toBeVisible();
22+
await expect(page.locator('.react-pdf__Page')).toBeVisible();
23+
await expect(page.getByText('Sample PDF')).toBeVisible();
24+
});
25+
26+
test('displays an EPUB document', async ({ page }) => {
27+
await uploadAndDisplay(page, 'sample.epub');
28+
await expect(page.locator('.epub-container')).toBeVisible({ timeout: 10000 });
29+
await expect(page.getByRole('button', { name: '‹' })).toBeVisible();
30+
await expect(page.getByRole('button', { name: '›' })).toBeVisible();
31+
});
32+
});

0 commit comments

Comments
 (0)