Selenium Cucumber Java BDD Framework 9 - Hooks | Step by Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session i am raghav and you can find all my work on my website automationstepbuster.com in this session we are going to go very basic from scratch step by step and we will learn what are hooks why to use hooks when we should use hooks and we will also see a very basic step-by-step demo on how to create and use hooks we will also see how we can use tags with hooks conditional hooks a scenario hooks step hooks and how we can order hooks and then we will see some useful tips so let's get started and let us start with what are hooks now in cucumber we have seen in the earlier sessions that we create scenarios so let us say that we have a scenario to check the login credentials or check login with valid credentials now before we actually do the login steps that is we log into the application we have to do some prerequisite or we have to do a setup that is we have to set the browser and open the browser so let us say this is kind of a setup for us and then after the scenario is over we have to close the browser now these two steps are these two setup and tear down actions we have to do with every scenario suppose we have another scenario to check login with invalid credentials even before this login steps we have to open the browser and then we have to close the browser so whenever you have these common steps like setup and teardown steps that you need to run with every scenario you will either have to code them within the steps and add them as the steps of the scenario a better way is we can create a function a common function for open browser and close browser and then we can annotate these functions with at before and at after annotation and i will show you this in a demo in a moment so you can create two common functions and give these annotations and then you can have any number of scenarios these two functions or whatever code you have written in these two functions will run before and after each scenario whenever you run your feature so this is what actually hooks our hooks are functions or blocks of codes that runs before and after each scenario and then hooks in cucumber are like listeners in test ng if you know test ng there are listeners that where we can have set up and tear down that runs before and after each test or each class similarly we have hooks in cucumber and hooks can run before and after each scenario and also before and after each step in a scenario and i will show you that in a demo so you can define hooks by using these annotations add before and at after and then we have scenario hooks we also have step hooks so scenario hooks runs before and after each scenario and step hooks runs before and after each step and then we have conditional hooks where we can associate hooks with tags and then do conditional execution i will again show you this in a demo so this is what hooks are now with this knowledge let us move forward and learn why exactly we use hooks so as we have seen we can do the management of setup and teardown actions in these functions which are annotated with at before and at after and these becomes hooks and it will also help us in avoiding to create or write for these common setup and teardown actions again and again into our features and steps so we will only write the function once and use it as hooks and it allows for the better management of the code workflow so this is why we use hooks and before we go to the steps of how exactly we use hooks let us see when we want to use hooks so whenever you have any common setup or any action that you want to run before and after each scenario or before and after each step in the scenario you can always use hooks so this is about what are hooks why do we use hooks and when we use hooks now let us go to our demo and see how exactly we can use hooks so the first step is we have to create a new feature file you can also use any existing feature files so i will go to my windows system you can also use it on mac and i will go to my eclipse and here is the project that we have been using in the last sessions so i will first go to src test resources folder and here i can create a feature file so for documentation purposes i am going to create a new feature file and i will also create a new folder so i will do a right click here go to new and say folder and i will say hooks demo and finish and inside this folder now i will again do a right click and say new and i will create a file i will select file from here and i will say this is hooks demo dot feature so this is our feature file and i will say finish and it has created a feature file with some default data i will remove everything delete everything and i will create a fresh feature so i will say feature and i will say check login functionality and then i will create a scenario and here i will say given user is on login page when user enters valid username and password and clicks on login button then user is navigated to the home page so this is my scenario and this is the feature i will do a right click and say pretty format to correct the formatting and i will save my project so i have created a feature file and have created a scenario in the feature file so this completes step one now step two is we have to create the steps for the scenario in the feature file and then we also have to create the test runner so let me just first create the test runner file so that i will have this step definition then i will have the step definition created so let me just go to src test java folder and here we have the step definition and we also have the runner file so i'm just going to create a runner first if i go to any existing test runner this is how we have the test runner file or test on a class i can use the same class or i will create a new one so in my step definitions package i will do a right click and say new and i will create a class i will say test runner for hooks now i am creating a new tester just for documentation purposes so when you have this project i will put this project on github and if you get this project from github you will have all these testers separately created in the project and i will say finish and i can copy this code which comes before the class name this i will just copy from an existing tester class and put it here and here i will say the feature the feature i have to use is this one hooks demo dot feature so this goes in the i can also just go here hooks demo dot feature so i can do a right click here and say properties and this will show me the path here so i can also get it from here so i can copy it from here and after resources this is what i need so this is my location of the feature file that is src test resources hooks demo folder and hooks demo dot feature file which is this one here and then for the glue code or the step definition what i'm going to do is instead of using this folder so you can see step definitions this is the package which is being used instead of using this i'm going to create a new package the reason i'm doing this is because if i use this package i already have step definitions which will match the steps of this scenario that is given user is on login page and all these steps because we have already created these in the earlier sessions so that will be a duplicate so what i will do is i will create a new package and i will say this is steps for hooks and again this will help in documentation as well and this is what i will give here the step definition or the glucose could come should come from steps for hooks and i can save everything and now rest of the things are okay and i can now run this i will do a right click run as junit test and if i check the console now you will see that it is saying that the steps are not yet implemented and i can implement the missing steps and it is also giving me the code to implement and this is this was the reason i did not create the step definition earlier so that i could directly copy it from here so here i will now go to my steps for hooks package do a right click new and create a class and i will say hooks steps or hooks demo steps and say finish and then within the class i'm just going to copy and paste these steps that i copied from the console i will also correct the formatting i can select everything using ctrl a and then i will press ctrl i on the keyboard and now if you see here it is giving me all these errors because i have not yet imported all the classes that have these annotations at given at when everything so i will hover over this and i will import it from let me show you i will hover over this given and import it from io.cucumber.java.en similarly for when as well i will import from here and then i will also import this then from cucumber libraries and this can be and as i have given in my feature file so i will also import this from cucumber so you can see all these imports are here now so i don't have any errors now in this class i will also delete all these extra statements so i will delete all this what i am doing is i am clicking on the line here on the statement and pressing ctrl d on my keyboard so if you press control plus d this will delete the entire line if you are on mac you can press command d so click on the line and press ctrl d or command d it will delete everything so now i have my step definition file ready now if i run my test runner again it will pass but nothing will happen because i do not have any code added here so if i now again go to my test turner do a right click run as jgnet test and you will see everything shows pass one scenario and four steps and everything passed but as of now i have not added any data or any code inside my steps here so now i can add some code but what we need to show here is or what we need to do here is we have to do a demo of the hooks so here let us say before i log into the before i go to the login page or before i invoke the application or go to the login page i need to have the browser up and running so i have to do the setup of the browser so let me create a function here and let me expand this and show you i will create a function public void browser setup you can name it anything and here i will say suppose i have to start a chrome browser i will say system dot set property so i will use this and i will say webdriver dot chrome driver and here i have to give the location of my chromedriver.exe so here i will go and check where i have placed my chrome driver so i have my chrome driver exe here i will do a right click and go to properties and this is the location so i can copy it from here because it is the same project folder so i just need a relative path i can copy from here from src and i can give the location here so i have set the property webdriver.chrome.driver and i have provided the location of chromedriver.exe now i will say i need to also use the webdriver and that i can declare at the top as class variable i will say webdriver driver equals null and i will also have to import it from selenium and all these libraries we have already added in the earlier sessions and the reason i'm declaring this as a class variable so that now i can use this driver in every functions and i will not have to declare it again so i can use it directly here i will say driver equals to chrome chrome driver or i should say new chrome driver so with this i have set up the driver i can also use some other setup i can say here driver dot i can say driver dot manage dot timeout dot we can say here page load timeout and i will say here let us say 20 seconds and here i have to use the time unit so this again is a library you can see it comes from java i have imported this here or it has directly inputted it here and then i will say time unit is seconds so i have done the setup for page load timeout i can similarly say driver dot manage dot timeouts dot implicit wait time and again i will say here implicit wait time is 10 seconds so i have to say time unit dot seconds here and then if i want i can also say driver dot manage dot window dot maximize if i want to maximize the window before starting the test so you can see i have created this function which is doing all the setup of the browser and starting the browser opening the browser before we can actually do our login steps or we can go to the login page so this is kind of a hook or a setup and to make this function as a hook or to run it before every scenario i have to do the annotation or add the annotation at before so i will say add before and you will get a error here so i will have to hover over this error and it will give me an option to get this before or import before from io.cucumber.java so let me use this and now you will see it has imported this and it has imported this here you can see this one so this is what we need to get it from and now this function has become a hook and this will get executed before every scenario in this feature and let me also put a statement here i will say syso control space bar to auto complete the print statement and i will say i am inside browser setup and similarly i can create a function called teardown you can name it anything and here i will close the browser so i will say driver dot close and driver dot i will quit the browser and to run it after every scenario i will use the annotation at after and i will have to import it from io.cucumber.java so you will see again here it has imported all these before and after annotations from here and now we have these two functions which is which work as hooks for us so this will execute before every scenario and this will execute after every scenario in the feature file so now i will save everything and here we have done the setup and teardown methods and we have also marked these methods with the annotation we can also use before steps and after steps but before that let me show you this i will run this and check so i will go to the test runner do a right click and run as junit test and you will see it says i am inside before and there is some error let me check the error okay there is an issue with the chrome driver so it says the version of chrome driver only supports version 81 and for this let me check i will go to some earlier step definition just to ensure that i am using everything correctly all right so i think this is fine i just need to change the chrome driver version so if i go to my chrome on my system and go to the settings or go to help and go to about so the version i have is 84 so i should be getting the chrome driver accordingly or the same version so if i say chrome driver download and go to the chrome driver download website and this is for version 84 i will get this one for windows and it is here so i will unzip it or extract it here i'll just replace it and it is here now i have to put this inside my project folder here and it is good to go physically to the folder and then do the copy and pasting sometimes it creates some issue if you just copy and paste within eclipse so i will go to this folder first and go to drivers and let me just rename this one i will say this is chromedriver1.exe and then let me go and copy the new chrome driver exe from downloads so i'll copy it from here and go to this folder paste it and now if i refresh on my eclipse i will refresh the drivers folder and you can see it has come here so i will try again i will go to my tester now do a right click run as junit test and let us see so again there is a failure if i go to the console and check so it says a feature not found all right i think uh this was a different tester now let me go back yeah this is the one this is the one i'm using i will do a right click run as junit test and yes it is opening the chrome browser and then maximizing it and then closing it so if i go to the console and see so you can see it says i'm inside inside browser setup this is our before hook and then it is setting up the chrome browser opening the chrome browser and then it is running all the steps of the scenario and as of now we do not have any code so it is just running this these are empty steps and then it is also running the after hook and let me also put some print statement in the after hook so that it shows on the console i will say i am inside tear down and now if i run the test runner again i will say run as junit test so it opens the chrome browser maximizes and closes and if i see the console you can see it says i'm inside browser setup and then after the scenario it runs i'm inside teardown so this is running fine now let's just say if we have multiple scenarios let me say we have one more scenario like this i will just copy and paste and then right click and say pretty format so we have two scenarios and if i run the test runner again i will say run as junit test so you will see the hooks get executed before and after each scenario so this is the first one and now it is running the second scenario and it is opening and closing the chrome browser so that means the hooks are getting executed before and after every scenario so if you see here it is saying i am inside browser setup then running the scenario and then the after hook i'm inside tier down then before the start of the second scenario again it is running the before hook and then it is running the after hook so this is how you can use hooks before and after every scenario by using at before and at after annotation and then if you have to run something before and after every step you can again create a function and then annotate it with before steps and after steps so if i create another function here let me just create another function i will say public static void before steps you can name it anything and i will just print out i'm inside before steps and i will annotate it with at before and if i control spacebar if i press ctrl spacebar you can see all the suggestions now make sure that you may find add before from some other libraries as well like junit but we have to get it from the cucumber library this is the cucumber library at before and then similarly i will create a function public static or i can just say public void and i will say after steps and then i will say print the statement i'm inside after steps and then i will do the annotation i will add the annotation at after steps uh sorry this should be four steps that was my mistake it should be before step annotation and here it should be after step annotation so if i press ctrl spacebar it is autocompleting and you will also see it has also added these import statements so now if i run my scenario again if i run the tester now i will do a right click and run the test runner and you will see now in the console so it is running first scenario and now it is running the second scenario and if i see the console you will find here that the before and after functions are executed before and after every scenario but thus before steps and after step functions are executed before and after every step of the scenario and this happens for both the scenarios so this is how we can use the annotations before after before steps after steps we have already created the tester now and we have also executed and checked our execution so this is how we can use now we can also use conditional hooks and here before i show you conditional hooks one very important thing that you can use is we can actually use multiple before and after functions or multiple hooks and we can also do the ordering of the hooks for example if i create one more function i will say public void and i will say this is set up to and i will just print out i am inside setup 2 and i will say backslash n this is for next line and here i will annotate it with at before now here we have two functions which have annotations at before now generally it will get executed in the sequence of alphabetic order of the names but if i want to set the order i can say here order equals 1 and here i can say order equals to or i can also say 0 so that this gets executed before this function and if i save and run the tester now i'll say run as jnet test and let us see the output so this is running the first scenario and the second scenario and if i go to the console now you will see here it is first running i am inside setup two that is the before function that has a lower sequence of order and then it is running the i am inside browser setup the same you can use with after annotation but then it will work in a reverse sequence so for example if you are using order with after let us say i say here this is my after function i will use one more function here i will say public void tier down to and here i will say i am inside tier down 2 and here i will say at after order equals 0 or order equals 2 and here i will say order equals 1. now in this case when doing the teardown this will get executed earlier i'm inside here down to and then i'm inside tier down let us check this i will run the test runner again and this is scenario two and if i check the console you can see it is first running i'm inside tier down two and then i'm inside tier down so this is how it works and you can use any number here it works in the sequence of numbering so for example if i say here minus 1 order equals minus 1 even this is fine this will work and this will get executed as per the sequence as we have seen so you can use negative numbers or any number it will work as per the sequence now let us go to conditional hooks and conditional hooks means we can use tags with hooks so hooks can be conditionally selected for execution based on the tags of the scenario and if you want to run a particular hook only for certain scenario you can associate before or after hook with tag expression and this is how you can use tags with all these annotated functions so let me show you an example if i go to my feature file i will tag the scenario i will tag this first scenario i will say at smoke and i am only tagging scenario first scenario with add smoke tag now if i go to my steps i can say here to run this i just want to run this for the add smoke tag scenario so i will say here at smoke and now if i run this if i run the test runner let us see the output so it is running the first scenario and it is open the browser and the second one is failed and this that is because it did not open the browser or setup the browser for the second scenario as it was not annotated with add smoke and we have said that this setup or browser setup should only run for the at smoke annotated scenario and if i see the console you can see here the browser setup runs only for the first scenario and it does not run for the second scenario and if you want you can also use this for at before at before steps at after or at after steps as well so it will work for everything you can also use it along with orders so if i say order here you will have to use it with value so you will have to say value equals at smoke and order equals 1 so this will work for this will work with order as well so you can use conditional hooks or you can use hooks with tags and it will work for all the hooks all these annotated functions and if you have to use tags along with order this is the syntax how you can use it and you can use all the combinations of tags as we have learnt in the earlier session so if you want to use any conditions or you want to use any multiple tags you can use that all this we have learnt in the earlier sessions so now if we look at some useful tips so ordering of the hooks we have already seen that we can order the hooks we can use this order and give the number so that we can assign an order of execution and then whatever happens in the hooks is invisible to the people who read the features so if you see here in the feature we have nowhere mentioned that we are doing the setup of the browser before the scenario it is nowhere mentioned that we are closing and quitting the browser after the scenario and the same will be shown in the results it will not show the hooks it will only show that all these steps that you have executed in the feature or the scenario and therefore we say that whatever happens in the host is invisible to the people who read the features and therefore only use hooks for low level logic such as starting browser deleting our data from database starting or stopping a database and if you need anything that should be visible to the people who are reading the features or who need to know what is exactly happening before or after a scenario we can use background so it is a more explicit alternative for hooks and this is what we will learn in the next session and then the function of your hooks or the functions function name that you annotate with hooks or add before and at after annotation you can keep anything it is not mandatory to keep it like setup or teardown you can name it anything and hooks will get executed even if the test fails so even if your steps in your scenario fails hooks will always get executed and we have als already seen that we need to import these two libraries so this is for before and after hooks and then if you need before and after steps this is what we need to import so these are the these are the import statements that you should be having and make sure that it is coming from dot cucumber so these are the useful tips and i hope now you know everything about hooks and you can use hooks in cucumber i hope this was very useful if you have any questions any doubts you can write in the q a or the comment section and i will try to reply as soon as i can and i will meet you in the next session thank you for watching
Info
Channel: Automation Step by Step
Views: 20,262
Rating: undefined out of 5
Keywords: what are hooks in cucumber, how hooks work, why to use hooks in cucumber bdd, when to use hooks, how to create hooks, what are conditional hooks, what is background, what are scenario hooks, what is step hook, cucumber bdd training, cucumber bdd beginners tutorial
Id: iBum6hUgxgg
Channel Id: undefined
Length: 37min 31sec (2251 seconds)
Published: Wed Oct 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.