In this video,
we're going to take a look at how we can add both before and
after hooks within our Karate framework.
So if we start first by focusing on before hooks,
i.e., code that runs before our scenarios are executed.
So at the moment, we have the Background block,
and we've been putting code to run here before scenarios.
And in the previous video,
we added this testDataGenerator again to run before all of our scenarios.
So instead of just putting this directly in the Background,
it might be better if we put this into another feature file and
then just bring that feature file in instead.
So in the helpers folder, I'm going to create this Hooks.feature file.
I'm just going to give it a name of Example of Hooks,
and I'll give it the same name for the scenario as well.
So in the Scenario, again,
I'm just going to do the same code just to create the testDataGenerator.
So I'm going to define a parameter for the testDataGenerator,
and that's going to come in from my Java class.
So we'll do Java.type.
We'll do the helpers folder, and then we want to get the testDataGenerator class.
We'll then add another parameter to hold our productName,
and we'll do testDataGenerator.getRandomProductName again.
Okay,
so let's now use this Hooks.feature file in one of our other feature classes.
So let's use our Categories.feature now instead.
So what I want to do is in the Background section is that I want to
bring in the other feature file that we just created,
i.e., the Hooks one.
So we're going to define the new parameter here for test data.
Then we use the Karate call words,
and we want to read from the Hooks.feature file.
So to do that, we provide the path to the file that lives on our classpath,
it's in the helpers folder, and the file is called Hooks.feature.
Now we can declare another variable to hold a productName.
We can call our testData parameter that we defined above.
And from that,
we have access now to the productName that lives within that feature file.
So this is going to run before, and it's going to give us this produceName,
which is going to have a random product assigned to it,
which is going to come from our testDataGenerator.
So let's now use this productName in one of these scenarios.
So I'm just going to use this Get all categories scenario.
Because this one doesn't actually use products,
what we'll do is we'll just print out the productName
here just to see how this works.
Now because we actually print this out twice,
so let's just create a second, very simple scenario.
So let's just do a scenario that just gets a single category.
It doesn't really matter what the scenario is in this case.
We just want to add something simple so that we can do another print message.
And here again, we're going to print out the productName value as well.
So the reason that we're going to try and print this out twice in
separate scenarios is that we want to see if the productName value
changes basically between the scenarios.
So I want to go ahead and execute these two scenarios now.
So I'm going to add a debug tag above both Get all
categories and Get a single category.
I'll save the Categories.feature file.
And then I think I also had a debug tag set in the
Products.feature from previously.
So I'm going to take that out and check that there's nothing else.
And I'll save this Product.feature and close that down for now as well.
Okay, let's go ahead and execute this.
So I open up the terminal and use our normal command
to execute with the debug tag.
And if we take a look in the logs,
we can see here we've got these two different names for the product.
So that means that the method to generate a product was called twice,
and each time it was called, it was called,
and it generated a different product name.
So remember,
if we only wanted the test data to be called once and we wanted to
set the same product name for all of our scenarios,
we could just change this to call once.
So if I open up the terminal and just execute the same test again,
and this time, the productName we can see is the same for both of the scenarios.
So we've seen a few examples now of how to run code before a scenario,
but let's now have a look at how we could run code after
a scenario or after a feature file.
That's something that we haven't actually covered yet.
So if you want to run some code after a scenario is executed,
what you can do is * configure and type in the parameter name afterScenario,
and then you can assign it just to a function.
And then, within the brackets,
you put the Karate code that you want to run afterwards.
So here, in this case,
I'm just going to run karate.log Scenario complete just to print
out a message after the scenario executes.
So it's important when you declare this that you do call
the after scenario this parameter name, i.e., afterScenario.
Otherwise,
Karate won't detect that this is what should run after a scenario is completed.
So let's try this out quickly.
Let me open up a terminal.
I'm going to run our test as usual.
When we take a look in the logs,
we've got this Scenario complete message being printed out twice.
So it's being printed out after each scenario is executed.
So that's how to run a code after each scenario.
What about if you want to run some code just after
the whole feature file is run?
Let's have a look at an example of how to do that.
So it's very similar.
This time, we do * configure and then afterFeature.
And it's very important that you call your parameter afterFeature,
if you want Karate to detect that this should run after your feature file.
And this time, I'm going to actually execute a couple of different commands.
So to do that, I'm going to use three double quotes,
then we use our function calls before, and add our brackets for a function call.
So the first thing I'll do is I'll just log again.
So I'll do karate.log Feature completed.
I add a semicolon at the end of that because I want to
do something else again afterwards.
So let's say we actually wanted to call another
Karate file at the end here as well.
So I'm going to create a new file for us to call.
So in helpers, let's create a file, and we'll call it PrintMessages.feature.
This is just going to be a really simple feature file.
It's just going to put out some text.
So I'm just going to call the feature Example of
printing message from other file, just give it a scenario name of Print message,
print Example message from other file.
Let's save that and go back over to the Categories.feature.
And now we want to call this file.
So we do karate.call, and then we just put in the path of the feature file.
So it lives on our classpath, it's in our helpers folder,
and the file is just called PrintMessages.feature.
Remember to add the semicolon on the end here as well.
So now when we run this, we should get two scenario complete messages,
and one message after the feature completes.
Let's try and run this now.
If we take a look in the logs,
we've got the two Scenario complete messages here and
then the feature complete message,
and also the printed message from the other Karate file.