Reputation: 2228
I am developing E2E tests with Playwright for angular app. I have to ensure that needed conditional selector exists in order to proceed, otherwise skip further tests.
In other words I need to skip all tests in a group if await page.isVisible('button[id=container]')
condition is not satisfied in beforeAll
hook.
The problem is that only the first test is being skipped and the second one is being passed(which is wrong). Please check screenshot below.
Here is the code:
test.describe.serial('Products ', () => {
let page: Page;
test.beforeAll(async ({ browser, baseURL }) => {
page = await browser.newPage();
await page.goto(baseURL as string, opt);
if (await page.isVisible('button[id=container]')) {
await page.locator('button[id=container]').click();
} else {
console.log('Error: Container tab is not loaded...');
test.skip();
}
});
test.only('Check if "Menu" tab is available', async () => {
... test goes here ...
});
test.only('Check if Orders page is available', async () => {
... test goes here ...
});
});
The interesting thing is that playwright skips all tests when beforeEach
hook is used instead, but I would like to achieve this result within beforeAll
hook.
Upvotes: 0
Views: 5504
Reputation: 733
That is interesting behavior, and maybe because inside a hook like beforeAll it’s treating it as a single test skip, whereas calling it outside any hooks or tests but within a describe block skips all the tests in that block. Not sure though.
My first solution to offer is based off your intent. Do you really want them to skip? Or should it not reach that point and actually be a failure and just fail all the tests in that group? You could just throw an error inside the beforeAll, like so:
throw new Error('Error: Container tab is not loaded...')
Or better yet, get rid of the if else all together, and just let the click error, which already checks actionability like being visible and takes advantage of auto-waiting. And if you still want the custom error message, wrap it in a try catch block where the catch does that console error and then rethrows the error. Or do an expect(locator, message).toBeVisible()
first with the custom message option, which still gives the auto-wait benefit.
If you are bent on skipping though, one option would be to use a ”flag”/boolean variable that you declare in the describe and set in the beforeAll, and then all the tests conditionally skip off that. Not as great as having skip in the beforeAll skip all the tests, but a workaround that should do the trick.
Upvotes: 1