Each test uses await expect(page).toHaveScreenshot();
. I wouldn’t want to do repetitions in every test.
test.afterEach(async ({ page }) => {
await expect(page).toHaveScreenshot({mask: [] as Locator[] || null});
});
There is a problem. Some tests have dynamic elements that I need to hide. How do I provide an argument/value for { mask }
to handle the first case in some tests?
I tried to give the { mask }
a value but to no avail:
test('should open sender`s grid view', async ({ page }) => {
// some code
{mask: value }
});
2
Answers
One possible approach is to use
describe
blocks that group the different maskings:If you don’t want the describe blocks, there’s the more naive pattern of setting a shared variable among tests:
The
mask = [];
line ensures that the empty mask is the default value if a test hasn’t overridden it.Note that
[] || null
will never evaluate tonull
, even if the array is empty. If you want to providenull
as the default value, replace all occurrences oflet mask = []
andmask = []
withmask = null
.Dynamic elements can be handled using Regex to accommodate many possible scenarios:
//some steps
// Assertion with screeenshots with masking