Skip subsequent Actions read for current testcase if any action Fails

148 views Asked by At

I have a playwright test with PageObject model(Typescripting) where spec file contains test.describe with For loop to read each actions from a data file and call respective functions defined in separate file.

I got 10+ paths in each data file having 10+ actions for each Path. Is there a way to skip the subsequent actions for the Path being executed if any action fails.

Specfile.ts

test.describe( async () => {
    test.beforeAll(async ({ }) => { const browser = await playwright.chromium.launch({});   };
    testData.forEach((data) => {
       test(data.path , async() => {
         if (action= XXX) {Call Function A}
        if (action=YYY) {Call Function B}
}
}


Datafile.ts

const testData [
{  Path: path1, PathTitile : test1,   Actions [ Action1, Action2, Action3 ]  }
{  Path: path2, PathTitile : test2,  Actions [ Action1, Action2, Action3 ]  }
{  Path: path3, PathTitile : test2,  Actions [ Action1, Action2, Action3 ]  }

]

test.skip is skipping the whole datafile and all remaining paths are skipped.

Expectation is to skip pending Actions in the current Path alone and proceed with next Path.

1

There are 1 answers

7
OPSM On

To achieve something like this, you will want to conditionally skip your tests. This will need added to your test block (test.describe) if you want entire blocks to be skipped, or to each test individually if you want each one to be skipped independently.

This is all from a quick mock up so tailor to your needs!

In your test, create a boolean variable called something like 'passed', and assign to default of true. Make sure you declare this outside your tests.

let passed:boolean = true;

Then, in each test, add something like:

test.skip(passed===false, 'Test failed. Skipping.'); At the very beginning of the test. Like:

test('do stuff', async ({ page}) => { test.skip(passed === false, 'Skipping'); });

This means each test checks the result variable and skips if it failed previously. All that is left is to add logic to each test that if it meets your desired condition, set passed to true. Else, set it to false. That can just be a simple if statement or something more complex. Your test goal will dictate this so I cant write that for you!

As mentioned, this is all just as an idea and im doing this on mobile, so ignore any syntax issues. This should steer you in a good direction!

If you want to keep the test going after failure, but still mark it as failed, look at adding soft assertions which are designed to do just that. Read up on this: https://playwright.dev/docs/test-assertions#soft-assertions