Request Handler Constructor | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Building a Framework
Instructor: Artem Bondar
Lesson Summary
In this lesson, we modify our request handler to enable API requests by passing a request fixture into the handler. Here’s a breakdown of the key steps: Modifications to the Request Handler We need to create a constructor in the request handler that defines required parameters for creating a new instance. Two parameters are required: request : of type API request context API base URL : of type string Remove the hard-coded URL from the class and pass it as an argument during instantiation. Updating the Fixture In the fixtures.ts , we need to pass the request fixture as an argument to the async function. Create a constant for base URL and pass it as the second argument to the request handler. Remove the hard-coded default base URL from the class and assign it through the fixture. Summary of Changes We successfully created a constructor in the request handler that accepts two parameters: the request fixture and the API base URL . These parameters are assigned to class fields for later use. The fixture was updated to accommodate these changes, allowing us to pass the necessary arguments when creating a new instance of the request handler. In the next lesson, we will begin making actual API requests using our custom request handler.
Video Transcript
All right, guys, in this lesson, we will make one more modification to our request handler to pass the request fixture into request handler to be able to make finally an API requests. So let's get into it. So this is our request handler. So far, we have several methods that collect the data for the request and get URL to build the API URL. We need to pass a request fixture, so this little guy into this request handler class to be able to perform those API requests request get, post, put, and delete. So how can we do this? So this global request fixture is available for us in two places. In the test body is an argument for the test and inside of the fixture. We also can create it if we create a new API request context, but we're not going to go that route for now. We're going to use a default request fixture for API request. So we need to pass this request fixture as argument inside of the request handler, and we can do it from the fixtures.ts right here. But before that, we need to slightly update the request handler. So in the request handler, we need to create a constructor. So constructor, and then defined which arguments are required to pass into the request handler to create a new object or create a new instance of this class. So this will be request fixture of type API request context. This is the correct type. Also, let's add second argument and we will remove this URL, which is hard-coded right now inside of the class. We will move it out and pass into the class when we create a new instance of a class. So I create API base URL and type will be a string. So after we created constructor, we need to assign those parameters to the field. So we need to create a new field, private request, and type is also API request context. For the base URL, we will use this default base URL. Inside of the constructor, I type this request equals to request, and this default base URL equals to API base URL. I made a little misspell here, request.equal to request. So I think you got the idea here. We pass two arguments into our class and assign those two arguments to the class fields. Now, when we go back to the fixture, we see it's red squigglies because currently our class requires two arguments. So the first argument will be a request fixture. Where can we get it from? We can pass this request fixture as the argument to async function of the fixture and then automatically pass it down into the class like this. And second argument, we need a base URL. And let's just take this base URL, remove this hard-coded value from the field, which is assigned by default, and we remove it to the fixture level. So const base URL equals to, and we put it right here, and then we put base URL as a second argument. All right, now everything is good and we can go back and remove this hard-coded value from the class. Now, this value of default base URL is passed into the class when we create a new object right here on the fixture level. And the value will be this one. Later in the class, we will refactor this a little bit one more time. But for now, we're gonna keep the base URL over here inside of the test fixture method. So I guess the modification is done. So let's quickly summarize what we did in this lesson. We created a constructor inside of the request handler. So constructor is a method that defines required parameters that have to be passed into the class when we create a new instance of the class. We pass two required parameters, which is request, which is a request fixture to perform API request and API base URL. Those values are assigned to the fields. So later, we can use the values assigned to the field inside of the class for different operation. And inside of the fixture, we update the fixture a little bit as well. So we added request fixture as an argument to API fixture. And we pass this argument down into the request handler and created the base URL constant. And I'll remind you, we're gonna refactor it a little bit later. But for now, we're gonna keep it like that. All right, I believe everything is ready. And in the next lesson, we will finally start doing API requests using our custom request handler. All right, see you in the next lesson.