what up everyone welcome back to the channel Jahmal here and in today's video we're going to
be continuing the cucumber framework series so in today's video what we're going to be looking at is
the hooks so in the last video we use our before and after methods inside of the step definitions
but now we're going to take that from the step definition actually put them in the hooks
so you may be wondering what our hooks and hooks are pretty much blocks of code that can run at
various points in a cucumber execution cycle they typically used for set up interior of the
environments before and after each scenario and that's what we're going to be taking a look at
today using our hooks where we take our before and after class and take it from the step definition
and we're going to create a new class for that because that's the proper way of
using the setup and teardown features all right so we're going to take a look at some of these
examples so you have like the before and this hook runs before the first step of each scenario
and this is how it's going to be annotated and java you also have the after so the after hooks
run after the last step of each scenario even when the result or step is failed on the fine pendant
or skip okay so that's very important pretty much you're going to use it to tear down our driver and
pretty much quit and close it so let's hop into it and what i'm going to do right now is simply
open up eclipse so it's currently on the project that i've been working on on the last cucumber
setup so this is pretty much the given and when then statements that we had in the previous video
so in today's objective what i'm going to do is remove the before
annotation and the after annotations i'm just going to put that in the hooks class because
that's the better place for handling these before and after statements so that's what we're going to
do so what i'm going to do is pretty much create a new driver manager so go to src test java you're
going to create a new package i'm just going to create call this package.com.jamal.managers
you can name it whatever you want click finish from here what i'm going to do is create a
new class i'm just going to call this driver manager so this is just going to handle a setup
of my driver all right that looks good so now we have a public class driver manager from here
what i'm going to do is simply just declare my variable so i'm just going to say public static
web driver driver and then i'm just going to import that driver all right that looks good
all right so what i'm going to do now is just pretty much create a new method i'm going to
call it public static web driver i'm just going to call this method get driver so this is just
going to set the driver and return the driver for me all right so right now i'm just going to
add a if statement and i'm just going to say driver if driver is equal to null we want to
set our driver so i'm going to do is go back to the step definitions i'm just going to copy this
um little piece of code that we had before i'm just going to paste it in this if statement
so to drive us now what we're going to do is set the driver all right and then we're going to
return the driver so that looks great all right from here what we're going to do is simply go
back to the step definitions that looks good what we're going to do is open up our step definitions
package and within the step definition package we're going to create a new class
and in this class we're going to call it hooks it's very important to create your hooks where
your step definition is so let's go what i'm going to do is copy the before annotation from
our step definition and bring it into our hooks class because that's where it really belongs
so now what we have to do is set the driver manager so i'm just going to initiate my variable
so i'm just going to bring back that public static web driver paste it here in my hooks so that looks
good and what i want to do is say drivers is equal to driver manager dot get driver oh
let's try that again so equal driver manager make sure i'm spelling that right that get so
it's not popping up so that means something is not right let me make sure my class is set properly
all right some look like i'm missing the am manager so i have to refactor this class so
what i'm going to do is head over to my package i'm just going to hit the refactor under that
class and they'll rename i'm just going to add that a for the driver manager and click finish
all right that looks good now so i'm just going to go back to my hooks class and i'm just going
to say driver equal driver manager dot get driver and now it pops up all right i'm my semicolon that
looks good so now we have our driver set using the driver manager so now we're going to go is
copy that after annotation and we're going to bring that as well into our hooks and now we have
our setup and theodo which looks great and this is exactly what we want the hooks for so i'm just
going to split my screen so we can see the step definitions the hooks and everything in one view
okay looks good all right make sure you'll save your files all right so now i have my before and
after hooks exactly where i want them so based on you know cucumber this is what we want the hooks
to do but that's what we did all right so now what we're going to do right now is head back
to our step definitions right now our web driver is definitely not what you're going to see is
driver is equal to hook stat driver so we're going to do is bring in the hook start driver
instance into our step definitions so now when the hooks execute and it sets that driver that
driver now is not null or that driver is active and then we're going to pass that driver into
the step definition so everything can function smoothly and then the after class will run and
quit so what we're going to do real now is just run our junit test and see what happens
all right so now it ran everything looks good so now we have successfully used the hooks
to actually set up our driver and actually tear it down because the driver actually quit as well
and that's the purpose of the hooks there's also some other methods that we can use so we just
did the after we can do the before step and the after step so these are typically set up you know
the before is used when you want to set data after step is usually when you want to take a
screenshot so i'm just going to do a quick test on how you can use the after step but
i'm just going to pre-print and statement instead of developing the actual function
i'm just going to create a simple method public void and let's just say take screenshot
all right so in here what i'm just going to do is just import that after step annotation
and next what i'm going to do is just say system.alt that print line i'm just going to say
capture screenshot very simple for now but in a real case you would actually use your screenshot
method to actually capture your screenshot for your important but we're going to be doing that
in another video so after the given when then statements you're just going to print capture
screenshot have to win capture screenshot etc etc so i'm going to do is set up another one
pretty much to be full step so i'm just going to copy this method i'm just going to switch
the name a little bit i'm just going to do before step and change take screenshot to like set data
okay and then i'm just going to change the print statement and for set data and that should be good
import might be full step and now everything looks good in my hooks class and now what i'm going to
do is just simply do another quick run a junit run click ok and now everything should run exactly but
now we should have some of these print statements on our console so let's see what happens
all right test press let's open this up so now we can see to be full step is set in data
the given statement is ran after that we have capture screenshot
etc etc so now we have all our full methods running nicely and that's the pretty much the
gist of the hooks but that's how you can get started if this video was helpful please feel
free to smash that like button subscribe to the channel and i'll see you on another video