Request Handler Improvement | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Building a Framework
Instructor: Artem Bondar
Lesson Summary
In this lesson, we address an issue in our request handler that causes parameters from one request to leak into another. This occurs when executing two tasks: getArticles and getTestTags . Problem Overview When running the getArticleTest , we noticed that parameters from the first request (to get articles) were unintentionally carried over to the second request (to get tags). This happens because the values for the request handler's fields are not reset between requests. Solution Steps Create a private method called cleanUpFields to reset the request handler's fields after each request. Set the following fields to their default values: API body : empty object API headers : empty object base URL : undefined API path : empty string query params : empty object Adjust the type for base URL to accept both string and undefined . Invoke cleanUpFields after every request method: GET , POST , PUT , and DELETE . Testing the Fix After implementing these changes, we reran the tests. The first request executed correctly with parameters, and the second request to the tags endpoint no longer included any leaking parameters. This confirms that our fix was successful. In summary, we effectively resolved the issue of parameter leakage in our request handler, ensuring cleaner and more reliable requests in future tests.
Video Transcript
Hey guys, in this lesson, we will make a little improvement to our request handler because currently the design that we are using has a little side effect that we want to fix. Okay, so let me show you what I mean. So here we have two tasks, getArticles and getTestTags. So let me copy temporarily just these steps and I will add these steps to this test. So getArticleTest will make, so let me fix it like this, response to, response to, response to. So getArticleTest will first make a request to get the articles and then make a request to get the tags. And let me fail this test so we will see the logs. For example, I put 5 and we'll run this test. Running the test and test failed. So let's open the report. Open in default browser. Opening the logs and look. So first we made the request over here, articles with the limit offset with parameters, everything is fine. We have a response and then we have a second request which is here to the tags endpoint. But look what we have here. We also have parameters attached to the tags endpoint but it should not actually happen. What we do in the code, we just request, hey, get the path with the tags. We did not request the parameters. What happens is the parameters from the previous step are leaking into the next test. It happens because over here in the request handler, let me open this guy, so we initially set the values for the instance of the request handler to these fields. And if we don't call these fields again in the next step, we technically don't override the values and the values while the test is running are still saved in these private fields. And if any internal method calls this field, then of course the value is available. So what we need to do is just a little fix here that after every request, POST, PUT, GET or DELETE, just clean the fields and restore the default values for the fields right here. It will be pretty simple fix. So going back to the top of the file and I will create a private method that I will call, for example, cleanUpFields. And this method will just set the default values for the fields that we are looking for. So we need to set API body to empty object. This, what else, API headers to the empty object. This, we need base URL, we need to set as undefined. And we need to set this API path is, what is, I think it's empty string, right? API path should be just an empty string. So, okay, do we miss anything? So body headers, okay, query params. The last one is query params. This query params should be the empty object. Okay, and we have as quickly that type undefined in us, not assignable to the type string. We need to have it undefined because we have a logic right here that the base URL, if it is undefined, then use the base URL. So we can fix the type right here and set that base URL should be either string or undefined, like this. So both values are acceptable. And looking over here and right now everything is fixed. So that's it. And now we just can call this method at the end of every method responsible for the requests to clean up the field. So let's start with the get request. We want to do it right after the request. The request happens right here, where's API this request get, and we want to call this method. This cleanup fields, and I call this method for every other test. Here, then here, put request and delete request. All right, that's it. And let's try to test it. Did it work or not? So running the test, test failed, open the report, open in default browser, like this. So this first request as expected with parameters, looking for the next request, and now it is fine. So we have clean request with tags and point without the leaking query parameters from the previous request. All right, so that's the quick fix, and see you in the next lesson.