|
| 1 | +import { Locator, Page, expect } from '@playwright/test' |
| 2 | +import { TestId, generateTestId } from './testIds' |
| 3 | +import path from 'node:path' |
| 4 | + |
| 5 | +/** |
| 6 | + * Helper class for interacting with file upload dropzones in Playwright tests. |
| 7 | + */ |
| 8 | +export class FileUpload { |
| 9 | + private page: Page |
| 10 | + private resourceName: string |
| 11 | + private componentId: string |
| 12 | + |
| 13 | + private dropzone: Locator |
| 14 | + private uploadButton: Locator |
| 15 | + private selectedFileName: Locator |
| 16 | + |
| 17 | + /** |
| 18 | + * Creates a new FileUpload instance. |
| 19 | + * |
| 20 | + * @param page - The Playwright Page object |
| 21 | + * @param resourceName - The resource name used in the API endpoint (e.g., 'job_file') |
| 22 | + * @param componentId - The component ID used to generate data-testid for the dropzone |
| 23 | + */ |
| 24 | + constructor(page: Page, resourceName: string, componentId?: string) { |
| 25 | + this.page = page |
| 26 | + this.resourceName = resourceName |
| 27 | + this.componentId = componentId |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Gets the dropzone locator (lazy initialization). |
| 32 | + */ |
| 33 | + private getDropzone(): Locator { |
| 34 | + if (!this.dropzone) { |
| 35 | + this.dropzone = this.page.getByTestId( |
| 36 | + generateTestId(TestId.FILE_UPLOAD_DROPZONE, this.componentId) |
| 37 | + ) |
| 38 | + } |
| 39 | + return this.dropzone |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets the upload button locator (lazy initialization). |
| 44 | + */ |
| 45 | + private getUploadButton(): Locator { |
| 46 | + if (!this.uploadButton) { |
| 47 | + const dropzone = this.getDropzone() |
| 48 | + this.uploadButton = dropzone.getByTestId( |
| 49 | + generateTestId(TestId.BUTTON, 'upload') |
| 50 | + ) |
| 51 | + } |
| 52 | + return this.uploadButton |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Gets the selected file name locator (lazy initialization). |
| 57 | + */ |
| 58 | + private getSelectedFileName(): Locator { |
| 59 | + if (!this.selectedFileName) { |
| 60 | + const dropzone = this.getDropzone() |
| 61 | + this.selectedFileName = dropzone.getByTestId( |
| 62 | + TestId.FILE_UPLOAD_DROPZONE_SELECTED_FILE_NAME |
| 63 | + ) |
| 64 | + } |
| 65 | + return this.selectedFileName |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Checks that the upload dropzone exists and is visible. |
| 70 | + */ |
| 71 | + public async expectDropzoneToExist(): Promise<void> { |
| 72 | + const dropzone = this.getDropzone() |
| 73 | + await expect(dropzone).toBeVisible() |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Uploads a file by selecting it through the file chooser. |
| 78 | + * |
| 79 | + * @param filePath - Absolute or relative path to the file to upload |
| 80 | + * @param baseDir - Optional base directory (defaults to __dirname if filePath is relative) |
| 81 | + */ |
| 82 | + public async uploadFile(filePath: string, baseDir?: string): Promise<void> { |
| 83 | + const dropzone = this.getDropzone() |
| 84 | + const selectedFileName = this.getSelectedFileName() |
| 85 | + |
| 86 | + // Resolve the full file path |
| 87 | + const fullPath = path.isAbsolute(filePath) |
| 88 | + ? filePath |
| 89 | + : path.join(baseDir || __dirname, filePath) |
| 90 | + |
| 91 | + // Extract just the filename for verification |
| 92 | + const fileName = path.basename(fullPath) |
| 93 | + |
| 94 | + // Start waiting for file chooser before clicking |
| 95 | + const fileChooserPromise = this.page.waitForEvent('filechooser') |
| 96 | + await dropzone.click() |
| 97 | + const fileChooser = await fileChooserPromise |
| 98 | + await fileChooser.setFiles(fullPath) |
| 99 | + |
| 100 | + // Verify the file name is displayed |
| 101 | + await expect(selectedFileName).toContainText(fileName) |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Clicks the upload button and waits for the upload to complete successfully. |
| 106 | + * |
| 107 | + * @param onSuccess - Optional callback to execute after successful upload |
| 108 | + */ |
| 109 | + public async clickUploadAndWaitForSuccess( |
| 110 | + onSuccess?: () => Promise<void> | void |
| 111 | + ): Promise<void> { |
| 112 | + const uploadButton = this.getUploadButton() |
| 113 | + |
| 114 | + // Verify upload button is enabled |
| 115 | + await expect(uploadButton).not.toBeDisabled() |
| 116 | + |
| 117 | + // Wait for the API response |
| 118 | + const uploadResponse = this.page.waitForResponse( |
| 119 | + `**/api/${this.resourceName}**` |
| 120 | + ) |
| 121 | + |
| 122 | + await uploadButton.click() |
| 123 | + await uploadResponse |
| 124 | + |
| 125 | + // Execute the success callback if provided |
| 126 | + if (onSuccess) { |
| 127 | + await onSuccess() |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Expects the upload button to be visible. |
| 133 | + */ |
| 134 | + public async expectUploadButtonToBeVisible(): Promise<void> { |
| 135 | + const uploadButton = this.getUploadButton() |
| 136 | + await expect(uploadButton).toBeVisible() |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Expects the upload button to be enabled. |
| 141 | + */ |
| 142 | + public async expectUploadButtonToBeEnabled(): Promise<void> { |
| 143 | + const uploadButton = this.getUploadButton() |
| 144 | + await expect(uploadButton).not.toBeDisabled() |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Expects the selected file name to contain specific text. |
| 149 | + * |
| 150 | + * @param fileName - Expected file name text |
| 151 | + */ |
| 152 | + public async expectSelectedFileNameToContain(fileName: string): Promise<void> { |
| 153 | + const selectedFileName = this.getSelectedFileName() |
| 154 | + await expect(selectedFileName).toContainText(fileName) |
| 155 | + } |
| 156 | +} |
0 commit comments