Another thing that we might want to actually start doing in Karate, start setting certain configurations. We go over to the IDE, if we look at this URL, for example, we are setting this URL here at the top of the feature file. Now, normally in Karate, once you start to grow your framework you'll actually have maybe hundreds of feature files, hundreds of different tests. You don't want to have to set the URL in each of them. The better thing that you could do is you can just set the URL in a central place, i.e. in the config file. Let's have a look at how to do that now. What I'm going to do is I'm going to go over to my Karate config.js. And we can see here in this file config, we're actually already setting an example here just for this for my var name. I'm just going to go ahead and take that out. I'm just going to replace that with the API URL. That's what I'm defining from my parameter. Now I just need to copy in the URL, so I'm just going to grab that from here like so. In my config, I will just paste that in there. There we go. Now we've got our API URL defined in the config. And if I now go back over to the feature. So instead of now having to hard code in the URL, I can just simply replace that with the API URL. Again that URL is now coming from here instead. And I'm bringing that into the background there like so.
What might be good at this point is if we start to refactor out some of these quite large feature classes, maybe that's refactoring out into a couple of different feature classes and then we can set the URL at the top of each of those like so. Let's have a look at how to do that now. Here in my Todos, I'm going to create a new file. Let's just call this one update Todos because we're going to put the update code in here instead. For the feature file, just call this second feature file for Todos the name doesn't really matter too much. For this scenario, let's just call it update and delete Todos. Need to add in our background block as well where we specify the URL. Now we do * Def URL. And again, instead of having to type in the whole URL, we can just use this parameter here instead. Let's say * Def URL is the API URL. Okay. Now what I'm going to do is I'm going to copy all of the code here after we call to get all the Todos.
Let's copy all of this here, update, and delete one. And then I'll just take the update and the delete for now. I'm just going to press command x to cut that. And then in my update Todos, I'm just going to paste that in here instead. And because we're trying to update the Todo with this ID, we actually meet to make sure that we create this Todo first. What I'll do is here this code here where we're creating a single Todo. What I'll do is I'll take this code as well. I'm just going to copy that, I'll paste that in here like so. We don't really need all of these assertions. Okay, we've got our task name here. I've actually put this X in the background. Apologies for that. Let me just take all of that out. Let's move under the scenario block that we go to, that's better. We just fixed this formatting quickly. Okay, that looks better. We need to make sure that we define our task name. Say def task name. Let's call it something else. Say my first task, we don't need all this on the status and the response. Take that out. We don't need to print out the value anymore. That's good. Okay, so let's talk through this now. So we're just creating us Todo here. For the task name we're calling it. We're using this task name parameter, checking that the task name is correct. We then go ahead and update the task name and finally, we delete it as well. We're using our URL up here. Okay, let's go ahead and save that or save the other one as well. And now we can go ahead and execute this as before. Again, we do mvn clean test. We get failure. What is it? I've actually got a typo in here in the background. So I actually don't need a def here. It should just be the URL. We don't need the def keyword. If we look at how we did it here, it's just Def URL. We don't need to define it. Take that out. Let's go ahead and run that again. And this time the test passed successfully. What's another thing that we could do? So if we go over to these Todo features. So at the moment here, this is where we're making the call to get all of the Todos. When we make that call, we get a JSON back that's got basically lots of different JSON objects. Each object represents a different Todo. What we can do is we can do a match on all of those objects that say that we wanted to check the status of all of them is equal to a Boolean, for example. How could you do that in Karate? Let me show you. If we wanted to check all the responses or all the response objects do star match. And this time we want to say each response. So if we look before each time, we normally just match on one response, but this time we want to match each response to check each response object. We want to make sure that each response contains something. We want to make sure it contains some JSON. We put in the brackets. But we don't have to fill in everything. We don't have to fill in the ID, the title, and complete. Karate is actually smart enough If we just put in one parameter it will just look like that's there and that exists. So let's just check that we want to look for the complete. We could make sure that all the response objects are false or true, for example. But because we don't know exactly what they're going to be, let's just check that we're actually getting a Boolean back. To do that let's do Hash Boolean like so. Okay. Again, that's going to match on each of the response objects. Okay, let's just go ahead and run that, and make sure that it works. And it's not actually it doesn't work because we're calling get all Todos. We've actually already got an empty because we at in the last test, we cleared down all the Todos. It's actually getting an empty response back. What we could do is let's just move this scenario call. Let's just move it under here. What we can do here for this get all Todos, what we can do is we can match on every different object that's coming back, basically. At the moment when we're calling get all Todos, we get JSON and JSON the right and it's got quite a few different Todos in. And so what we want to do, is we want to make sure that every one of those objects has a star in field, for example, the complete field. Let's have a look at how we can do that. So this one would say, check all response objects. So can we do a star match now instead of just do a star match response, we can do a star Match, we can say each, say each response. Say check that the check that it contains certain JSON. We don't actually have to put in the full JSON blob so we don't need to put in an ID title and complete. Karate is actually smart enough that we could just put in complete so that we could check that all tasks, for example are set to False. Okay, let's go ahead. And run that. Let's just do mvn clean test. And this time it's passing. I guess that all of the responses were actually set to false. Let me try if I set them to true. Let's run that again. This time we get an error it's because some of the tasks are set to True instead of False. So instead of just checking for true and false, let's just check that we're definitely getting back a Boolean.
So either a true or false for the complete status. Let me run that again once more just to check if that's okay. And the test has passed successfully this time. What might be a good thing to do at this point is to actually check the Karate results report. So each time we've just been looking through the logs. Every time that you run across, you test, it does actually print a really detailed report. We can actually go ahead and open that in our browser. I'm just going to copy the HTML report here. If I head over and paste that in our browser, we can say that this is the results report that Karate generates each time we execute that Maven command. We've got here are two different feature files. So if I look at my Get all Todos one. This one is making the call to get all Todo so we can see all of the HTTP logs. This is really useful when you want to debug and explore your tests. And you can even send these reports to your stakeholders, etc., just to show them how the test execution went. So again, if we go through here, we can see that we've got all of the different calls that we made for this particular scenario.
Let's Get all Todos. If we go back to the summary, we can also see the updated one as well. And again, we've got one scenario here. And like before, we can see all of the different logs in here. This is just really useful to refer to when we're debugging. So let's take a quick pause here. And when we come back, we're going to take a look at how we can start using Karate tags to only execute specific tests.
Comments are closed.