Skip to content

Instantly share code, notes, and snippets.

@jeremieflrnt
Last active August 9, 2022 13:34
Show Gist options
  • Save jeremieflrnt/ae59768bc5f8fe8fb153dc99c122e87e to your computer and use it in GitHub Desktop.
Save jeremieflrnt/ae59768bc5f8fe8fb153dc99c122e87e to your computer and use it in GitHub Desktop.
Write tests for every story
import componentList from '../resources/componentList.json';
const iFrameUrl = '/iframe.html?id=';
for (const [component, stories] of Object.entries(componentList)) {
test.describe(`Smart tests for component: ${component}`, () => {
for (const story of stories) {
test(`Smart test for story: ${story.split('--')[1]}`, async ({
page,
baseURL,
isMobile,
browserName,
}, testInfo) => {
test.skip(story.includes('mobile') && !isMobile);
await page.goto(baseURL + iFrameUrl + story);
await expectToMatchSnapshot(page, testInfo, component, story, 0, 'base');
page.on('filechooser', async (fileChooser) => {
await fileChooser.setFiles('resources/image1.jpg');
});
//TOOLTIP
[...]
//BUTTON
const buttonLocator = page.locator('#root >> //button[@type="button" and not(@disabled)]');
const buttonCount = await buttonLocator.count();
for (let i = 0; i < buttonCount; ++i) {
await buttonLocator.nth(i).hover();
await expectToMatchSnapshot(page, testInfo, component, story, i, 'button-hover');
await buttonLocator.nth(i).click();
await expectToMatchSnapshot(page, testInfo, component, story, i, 'button-click');
}
//ELEMENT WITH ONCLICK
[...]
//INPUT TEXT
const inputLocator = page.locator(
'#root >> //input[@type="text" and not(@disabled or @readonly or @role="combobox")]',
);
const inputCount = await inputLocator.count();
for (let i = 0; i < inputCount; ++i) {
await inputLocator.nth(i).hover();
await expectToMatchSnapshot(page, testInfo, component, story, i, 'input-hover');
await inputLocator.nth(i).click();
await expectToMatchSnapshot(page, testInfo, component, story, i, 'input-click');
await inputLocator.nth(i).fill('Toto');
await expectToMatchSnapshot(page, testInfo, component, story, i, 'input-fill');
}
//TOGGLE
[...]
//RADIO BUTTON
[...]
//UPLOAD FILE
[...]
//SELECT
[...]
//CLICK TO FULLSCREEN
[...]
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment