Get Request | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: API Testing Basics
Instructor: Artem Bondar
Lesson Summary
In this lesson, we learned how to write API tests using Playwright . We focused on creating GET requests for our API. Here’s a summary of the key concepts covered: Test Structure The test function is used to define a test, taking a title and a callback function as arguments. For API testing, we use the request fixture instead of the page fixture , which is used for UI testing. Making a GET Request To perform a GET request: const response = await request.get('API_URL'); We must use the await keyword because the get method is a promise that waits for a response. Response Handling To extract the JSON body from the response, use: const jsonResponse = await response.json(); Assertions can be made using the built-in expect library in Playwright. Assertions Examples Check the response status: expect(response.status()).toEqual(200); Validate properties in the JSON response: expect(jsonResponse.tags[0]).toEqual('test'); Check the length of an array: expect(jsonResponse.tags.length).toBeLessThanOrEqual(10); In summary, we created two tests for our API, ensuring we understood how to make requests, handle responses, and validate results effectively. This approach allows for quick and reliable API testing.
Video Transcript
All right, guys, so let's finally write some API tests in the Playwright. So previously, we familiarized ourselves with our API under test. And in this lesson, we will write some of the GET request for our API in Playwright. So open the test project that we created before in Playwright, and we will modify it a little bit and start writing the test. So here we have two test examples. And let's remove the test number two, we don't need it anymore. And for the first test, let's remove the content of this test entirely. We don't need it as well. So now let's talk about the test structure a little bit. So here in the first line, you see the import of the test function from the Playwright library. And then this test function used over here. So test function is the single definition of the test. So the test function has two arguments. The first argument is the title of the test. And the second function is a callback function that pretty much defines the test body, all right? And here we have a page fixture as the attribute. So what is page? For UI testing in the Playwright, page fixture represents a new instance of the web browser page where you would run your test. But we run API test, we don't need page and we don't need browser at all. Instead, we will use a different fixture called request. So type here request fixture, replacing with a page fixture. Request fixture is automatically visible anywhere in Playwright test and it should not be separately imported into the test file. It's just automatically visible. And then inside of the test, you can call this request fixture executing your test. So what we will do, let's open Postman and previously we created these two types of requests, get tags and get articles. So let's start with the most simple one, get tags for our application. So going back, I call request, then dot. And I have available methods for the request function. And the method I'm looking for is this one, method get. Method get, then parentheses. And as the argument for this method, I provide URL for our API. Going back to Postman, copying this API URL and pasting it right here. And that's pretty much it, we can execute it. So if you don't see this little green arrow right here next to the test, click here on the test explorer. So your tests will be loaded right here. So the test explorer, you can see the test files and tests inside of it. After that, you should see this green arrow should pops up. And by the way, let's change the name of the test, get test tags. Okay, something like this. And click on this little arrow, let's see what's gonna happen. So we click on that, and we see the interesting error right here. API request context get target page context or browser has been closed. So why did that happen? Because we forgot to use a keyword await right here. So what is await? So await is a mandatory keyword when you trigger functions which are promises, okay? So okay, what is promise? So promise is the type of function in JavaScript that should return you the result within a defined timeout. So promise has two different outcomes. Either promise is resolved or rejected. When promise is resolved, it means that promise gave you the result of the execution within the timeout which is defined. If promise is rejected, it means the result was not successful and promise was not able to give you the result of the execution within the timeout. When the function is a promise, you always have to use the await command. So how do you know that get is a promise? So if I hover over get right here and scroll it a little bit down, look at this, the type is promise. And when you have a promise type of the function, you have to use await. So when Playwright make a get request, the response is not coming back immediately. It takes some time while API process this request and then return you the response. So Playwright should wait for this response. And this is how promises is used. So get is a type of promise that waits for the response coming back from the API. And you have to use this keyword await to wait for this response. If you're not gonna use the keyword await, then the method get will be just executed and then closed immediately without waiting for the response. Now, good question, how long Playwright will wait for this response? So Playwright will wait up to the 30 seconds for the command to be executed. This is a default timeout in Playwright. You can configure it in configuration settings, but we're not gonna talk about it. It doesn't matter for us right now, just for your understanding that the timeout in Playwright for the test execution is 30 seconds. And the second type of timeout that can be triggered is the timeout on the server side. So usually, all APIs has own response timeout configuration. And usually, it's also 30 seconds, by the way. And if the API was not responded to you within 30 seconds, the API will return you the response that timeout exception or something like this. Okay, so we used a keyword await. Let's run this one more time. And now, everything was successful. So look, this request took us about 187 milliseconds. And we see a past over here, but what's next? So what we can do with this result, or how do we know what actually happened? So to know the result, we need to assign the result of this execution to some constant or variable. Okay, so let's create a new constant and assign result to it. I create a const. And let's call it, for example, textResponse, something like this. Equals to await and textResponse. So now, the result of this execution will be assigned to this constant. Let's check it out what's actually in there. We can put console.log, okay, and put this textResponse. And we will print to the console what is inside of this object. So running the test, and here we go. We see some details printed into the console. So what we see, the response status is 200 and some other, I don't know, understandable for us. This is some headers, this is some other details about this response, not very useful. We need a response object or response body. Coming back, how to get this? To get the response body from the API, you need to call another method from the text response object that we got in the previous step. So let me create a new constant, const textResponse.json equals two. Then I call await textResponse. and method I am looking for is this one, json. Not this one, body. No, it will not give us the json value of the response. The type you see is some kind of promise buffer or something. We are not looking into this. We need a json object, this one. So call this guy, and this is a method. And since this is also a promise, look at this. I hover over, you see the type is a promise. So I need to use method await over here as well. If I will not use await, it's not gonna work. So don't forget to use awaits. And then print this guy to the console. Let's see what's gonna happen. Execute, yay, finally. We have now a familiar response of the text, the same response we saw in the postman. Perfect, so now knowing the response object and knowing that the result was successful, let's add some assertions. How to do that? Okay, for the assertions, you can use the expect library, which is built in in Playwright as well. So you can see the expect is also imported from the Playwright library. It's built into the Playwright framework. So first, inside of the expect, you need to provide the actual result, what was in the response. For example, let's make the text response. And it has a method status to assert the response status code, okay? And then textResponse.status.toBe. And there are different versions for the assertion. We are looking to be, let's do toEqual, toEqual 200, okay? Let's run this test and test pass successfully. Let's try to fail this test. Let's see what's gonna happen. Let's say toEqual 202, run this test. And the test has failed right now. Why? Because the assertion failure, expected 202, received 200. So, so far, so good. All right, let's add some other assertions related to the response object. So what we have in the response object. For example, we can make assertion that the first value inside of the text list equals to test. Let's start with this one. And again, I start with the keyword expect. Expect, then I call the text response JSON object, is this guy. And the first property inside of this object is text, so I call .text. And text is array, you can see the square braces over here. And the first value inside of the array is test. So I add square braces, get the first value of the array. And toEqual, and the value is test. Okay, let's run this test. Okay, test passed successfully. Let's mess with this test a little bit up, for example, like this. And here we go, test failed. It's telling us what's the difference. Okay, this is working. And let's add one more validation. For example, we know that the length of this response should be values of ten tags. So it can be less than ten, but no more than ten. Let's make this assertion as well. So I call another assertion, expect then text response JSON.tags. This is our array. And for the text array, I can call a built-in property which is length method that will return us the length of this array. And then we can compare it either to equal ten, but we want to validate that it can be ten or less. And there is an assertion to be less than or equal. Here we go, to be less than or equal, for example, ten. Okay, and it passed successfully. So that's pretty much it. So we created the first test. So we made a get request. We extracted the JSON response, validated status 200, validated that first value of the tag equals to test. And that length of this array should be less or equal ten. So let's solidify our knowledge and create one more test just from scratch for the second endpoint in list of endpoints, which is list of the articles. And do the same thing. So I start new test, call it getAllArticles, comma, then it's a sync function. Inside of this function should be request and the callback function, and this is our test body. Next, let's create a new constant. const articlesResponse equals to await request.getMethod and getting this URL, get, done. Next thing, we need to extract the JSON. const articlesResponse, call it JSON, equals to await articleResponse.json, and this is the response object. Let's print it out to the console, make sure that it is working. console.log articleResponse.json. Run the test, and yeah, it's passed. We can see the entire object was printed right here. Okay, moving on. So now what we can validate? We can validate that the number of the articles in the array is ten, because we provided the parameter for the URL limit of ten, and it should return us ten articles, so let's validate that. Expect json.articles to be less than equal. We need to call articles.length to be less than equal. Ten, let's run it again. Okay, it has passed successfully. And also, we can validate, for example, this property that articleCount is equals to ten. Let's write one more assertion, expect articlesResponse.json. This is articlesCount.articlesCount to equal ten. So we're validating the value of this property in the JSON response. Let's run it one more time. And this passed successfully. Let's, for example, made eight, and let's see how it's gonna happen, how it's gonna fail. Okay, and the test failed. Expected eight, but received ten, so our assertion works correctly. Moving back to ten, and I believe, by the way, one more thing that we forgot to add at the status code validation. Expect, and we getting this article response, not article response JSON, because JSON doesn't have information about response status code. We are looking for article response. So articleResponse.method.status, and it should equal to 200 as well, okay? And I remove this, and this is our final test. So let's run this one more time, and it works together. So we created two tests. So now let's run both tests together. How you conveniently can do that? So here on the Test Explorer, you can click over here on the left, and you see this list of your available tests right now. You can click on this little button, Run Test, and it will execute both of your tests. Well, that's it. I didn't complete my phrase, but this is executed. Look, the speed of execution, 475 milliseconds, super, super quick. This is why API automation is so much valuable, because it runs very, very fast, and it's very reliable at the same time, much faster than running UI test or something like that, okay? So we created two tests. Let's make a quick summary what we did in this lesson. So when you want to make API requests in Playwright, you have to use a request fixture, which is default fixture in the Playwright. And this fixture has different methods to interact with the API, such as get, post, put, and delete, and so on. Get method is a promise. So promise is a type of the function that returns you the result within a particular timeout. And when you call a promise, you need to use a keyword await always for that. To extract the JSON object from the response, you need to call the JSON method on the previous response that you got from the API. That way, you will get a JSON object that you can later work with. And to make an assertion, you use a built-in expect library, which is built into the Playwright framework. The first argument in the expect is the actual result that is coming back from your API, and then you call in a different methods. For example, to equal or to be less than, or something like that, whatever your requirements, finding the right validation point for your API response to validate the information that you need inside of your API. All right, that's it, guys. Two tests were created, and we're moving on to the next lesson.