Your First API Test

Okay, let's go ahead and start writing our very own Karate test now against the Karate Todos application. Back here in the IDE, I'm going to use this examples folder. I'm going to create another folder in the examples folder and then just call it Todos, just to represent our Todos application. And then right-click on that new folder. Then let's create a new file called Todos.feature. We can see here when we create a feature class in our IDE because we install that plugin because we installed the Karate plugin here, it actually auto-populates us where feature and the scenario keyword for us straight away. The feature file that's basically just that's what we're going to use to group together. Lots of different test scenarios. For us here, let's just give our feature folder name. Let's just called it Karate Basic Todos. For our scenario, this is where we actually need to define where we define the test. We define the test steps. So first we need to give our scenario a name. Let's just call this one get all Todos. And then in that scenario, we actually need to define the steps to make our API test. So can we use the given when then syntax? The idea is that you say, given something that does the setup of the test, the when part is then the actual execution of the test, and then the then part at the end, that's where you do the assertion or the check on the test just to make sure that the check completed. Let's write out some code just to have enough an example of that to be write the given keyword here. And what we need to do is we need to put in a URL and this is what we put in the URL of our API basically. So our API lives http, it's running on localhosts, running on port 8000. Sorry, I think it's running on port 8080 not 8000 and our endpoint is API/Todos. Now, I need to do when method Get. So with this which means we just want to make a Get call to this endpoint. And then we want to check the status that we get back. We just want to make sure that we get a 200 back. A 200 response code status basically just means that the API call works. Okay.

I just make sure that I save this file we've come on this and now we are ready to go ahead and run this API test. So to do that, I can just click run on here if I had the Karate plugin installed. But because I don't have a subscription for the plugin, we actually need to run the tests in a different way. To do that, I'm going to open up a terminal and in the terminal, all I can do is just type mvn clean tests to just run the test through Maven instead. We can see here that the test. Is run for okay. And if we look at the logs here, we can see that all of the tests are passed all of our one test even though we've only got one test that's passed successfully. We can also see here the response that we're getting back from the API. So this looks similar to what we looked at in Postman again. We can see that we're getting this 200 response back when we're calling the API. So let's just check that it works. What about instead if I was looking for a 202 status, let me save that and then run the test again. And this time the test fails and it fails because it was expecting a 200 response code, but it expected a 202 response code, sorry, but it got back a 200, which is what we didn't expect. So that's good. Let's change that back to a 200 like so. We closed down the terminal. Okay, That was good.


Let's go ahead and start writing some more tests, shall we? I'm going to create a second scenario here. And this one I'm just going to call Basic Todo flow. Let's start writing some Karate tests in here. I'm just going to add a comment in here, just so I know which test I'm writing, which tests I'm referring to in the code. Let's call this one Just create a single Todo. So in order to create Todo, this time, we need to make a post call means make a post call to our API and that will create a new Todo in our application for us. Normally what we would have to do before that, we would have to specify the URL. But I don't really want to have to keep specifying the URL and every scenario that's around. There must be a better way to do that. And of course, there is. So to do that, what we can do is we can add in a background block and basically the code in the background block, will get run before every single scenario. What I can do here is I can just define the URL and I can define that by saying * URL and then just put the URL in there like so. Now, I don't need to specify the URL each time because I put in the background. I actually only need to set it once and that will get set automatically in all of the scenarios. Now this star here, what this just does in Karate, this is just a special word that replaces a given when or then. So actually, all of these given when then I could actually replace all of these just with a star. We just leave them in our scenarios just because it makes our scenario a little bit more readable. But in the background block, I can just put star to define the URL instead. I don't have to put given when or then. Let's carry on with creating our Post call to create a single Todo. So for this one we need to send a request body. Similarly to like we did when we looked for a Postman. What we can say is given request. And here we just need to put in a JSON blob, which is just a post request that we're going to send to our API. So our request body is going to look like this. This is very simple JSON blob which just going to have a title first. And it's just going to have a complete status of False. This is the JSON that we're going to send up to our API. We then do when method this time we do a post. So last time we did a Get call. This time we're going to do a Post. Let's also check again that the status that we get back from the API is a 200. Okay, that looks good. I think we can just go ahead and run this just to make sure. That it's working. I'm going to open up my terminal again and again do the Maven clean test. We get a build success message back. What I'm going to do here, I'm actually going to open up the application and we can see here that our first task is actually being created when we made that. API call. We've also just run this again a second time. That's the trigger again, the same call. This call here, that's going to create a new task. If go back over to the application again, refresh and now we've got two other tasks. We know that these API calls are working and that they're creating tasks for us. The moment we're just checking on the response code, which is checking that we're getting a 200. What we can actually do is we can actually check some more of the content of what we're getting back from when we're making that Post call.


You can see that when we made the Post call that we got this response back with the title, the complete status, and also the ID. We can actually write an assertion that's also going to check for that as well. So to do that, we can say and match and response because we want to take the HTTP response. We did double equals and then we want to make sure that response equals a particular JSON blob. The first thing that we want to check is that the response has an ID because this ID is basically just a randomly generated string. We're not going to know. We're not going to be able to check on the exact string, the exact value of the string. What we do just want to check is that we just want to check that the response is an ID and it is a string basically. So to do that, we can just use a special Karate Hash here and we can just say that we want to make sure that the ID is of a string. Now for the other things like the title and the complete, we can actually be more specific, because we know that the title is going to be first. We also know that the completes were expected to be false. We can actually explicitly type those in instead. Okay. I've got my title and my completes and this is my JSON blob that we're getting back. Let's go ahead and run this again just to check that it's still working. And the test passes again. Let's just check that it is definitely still working. So if I was to change this ID, let's say that we were expecting a number instead of a string, we go ahead and run that again and this time it fails. It fails because the ID wasn't a number. The ID was coming back as the string. So that's good. That means that working. Let's going to change that back over to a string. Now, another thing we can actually do here is we can actually capture some of the response that's coming back from this API call. So say in the next call say that this call was creating a single Todo, let's say that we wanted to make a follow-up call to actually get a single Todo. In order to make that Get call, we need to grab this ID and we need to save that into a parameter. Let's see how we can do that. To do that in Karate I can just do * Def. I'm going to name my parameter ID. And then the ID is just going to come from the response and then from that response, we're just going to want to get that ID. Basically, the ID that comes from the response. We could capture anything from the response here, we could also capture the title. So to do that, it would do response.title, we wanted to capture the status as well. We could just say response.complete. In Karate we can also print out these values as well just to make sure that we're capturing them. So just do * Print, let's say, value of ID, put a double quote, say value of ID and then print out the ID like that.


We could do the same for the title and the status. I won't do it here or just do it for the ID for now, let's go ahead and run this again. So again, we do Maven clean test. The test is passed successfully. If we take a look at the logs, where is it? We can see down here the value of the ID equals this, which is the ID that came back from what's the ID that basically gets generated for that particular task? Let me close that down. Let's go ahead now and make a second API call. In this scenario. So far, we've only got just this one call here. This is one call here that's making the post request. Let's go ahead and add another scenario. So let's say get a single Todo. This one, we say given we don't need to specify the URL because that's already included in the background. But what we do want to do is we want to parse in the path i.e. that ID. Remember that we're going to get a single Todo we want to call this URL. And we want to do another slash and then with the ID of the URL that we want to get, that was what we saw in Postman previously. So to do that Karate, I can just do given path ID. And then the ID that's going to get appended up to the URL I get. Given path ID which is to when method Get.


So just making a Get call. Let's again check that the status is 200, that's the match on the response as well. Similarly so like we did above. Now this time when we're checking the ID, instead of just doing checking for a string, we can actually check for the actual ID because we've already got that saved in this ID parameter. So to do that, we can use hash open brackets and then we just put in the ID. Again, that's the ID coming from here, the one that we've already saved. We're going to check on that. We're going to match on that on our response just to check that they actually exist. Also, check on the title as well. Again, that title should be as before. So the title will be first and the complete status should still be False as well. But it's good. Let's save that. Let's go ahead and execute again. So again, I do Maven clean test. And our test is passed successfully. Again, if you want to change that, I don't know, I could just add a few Xs here onto that first. Run the test again. And this time the test has failed, as we would expect, because the source was wrong. Let's just change that back. That's how you can put two different calls. And you can actually capture something from one call and chain it onto the next one.


Let's keep going. Let's do another call. This time we're going to create a second Todo. So we're going to do another Post call. So here what we did when we created this Todo, we put our JSON, we put it just in line in the request like so. But what we could actually do is we could just define the JSON separately and parse in as a parameter. Let's take a look at how to do that now. To define some JSON, I'm just going to do * Def Todo equals. And we're going to do 3 double quotes just because I want to put a blob of JSON in here. In that Todo I've just put this blob of JSON. It's similar to before. But this time it's just got the title of second and the complete a False. I've also put in the proper double quotes for this JSON as well. When we did the in-line blob here, Karate took care of the double quotes for us. But this time, because I'm putting it in this triple-double quote. I want to make sure that I have proper JSON either I'm including the double quotes here. Now we can write our Karate test before. So we're going to say given request. And again, instead of putting in a body like this, I can just put in this Todo instead because that's got our JSON for us. Let's say given the request Todo. And another thing we can do here is we can ad HTTP headers as well, so we don't strictly need them for this API just because it is quite generous in that we don't need to supply many headers, but it's quite common to need to supply HTTP Headers particularly when you're providing different content types like JSON and XML etc.. If you wanted to add in a header, we can just say, And the header, let's say our header was called content-type and a common content type. And the one that we're sending is application/json. So that's just a simple example of how to add headers in Karate.


Okay, let's do When method Post and we'll check again that the Status is 200. And then let's just check in the response. Let's just check this time that the title is equal to second. So to do that, we can just do And match response. Just call the title of the response and Karate takes care of all the formatting for us just to grab out the title from the response. We can just check that it's equal to second. It's good. Let's go ahead and run this again. Okay, that looks good. And our test is passing, so that looks good.


Now, we've created a few Todos. Let's make one more call to get all the Todos, again. This time we don't need to provide any given because all that we need is the URL and we've already included that in the background. So we can skip this given we can just say straight away, When method gets we're just making a Get call to this URL.


Let's again check that we get a 200 response back. And that's also just grab the JSON for the first task as well. We say start the first task. Let me just take our response and then we would expect our response to have quite a few different tasks in it. So I'm just going to take the one that 0. That's just going to be our first task. And now we can do some matches on that as well. Just to check that is what we expect to say match first task that say the firstTask.title. Let's make sure that it's equal to first. And also match the first task. Oops, firstTasks.completes. Let's check that that's equal to False. Okay, that looks good. Let's go ahead and run this one last time. And that looks good. You can see in the logs that we're making these API calls, we're getting all of this JSON back here. It's got all of our different tasks that we're creating. I jump over to the application and refresh. You can see here now that we're populating it with lots of different tasks. Let's take a quick pause here. Then when we come back, we'll have a look at how we can add some other HTTP calls, i.e. to make the update and delete calls.


Comments are closed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}