|
| 1 | +import { faker } from '@faker-js/faker'; |
| 2 | +import { expect, Page, test } from '@playwright/test'; |
| 3 | +import { PreviewPage } from '../../page-objects/pages/PreviewPage.js'; |
| 4 | + |
| 5 | +const expandAllCategories = async (page: Page) => { |
| 6 | + await page.evaluate(() => { |
| 7 | + const collapsedDetails = document.querySelectorAll<HTMLDetailsElement>('details:not([open])'); |
| 8 | + |
| 9 | + collapsedDetails.forEach((details) => { |
| 10 | + details.open = true; |
| 11 | + }); |
| 12 | + }); |
| 13 | +}; |
| 14 | + |
| 15 | +test.describe('Preview Form Render', () => { |
| 16 | + test('demo forms load', async ({ context, page }) => { |
| 17 | + const previewPage = new PreviewPage(page); |
| 18 | + await previewPage.goToPage(); |
| 19 | + |
| 20 | + const formPreviewLinks = await page.locator('.form-preview-link').all(); |
| 21 | + |
| 22 | + expect(formPreviewLinks.length).toBeGreaterThan(0); |
| 23 | + |
| 24 | + for (const link of formPreviewLinks) { |
| 25 | + const [formPreviewPage] = await Promise.all([context.waitForEvent('page'), link.click()]); |
| 26 | + |
| 27 | + await formPreviewPage.waitForSelector( |
| 28 | + '.form-initialization-status.error, .form-initialization-status.ready', |
| 29 | + { |
| 30 | + state: 'attached', |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + const [failureDialog] = await formPreviewPage.locator('.form-load-failure-dialog').all(); |
| 35 | + |
| 36 | + expect(failureDialog).toBeUndefined(); |
| 37 | + |
| 38 | + await formPreviewPage.close(); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + test('all forms are rendered and there is no console error', async ({ page, browserName }) => { |
| 43 | + let consoleErrors = 0; |
| 44 | + page.on('console', (msg) => { |
| 45 | + if (msg.type() === 'error' || msg.type() === 'warning') { |
| 46 | + consoleErrors++; |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + const previewPage = new PreviewPage(page); |
| 51 | + await previewPage.goToPage(); |
| 52 | + |
| 53 | + // this ensures that Vue application is loaded before proceeding forward. |
| 54 | + await expect(page.getByText('Demo Forms')).toBeVisible(); |
| 55 | + |
| 56 | + // Let's expand all categories, so that Form list is visible |
| 57 | + await expandAllCategories(page); |
| 58 | + |
| 59 | + const forms = await page.locator('ul.form-list li a').all(); |
| 60 | + |
| 61 | + expect(forms.length).toBeGreaterThan(0); |
| 62 | + |
| 63 | + for (const form of forms) { |
| 64 | + await form.click(); |
| 65 | + |
| 66 | + // Traverse the form element by element |
| 67 | + // if focused element is an editable textbox then fill it |
| 68 | + // Exit the loop when focus is on the Send button |
| 69 | + while (true) { |
| 70 | + const onSendButton = await page.evaluate(() => { |
| 71 | + const activeElement = document.activeElement; |
| 72 | + return activeElement?.tagName === 'BUTTON' && activeElement.textContent === 'Send'; |
| 73 | + }); |
| 74 | + |
| 75 | + if (onSendButton) { |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + await page.keyboard.press(browserName == 'webkit' ? 'Alt+Tab' : 'Tab'); |
| 80 | + |
| 81 | + const inputType = await page.evaluate(() => { |
| 82 | + const isInputElement = ( |
| 83 | + activeElement: Element | null |
| 84 | + ): activeElement is HTMLInputElement => { |
| 85 | + return activeElement?.tagName === 'INPUT'; |
| 86 | + }; |
| 87 | + |
| 88 | + const activeElement = document.activeElement; |
| 89 | + |
| 90 | + if ( |
| 91 | + !isInputElement(activeElement) || |
| 92 | + activeElement.hasAttribute('readonly') || |
| 93 | + activeElement.hasAttribute('disabled') |
| 94 | + ) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + return activeElement.type; |
| 99 | + }); |
| 100 | + |
| 101 | + if (inputType === 'text') { |
| 102 | + await page.keyboard.type(faker.internet.displayName()); |
| 103 | + } |
| 104 | + // Select the next option, if the last option is selected by default |
| 105 | + // then browser selects the first one. |
| 106 | + else if (inputType === 'radio') { |
| 107 | + await page.keyboard.press('ArrowDown'); |
| 108 | + } |
| 109 | + // Tab behaviour for checkboxes is different, each Tab press moves the focus |
| 110 | + // to the next option. Here we are toggling every checkbox option. |
| 111 | + else if (inputType === 'checkbox') { |
| 112 | + // Toggle the option |
| 113 | + await page.keyboard.press('Space'); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + const langChanger = page.locator('.larger-screens .language-changer'); |
| 118 | + |
| 119 | + if ((await langChanger.count()) > 0) { |
| 120 | + await langChanger.click(); |
| 121 | + await page.locator('.language-dd-label').last().click(); |
| 122 | + } |
| 123 | + |
| 124 | + await page.goBack(); |
| 125 | + await expandAllCategories(page); |
| 126 | + } |
| 127 | + |
| 128 | + expect(consoleErrors).toBe(0); |
| 129 | + }); |
| 130 | +}); |
0 commit comments