Current section: End-to-End 6 exercises

Implementing Fixtures to Ensure User Deletion in Playwright Tests

Loading exercise


00:00 So we're not totally out of the woods yet. We still have a situation where we can end up with a user that was generated as part of the test and not actually deleted. And that situation is pretty simple, actually. If we've got an error that's thrown after we create the user, then that user's gonna stick around

00:19 because we never get around to the part of the code that is gonna actually delete the user. So here we go, got another one, and we play it again. It's gonna break again after creating the user, and we get another one. So your job is to make certain that we delete the user after we've created the user.

00:37 And there are various ways that you can accomplish this, but the way that we do it in Playwright is with this concept called fixtures. So there's some instructions on what fixtures are and how you can use that API in Playwright. So follow those instructions,

00:54 follow the instructions from the emoji inline, and you should be all set to go to make certain that this delete will actually happen. I'll mention also that an alternative is we could just wrap the entire contents of our test

01:12 in a try-catch and then put this in a finally block, which sounds awful. And another alternative is to use an after-each hook, which Playwright also supports. But fixtures is really the best way to do this with Playwright. It is the way that the Playwright team recommends as well,

01:30 and it avoids some pitfalls that can happen to you if you use hooks for this type of thing. I'll also add a link in the instructions that will show you the documentation for fixtures, and they have a little section in there explaining why fixtures are better than hooks as well.

01:50 There's some more detail if you wanna look at that. Your task is to create a fixture that allows you to create a new user in the database and make sure that that user gets deleted after the test is finished. Have a good time with this. We'll see you when you're done.