Delete Request | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: API Testing Basics
Instructor: Artem Bondar
Lesson Summary
In this lesson, we build upon the previous one where we created an authorized post request to create a new article. The focus now is on adding a final step to delete the article , allowing us to rerun the test without encountering issues related to duplicate articles. Key Concepts The delete request does not require a request body; it only needs the URL, which includes a unique identifier for the article. Authorization token must be provided to perform the delete request. The response from the post request contains a slug , which serves as the unique ID for the article. Steps to Implement the Delete Request After making the post request, save the slug into a constant: const slugID = newArticleResponse.json().article.slug; Create the delete request using the saved slug ID: const deleteArticleResponse = await request.delete(`url/${slugID}`, { headers: { Authorization: 'Bearer token' } }); Validate the response status code: expect(deleteArticleResponse.status()).toBe(204); Summary of the Lesson To make a delete request in Playwright , use the delete method within the request fixture. Utilize interpolation for dynamic URL values by using backticks and the syntax ${variable} . Always provide headers for secure APIs and validate the status code to ensure the request was successful. By implementing these steps, we ensure that our tests can be run repeatedly without issues, maintaining a clean testing environment.
Video Transcript
In the previous lesson, we created an authorized post request to create a new article. In this lesson, we will continue this test, adding a final step to delete the article. So that way, we will be able to rerun this test again and again, not afraiding the situation when we cannot create duplicating article because this is the functionality of our API. So we reset the test precondition every time when we run the test. So let's get into it. So previously, we were using Postman to delete the article. So we were using this API request. It was a delete request to delete the article. The delete request, I remind you, does not have a request body. So all it has is just the URL. And as part of the URL, we have a unique identifier for the article that we want to delete. And that's it. We also need provide authorization token and perform the request. Delete request also does not have the response body. So what we need to do is to get this unique identifier and perform delete request in the playwright. So where to get this unique identifier? When we made a post request, the response object has this one article, and article details is a slug. Slug is a unique ID for the article. So after we make a post request, we need to save this value into the constant and then reuse this value as part of the delete request right here, as part of our request API URL. All right, so let's get into it. So this is our test, and let's add a final step to delete the article. So I will rename the test, create and delete article. So this name will better reflect what we are doing in the test. So next, after we made a post request right here, as part of the JSON object from here, we need to create a new constant. Const, and I call it slug ID equals to new article response JSON. Then it's what? It's article.article. And the next key is slug.slug. So this constant will have a slug ID after we created the article. Then we can use the slug ID as part of the delete request. Let's create a delete request. Const delete article. Response equals to await request dot, and I use a delete method. And provide the URL, take this URL from Postman. And provide it right here. But instead of using the hard-coded value for the article ID, we want to use a dynamic value that we saved right here. How to do that? So instead of using a single quotes as part of the delete method, I will use a backticks right here. So that's a different symbol. It's located to the left from the key one on the keyboard. So you see, it looks slightly different compared to the single quotes. And now I can add this constant as part of my URL. So I add dollar sign, curly braces, and then put the slug ID right here. So now the slug ID will dynamically be passed into request URL. And the second thing I need is, of course, authorization header. Because this request have to be authorized. And then just copy the headers authorization token right here. After we made this request, we need to make a validation of the status code. Status code is 204. So let's go back, I'll copy this. And this is delete article response. Status should be 204. So I believe that's it. And let's try to run the test to see if it is working. All right, that's our test. Let's run it. All right, it failed, 422. Okay, so most likely the article already exists. Let's refresh. Okay, delete the article. And let's try to run it one more time. Okay, and now test pass successfully. And look, now when we run this test again and again, it's gonna work successfully because we're covering the full cycle of the testing scenario. We get a token, we create a new article. Then we get this article to validate that this article was properly created. And then we made a cleanup step using the delete request to delete the test article after the test scenario execution. All right, so let's quickly summarize what we did in this lesson. To make a delete request in Playwright, you need to use a delete method as part of the request fixture. To add additional constants or values as part of API URL, it's convenient to use interpolation. When you use a backticks instead of a single quotes where you put your URL, and then using the syntax dollar sign and curly braces, pasting the constant value. And also you need to provide headers if this is a secure API, in our example it is, to make a secure request. And after that, we perform a validation of the status code that request was successful. All right, that's it guys, and see you in the next lesson.