Karate API Testing In Action (Demo)

Great. So we've seen the theory, we've seen what Karate looks like and some of the features that make Karate tick as an API test automation tool. So I'm going to now jump across to the Karate website. The Karate Labs website has been organized into very easy to access resources. I'm going to go straight ahead to Visual Studio Code here and I click on Visual Studio Code. I'm landed straight on our documentation page, and this is designed to be a very easy to follow Quickstart, especially if you don't have any programing experience.

You can follow along and get started really quickly and you can write your first API test in literally less than 5 minutes. What I've done, I've already, for the sake of simplicity, I've already installed Visual Studio code installing the extension. It's quite easy. Let me open Visual Studo Code for a moment and remove all that I hide. When you go to Visual Studio Code and you click on the extensions tab on the left, you can search for extensions like this by typing in Karate.

 Make sure that you choose the official Karate plugin. Here, you can see that it's got a tick here that say it's a approved official plugin from Karate Labs. It will be simple, it should take you less than a minute to install this. So once this is installed you can create a fresh folder anywhere on your local machine. And what I'm going to do is create a new file and we need to create call this file something dart feature dart feature, which is the same as Cucumber by the way. This is the extension that you use for Karate tests. I'm just going to call this test.feature. It doesn't really matter for now. And to help you, we actually have the feature and scenario keywords and now I am going to hop back to the tutorial guide that we have on the web page or the wiki.

So we have already created a project folder. Just create one and I'm now going to cut and paste these. It's actually five lines of code. And one way to do this, if you click on this icon over here, you can actually copy that entire piece of text onto the clipboard, go back to your Visual Studio code feature and I'm just going to do a select all, remove everything and then paste what's there from the wiki page. And let me explain this, the first thing we need is the URL.

By the way, you notice that we are not using the Gherkin given when then because we're just trying to keep things simple. All we going to do right now is call this URL. Append the this path parameter users to this URL and make a get request in terms of HTTP. It's that simple. In fact, you could even compress this into two lines if you put the path into the water. But there are reasons why the path should be treated separately. One good reason is in the future, if you wanted to switch this URL to do another URL because this typically is done when you switch environments like staging pre prod and production.

But for now, they have URL hardcoded and when you want to get into deep advanced areas of Karate, there are many ways in which you could manage and define variable. Keep that in mind. Right now it's simple. And to repeat it just takes three lines to make an API call. If you click run over here at the top, you can actually execute this call. If this URL happens to be a public URL where there is a test API. And you can see that there's a bunch of things that come to the log. In fact, the response is quite big.

 Now, if you go to the last line of the log, you can actually click and open in a browser. The Karate HTML Report, the HTML report that's built into Karate is really great. You can see that it shows you the three lines of code that was part of your test. But now when I expand this line over here, which is where we actually made the request, which is the method keyword in Karate, you can see the actual HTTP traffic in line. You see all header, all the low level technical details. That's one great thing about Karate.

Once you see a Karate test report, you actually have all the information you need to understand what its HTTP call was made. Where it was made and what came back. What did we get back? We, of course, got a 200 Okay. And a whole bunch of headers. But we actually got a large JSON response, which is an array. Again, one shortcut, if you see a square bracket like what I've highlighted, that's a JSON array. And if you see a curly brace, that's the beginning of a JSON object. So you see JSON, the double quote and a lot of data coming back.

Now the next thing we need to do is do assertions. Keep in mind what I said earlier, which is automation is easy. Yes, we are right now automated the calling of an API, but we did not do any testing. We just made a call and we stopped. What we really need to do, and it's explained in the wiki documentation of the next step is to add assertions. Okay. Let me start with a simple assertion, which is status 200. Okay,

Now we are doing a little more than just automation. We are starting to do testing. We are actually ensuring that when we made this API call we got a status 200 back. If I run this, it should pass, which is great, and everything looks good in the report. But let's see what happens if I give a different status. If I just say 404, you can start seeing Karate's ability to actually fail a test. And if I go to the HTML report, one of the nice things about the HTML report is that you can just refresh and see the effects of tests. So you can see Karate actually saying, oops, your assertion failed. This is not what they expected.

Let's go back to 200. Now, if we study the response, we as I said, the response is actually an array of objects. The first object is this It's got a whole bunch of values in it, ID name and so on. And what we're going to do is do a slightly more complicated assertion. And the assertion which you can cut and paste from the guide is we're going to match that. The first element in the response array, which has index zero, contains I.D. name, and website with these values.

Okay. And if you look at the actual response, you see, yes, that's the name. That's the ID, which happens to be a number. It doesn't have quotes around it. And, Yes, the website is over here. And the nice thing about JSON and Karate is that when you use the contains keyword so I'm going to paste this next line over here. When you use the contains keyword, we are not testing for all the values, we just testing for the values that we care about. This is great again. Karate makes it simple for you to only focus on the data or the business values that you really care about for testing when you are in a particular API testing context. Let's run this again. It should pass the 200 Okay. And we are doing an assertion.

Okay. And of course, if I reload the report, you can see the test is getting more complicated or this is getting to be a property API test because we have assertions. And let me show you what happens if I fail the assertion. If I let's say I deliberately make the name assertion fail. If I read on the test, yes, you do get a fair amount of diagnostics on the console, but the HTML report of what users really like to use for debugging tests, to be honest. And the nice thing about Karate is it would tell you exactly which field. And keep in mind, this was a rather a complicated JSON response. That's a large Array that came back, but we only focused on one Array element to do an assertion. And because our assertion failed, Karate told us exactly which part of the JSON did not match. And you get a lot of helpful information in the report.

Comments are closed.

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