|
1 | 1 | import { expect, test } from '@playwright/test'; |
2 | 2 | import { handleConsentPopup, runSmokeTestOnPage, waitFor } from './utils'; |
3 | 3 |
|
4 | | -async function openPage(page, sidebarPage) { |
5 | | - // Find all toggles |
6 | | - let currentElement = sidebarPage; |
7 | | - const toggles = []; |
8 | | - |
9 | | - while (currentElement) { |
10 | | - const parentElement = await currentElement.locator('..'); |
11 | | - const isSuperParent = await parentElement.evaluate( |
12 | | - (el) => el.getAttribute('data-testid') === 'sidebar__content' |
13 | | - ); |
14 | | - if (isSuperParent) break; |
15 | | - |
16 | | - const parentElementClass = await parentElement.getAttribute('class'); |
17 | | - if (parentElementClass === 'sidebar__section') { |
18 | | - const toggle = parentElement.getByTestId('sidebar__section__toggle'); |
19 | | - if ((await toggle.count()) > 0) { |
20 | | - toggles.unshift(toggle.first()); |
21 | | - } |
22 | | - } |
23 | | - |
24 | | - currentElement = parentElement; |
25 | | - } |
26 | | - |
27 | | - // Click all toggles found top-down |
28 | | - for (const toggle of toggles) { |
29 | | - const isCollapsed = |
30 | | - (await toggle.first().getAttribute('aria-expanded')) === 'false'; |
31 | | - if (isCollapsed) { |
32 | | - await toggle.first().click(); |
33 | | - } |
34 | | - } |
35 | | - |
36 | | - // Click on the page |
37 | | - await sidebarPage.click(); |
38 | | - const content = page.getByTestId('content'); |
39 | | - await content.waitFor(); |
40 | | -} |
41 | | - |
42 | 4 | test.describe('Smoke test for sidebar', () => { |
43 | 5 | // Slow test |
44 | 6 | test.setTimeout(100_000); |
45 | 7 |
|
46 | 8 | test.beforeEach(async ({ page }) => { |
47 | 9 | await page.goto('/test-product/'); |
48 | 10 | await page.waitForLoadState('load'); |
| 11 | + await waitFor(async () => await handleConsentPopup(page)); |
49 | 12 | }); |
50 | 13 |
|
51 | 14 | test('sidebar renders', async ({ page }) => { |
52 | | - await expect( |
53 | | - await page.getByTestId('sidebar__header').count() |
54 | | - ).toBeTruthy(); |
55 | | - await expect( |
56 | | - await page.getByTestId('sidebar__content').count() |
57 | | - ).toBeTruthy(); |
| 15 | + await expect(page.getByTestId('sidebar__header')).toBeVisible(); |
| 16 | + await expect(page.getByTestId('product-selector')).toBeVisible(); |
| 17 | + await expect(page.getByTestId('sidebar__content')).toBeVisible(); |
58 | 18 | }); |
59 | 19 |
|
60 | | - test('each section page on sidebar renders', async ({ page }) => { |
61 | | - /* Click on each link */ |
62 | | - const sidebarPages = await page.getByTestId('sidebar__page').all(); |
63 | | - for (const sidebarPage of sidebarPages) { |
64 | | - await waitFor(async () => await handleConsentPopup(page)); |
65 | | - await openPage(page, sidebarPage); |
66 | | - await runSmokeTestOnPage(page); |
| 20 | + test('product selector renders when clicked', async ({ page }) => { |
| 21 | + // Click on product selector |
| 22 | + const productSelector = page.getByTestId('product-selector'); |
| 23 | + await productSelector.click(); |
| 24 | + |
| 25 | + // Ensure the product selector has items visible |
| 26 | + const productSelectorContent = productSelector.getByTestId( |
| 27 | + 'product-selector__content' |
| 28 | + ); |
| 29 | + const products = await productSelectorContent |
| 30 | + .getByTestId('product-selector__product') |
| 31 | + .all(); |
| 32 | + |
| 33 | + expect(products.length).toBeGreaterThan(0); |
| 34 | + for (const product of products) { |
| 35 | + await expect(product).toBeVisible(); |
67 | 36 | } |
68 | 37 | }); |
| 38 | + |
| 39 | + test('clicking on a link', async ({ page }) => { |
| 40 | + // Greedy logic assumes there is at least one link without having to open a dropdown. |
| 41 | + |
| 42 | + // Fetch only visible links (aka ones not nested in a dropdown) |
| 43 | + const sidebarContent = page.getByTestId('sidebar__content'); |
| 44 | + const pages = await sidebarContent.getByTestId('sidebar__page').all(); |
| 45 | + const visibleResults = await Promise.all( |
| 46 | + pages.map(async (page) => await page.isVisible()) |
| 47 | + ); |
| 48 | + const visiblePages = pages.filter((_, index) => visibleResults[index]); |
| 49 | + expect(visiblePages.length).toBeGreaterThan(0); |
| 50 | + |
| 51 | + // Click on link |
| 52 | + const firstPage = visiblePages.at(0); |
| 53 | + await firstPage.click(); |
| 54 | + await waitFor(async () => await handleConsentPopup(page)); |
| 55 | + const content = page.getByTestId('content'); |
| 56 | + await content.waitFor(); |
| 57 | + |
| 58 | + // Run smoke test to validate page is not 404 |
| 59 | + await runSmokeTestOnPage(page); |
| 60 | + }); |
| 61 | + |
| 62 | + test('clicking on a dropdown', async ({ page }) => { |
| 63 | + // Greedy logic assumes there is at least one dropdown at the first level. |
| 64 | + |
| 65 | + // Fetch only visible dropdowns (aka ones not nested in a dropdown) |
| 66 | + const sidebarContent = page.getByTestId('sidebar__content'); |
| 67 | + const dropdowns = await sidebarContent |
| 68 | + .getByTestId('sidebar__section__toggle') |
| 69 | + .all(); |
| 70 | + const visibleResults = await Promise.all( |
| 71 | + dropdowns.map(async (dropdown) => await dropdown.isVisible()) |
| 72 | + ); |
| 73 | + const visibleDropdowns = dropdowns.filter( |
| 74 | + (_, index) => visibleResults[index] |
| 75 | + ); |
| 76 | + |
| 77 | + // Since it is not guaranteed there will be any links under a dropdown, we should check the chevron changes. |
| 78 | + // Also this should not check if links work. That is the job of the 'clicking on a link' test. |
| 79 | + const firstDropdown = visibleDropdowns.at(0); |
| 80 | + const chevron = ( |
| 81 | + await firstDropdown.getByTestId('sidebar__chevron').all() |
| 82 | + ).at(0); |
| 83 | + const openClassName = 'sidebar__chevron--open'; |
| 84 | + expect(await chevron.getAttribute('class')).not.toContain(openClassName); |
| 85 | + |
| 86 | + // Open the dropdown |
| 87 | + await firstDropdown.click(); |
| 88 | + expect(await chevron.getAttribute('class')).toContain(openClassName); |
| 89 | + |
| 90 | + // Close the dropdown |
| 91 | + await firstDropdown.click(); |
| 92 | + expect(await chevron.getAttribute('class')).not.toContain(openClassName); |
| 93 | + }); |
69 | 94 | }); |
0 commit comments