Get Requester | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Building a Framework
Instructor: Artem Bondar
Lesson Summary
Summary of the Lesson: In this lesson, we created a custom method getRequest to perform an API request using our custom request handler. Here are the key steps and concepts covered: Creating the getRequest Method Defined a new method getRequest responsible for sending API requests. Utilized a constant URL obtained from the previously created getURL method. Made a GET request using Playwright's request method. Handling Headers and TypeScript Issues Specified headers using API headers and addressed TypeScript type issues by defining it as a Record . Ensured the getRequest method is asynchronous to handle promises with await . Improving the Testing Process Integrated status code validation directly into the getRequest method. Allowed passing the expected status code as an argument, simplifying test assertions. Demonstrated the new structure by creating a test for retrieving tags, which also passed successfully. Conclusion: The getRequest method streamlines the API request process by handling common tasks like status code validation and JSON response retrieval, making tests cleaner and easier to maintain. Future lessons will focus on further improvements and additional features.
Video Transcript
And finally, the moment of truth. Everything that we have done so far, we're going to use in this lesson and we'll create a custom method getRequest to perform a first API request using our custom request handler. All right, so let's get into it. So here in the request handler, let's create a new method that I'm going to call getRequest. So this method will be responsible for actually sending the API request to our endpoint, to API endpoint. So what will we need to perform this request? First of all, we need a URL. So I create a constant, const URL equals to this getURL. I'm going to use method getURL that we have created before to get the URL. Now, let's make a request. Const response equals to this request. This is the field. You remember we assign the request fixture into this field. So we are using right now this field, get and I call well-known method of playwright to make a getRequest. Now we need to provide a URL as a first argument. So I provide URL and then we need to provide other attributes that we need for the API request. So for the getRequest, all we need is just the headers if headers are provided. So I type headers and for the headers, I will call this API headers, another field. We have some red squigglies and what is that? So type object is not assignable to type key string and string. All right. So this is a TypeScript, of course. So we need to update this object. So the headers doesn't like the type of object that we were using for the value of API headers. It's expecting us to have the value of string and string for key and value type. So let's explicitly tell that. So for API headers, we replace with record and type of string and string. Okay. So I'll take this. We also need to update over here, string and string and we have fixed compiling issue. Everything is fine. After we get the request, let's get the JSON response. const response.json equals to await response.json. Okay. Why it doesn't work? Await, I think, okay. We forgot to put await right here and our method also have to be asynchronous because we use await. Okay. We fix everything. So get request is the promise. Remember that it's a promise. So for the promise, we have to use await, but await can be used only inside of the synchronous functions or methods. So this method have to be asynchronous. Response.json and when we execute this method, we want to just return this response.json at the end. Yeah. So let's try to use this method inside of our test. So smoke test, this is the API. So I create new const response equals to, and instead of calling just API, I need to call await API because we're going to use the method which is asynchronous right here. With asynchronous method, we will need to use await. Now, let's build our request. We don't need URL for over here. So I remove it. Request articles limits. We don't need headers. We don't need body. All that is not needed for our request. Let's just make a get request. Let's bring to the console the result, console.log and I'm going to take the response and put it right here. Let's try to run it. Look, everything is working. So we got the response from the API and all our request handler working successfully. So we were able to collect the data as path and the parameters. URL was provided automatically as a base URL. We make a get request and getting the response. What else we can do and improve over here? So instead of making the assertion every time inside of the test, let's give this responsibility also to get request method. So let's this method do this for us. So over here, I'm going back and I'm going to add the assertion. So expect and I need this import inside of the request handler. So right here, import expect from the playwright library going down, expect response.status to equal and for to equal. Instead of hard-coding the value, let's pass the expected value of the status code as the argument inside of the get request method. So status code should be a number, and then expect status code to equal status code like this. Then going back over here and put the status code over here, running the request and assertion was successful. Let's try to change it to 201 for example, and assertion failed. Look at here, so expected 201, received 200. So it works and look, I made a little misspell. So instead of request, need to change it to get request. So like this, and where is it? Okay, 200, running it one more time. Now, let's take the assertions for this test and move it to our new modified smoke test like this. So we can remove console log, put it right here and the response, we change it like this. Let me rename it to getArticles. It's complaining because we did not import the expect library into the test. Okay, let's fix it. Copy and paste, and everything works. Let's run it one more time. Test pass successfully. So let's stop right here and just review what we have so far. So look, in our previous test, we had this type of structure. So we made a get request with a URL with all bunch of parameters. Then we had these two extra steps to extract JSON and validate status code and then the assertions. Right now, we significantly simplify the scripting experience with this new approach. So we just provided path, parameters, perform getRequest and getRequest method automatically perform validation of the status code. You just provide the, hey, this is the 200 status code that I expect, and it works. Also, this method returns automatically the format there in the JSON object format. So you don't need to write this line in every single request again and again and again. It just works, okay? So you just get the response and then add your assertion for validation. How nice is that? Also, it's much easier to read as well. So look, you can see the structure of your test. So this is your request and this is your assertion. Much easier to read. So let's write one more test to getTags. So the test that we have in example.spec, getTestTags, this one. Test, getTestTags. Async, then we need to provide API fixture. Now, const response equals to await API. Then path tags, and for this request, we don't need anything else, just getTags. And getRequest, the expected status code should be 200. And then expect the tags over here. Copy this and paste it right here. Okay, and response, replace with response and response. Run the test, and this test also passed successfully. All right, so that's pretty much the main transformation for our testing approach with modification to the framework. We moved all redundancy and all unneeded steps such as status code validation and retrieving of the JSON object inside of the getRequest method. And if we need other parameters, such as headers or body for the post request, we can just provide those extra parameters with the methods dedicated to that. It makes the scripting significantly more structured and easier to read and down the road, easier to maintain. So that's the main approach. This is just a beginning, okay? We just started. So there are still several things to improve, but overall high level picture, this is how your test gonna look like. So let's quickly summarize what we did in this lesson. So we have created a new getRequest method that is responsible for sending API request. We use the getUrlBuilder and use this getRequest, which is instance of the Playwright API fixture to perform the getRequest, providing API headers from the API headers collected above, if they are provided. And also we move responsibility of status code validation inside of this method, providing the argument of the expected status code right here. So this method will handle for us status code validation as a kind of default behavior. And also at the end of execution of this method, it will return us the JSON object automatically. Then inside of the test body, we can do a standard operations with validations of this JSON object for the values that we expect inside. All right, guys, we just started and in the next lessons, we will continue improvement of this framework, adding a new features. All right, see you in the next lesson.