Call From Other Files

Now another common use case that you'll come across when you start writing your Karate tests is that you'll want to be able to reuse code or put code into helper methods. So, for example, if there's a certain API Call or certain function that you find making a lot, instead of having to repeat that each time, you can actually put that into a central place. For example, in us, we're making quite a few different calls to the post and we're repeating that sort of logic for that each time, whenever we're creating a new Todo. We could actually just put that into one place and then call it separately. Another thing that we could do is that we could refactor out the code, whereas down here where we clear all of the tasks, this would probably be quite good candidate put into helper methods. Again, if we grew our framework to be lots of different tests, it would be handy to have this code in a helper method whenever we needed to call it. Let's have a look at how we can refactor that out now and how we can then call it from within our Karate main framework.

What I'm going to do here is I'm going to make a new folder and I'm not going to make the folder in examples. I'm going to make it in the Java folder. So make sure you do a new folder in Java and give that a name of helpers. The reason that we're making the folder here is that we don't want this helper. We don't want the code that we're putting in here to be run by this example's runner. So each time when we're executing for Maven at the moment when maven's basically executing this examples test runner and that's just running all of the code, all of the Karate tests that we have in the examples folder. We don't want that helper to be triggered by that. We only want it to be triggered from when we run an example test. You'll see what I mean as we write out the code. But let's just make sure that we put the helpers in the Java folder. Okay. Within that helpers, let's create a new file, and let's call it ResetTodos.feature. We're going to give our feature name. Just call that I don't know, Helper to reset all Todos and our scenario will just be reset all Todos. Okay, now we just need to put in the code. I'm just going to steal it from here for where we clear all of the tasks. Just going to count all of that counts. Close that and then in the reset Todos paste it in there. Let me just fix this formatting quickly because it bothers me. And save that. Let's enhance this a bit as well. Just to print out the number of tasks that get deleted, that's to find a variable to hold the number of tasks deleted. To do that, we can take the response from above and we just grab that deleted parameter that comes back from the JSON. And then let's just print that out.

 So as a print our number of tasks deleted and then print out the number deleted it like so. Okay, that looks good. All that we need to do now is that we need to just make the call within our main Todos feature. We need to make the call here to the reset all Todos. Let's have a look at how to do that. To do that in Kartae, you can just simply do star call and then you just specify the call or the Karate file that you want to read from. We say read and then we need to just specify the path. So the path of this resetTodos.feature, we can just say it's on the classpath because it lives in the source test Java folder and it's in the helper's folder and then it's called ResetTodos.feature. Now basically when Karate, when we execute this, when we get to this point here, it's just going to make this line here that's just going to call this reset Todos feature and it's going to execute the code in here, like say, so we should see in the logs, we should see the number of tasks that get deleted. This print line statement at the bottom that should print out for us. If it's indeed working as we expect. Okay.

 So let me save the Todos feature. Let me check my tags. I've got the ignore tag here on update. That's okay. Then close the config. Let's open up our terminal again and we do mvn clean test. So our test is executed if we take a look at the logs. And we can see here that it deleted the two tasks and it also printed out the number of tasks that got deleted as well. So that's good. Our call at the bottom here is working as we would expect. So that's how to call from another file in Karate. Again, we could do this. We could build on this if we wanted to call refactor for the Post calls, for example, or to Get calls, we could put them into their own methods if you wanted to. But again, I just wanted to show you that example. We could also just take a quick look at the results report just to see what that looks like. And again, here at the bottom of our basic Todos flows, we can see here it made the call to help us ResetTodos. And then again, we have all of the logs printed out for that as well. When we made that get call to do all of the deleting. That's how to call from other files within Karate. 

Comments are closed.

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