playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature]: Support a Method to Explicitly Mark Test as Passed and Exit Early

Open slittleton opened this issue 6 months ago • 2 comments

🚀 Feature Request

Currently, Playwright does not provide a method such as test.pass() to immediately mark a test as passed and exit early. While using return achieves a similar effect, it can be ambiguous when reviewing test logs or understanding developer intent. Having a clear, expressive way to exit a test with a "passed" status—especially under conditional scenarios—would improve readability and convey semantics more directly.

Example

  1. Sometimes I need to run a test suite but some of the tests should be skipped either for time saving or they are not relevant.

  2. Dynamically skipping parts of a test when preconditions are met, but still considering it a successful validation.

  3. Improving clarity in CI logs by distinguishing between skipped, failed, and explicitly passed conditions.

  4. Allowing finer control in custom test helpers and shared test setups.

Motivation

I believe that are a number of use cases for this functionality and I specifically find that test.skip() is often inconvenient if I want part of a test to run. This would allow us to pass a test at any point where we determine it is appropriate.

slittleton avatar Jun 23 '25 15:06 slittleton

Could you elaborate on the shortcomings of return? You can communicate intent by conventionally doing return "passed" or something else. Playwright doesn't read the return value, so you can return whatever.

Skn0tt avatar Jun 24 '25 09:06 Skn0tt

I see your point. Thanks for pointing that out. I think it could be useful in some circumstances where you might have to nest return statements but for most use cases that probably wont be a major issue.

Sometimes I have to write tests that have a lot of filling out forms and I end up writing utility functions to just generate dummy data to get to the next stage, or for functions that cover chunks of test steps that get reused in multiple tests. Sometimes there are multi-stage forms where the behavior of the next stage of the form changes depending on what is being tested. This requires multiple utility functions that often require some nesting. I was just thinking it would be nice to be able to just drop in a method that told the test to pass right at that point even if its deeply nested in multiple function calls. If I return from multiple points of nesting functions it could potentially get a bit unwieldy.

thanks for the reply

slittleton avatar Jun 24 '25 21:06 slittleton

Voting for this feature request. (I raised #36665 )

Having a method that performs the equivalent of Assert.Pass() would greatly improve the flexibility of Playwright tests. This is especially useful in complex tests where nesting and having to add in multiple return() across calls can be time consuming.

I have the same need as @slittleton -- depending on the test data, tests can take various branching paths. If using the alternative, which are return(), then I would need to modify more than just one method at a time, perhaps even up to a n-level/test level depending on how deep the code is.

Example

[
   {expectvalidationerror: true},
   {expectvalidationerror: false}
].forEach((expectvalidationerror}) => {
    test('Add User Test', async ({ page }) => {
          //code to generate test data depending on expectvalidationerror
          ....
          await AddUser(page, UserObject, expectvalidationerror);
          //more steps for the expect false scenario that are no longer relevant for the 'true' case
          //including database checks, UI checks, etc
          ....
    });
});
export async function AddUser(page : Page, TestUser : User, expecterrors: Boolean){
      //code to manipulate UI
      ....
      if(expecterrors){
           //this is where expects would be added to check if errors are shown with the correct messages
           //in Selenium, this is where I would use an Assert.Pass() to stop the test if the expected validation errors were shown
      }
      else{
           //proceeds through to the next methods in the test
      }
}

addtcubico avatar Jul 15 '25 06:07 addtcubico