Skip to content

Commit

Permalink
(hds-2521) e2e tests for focus move
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoHelle committed Nov 20, 2024
1 parent cf1631a commit 4616a52
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
26 changes: 23 additions & 3 deletions e2e/tests/react/components/react-cookie-consent-spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { test, expect, Page, Locator } from '@playwright/test';
import { isLocatorSelectedOrChecked } from '../../../utils/element.util';
import { getFocusedElement, isLocatorSelectedOrChecked } from '../../../utils/element.util';
import {
gotoStorybookUrlByName,
createScreenshotFileName,
takeScreenshotWithSpacing,
waitFor,
getLocatorElement,
} from '../../../utils/playwright.util';
const componentName = 'cookieconsent';
const storybook = 'react';
Expand Down Expand Up @@ -80,7 +81,7 @@ test.describe(`Banner`, () => {

const storeConsentsAndWaitForBannerClose = async (
page: Page,
approveType: 'all' | 'required',
approveType: 'all' | 'required' | 'selected',
bannerLocator: Locator,
) => {
const banner = bannerLocator || getBannerOrPageLocator(page);
Expand Down Expand Up @@ -179,7 +180,7 @@ test.describe(`Banner`, () => {
const screenshotNameSV = createScreenshotFileName(testInfo, isMobile, 'sv language');
await takeScreenshotWithSpacing(page, banner, screenshotNameSV);
});
test('Banner is closed after approval and not shown again.', async ({ page, isMobile }, testInfo) => {
test('Banner is closed after approval and not shown again. Focus is moved.', async ({ page, isMobile }, testInfo) => {
if (isMobile) {
// viewport is too small to test with mobile view. Banner blocks the ui.
return;
Expand All @@ -188,6 +189,9 @@ test.describe(`Banner`, () => {
await changeTab(page, 'banner');
const banner = getBannerOrPageLocator(page);
await storeConsentsAndWaitForBannerClose(page, 'all', banner);
const focusTarget = await getLocatorElement(page.locator('#actionbar > a'));
const focusedElement = await getFocusedElement(page.locator('body'));
expect(focusTarget === focusedElement).toBeTruthy();
const screenshotName = createScreenshotFileName(testInfo, isMobile);
await takeScreenshotWithSpacing(page, page.locator('body'), screenshotName);

Expand All @@ -207,6 +211,22 @@ test.describe(`Banner`, () => {
const consents = await approveRequiredAndCheckStoredConsents(page);
expect(consents).toEqual(['essential', 'test_essential']);
});
test('Element given in openBanner is focused on banner close', async ({ page, isMobile }, testInfo) => {
if (isMobile) {
// viewport is too small to test with mobile view. Banner blocks the ui.
return;
}
const openerSelector = '#banner-opener';
await gotoStorybookUrlByName(page, 'Example', componentName, storybook);
await changeTab(page, 'actions');
const bannerOpener = page.locator(openerSelector);
const banner = getBannerOrPageLocator(page);
await bannerOpener.click();
await storeConsentsAndWaitForBannerClose(page, 'selected', banner);
const focusTarget = await getLocatorElement(page.locator(openerSelector));
const focusedElement = await getFocusedElement(page.locator('body'));
expect(focusTarget === focusedElement).toBeTruthy();
});
test('Stored consents are changed via settings page', async ({ page, isMobile }, testInfo) => {
if (isMobile) {
// viewport is too small to test with mobile view. Banner blocks the ui.
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions e2e/utils/playwright.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,8 @@ export const gotoStorybookUrlByName = async (page: Page, name: string, component
await page.goto(targetUrl);
return targetUrl;
};

export const getLocatorElement = async (locator: Locator): Promise<HTMLElement | SVGElement | null> => {
const first = locator.first();
return first.evaluate((el) => el);
};

0 comments on commit 4616a52

Please sign in to comment.