#7 - What is #Background keyword in #Cucumber (Latest 2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is naveen here welcome back to naveen automation labs in today's cooking video we are going to talk about what is background in feature file a very important concept to define some preconditions for all the scenarios so let's talk about it so what i'm saying is let's see we have one scenario and let's see on amazon page this is amazon.n or dot com whatever you want to open and what i want the scenario is that you have to log in first so i'll go to sign in and then i'll enter my email id continue and let's see i'm entering my password over here and then click on login and when i click on login what i want i want to see my previous order details over there so i'll go to this particular section and i'll go to my your orders and here you can see that okay these are my previous orders are available over here do multiple things that i can go to buy again i can go to open the orders i can go to the cancelled orders i can go to the once again multiple orders i can check it it means my previous orders i can check past three months order i can check plus and previous history also i can check open orders and cancel order details also i can check that so these are the let's see i'm writing three scenarios over here that when user login and go to order details page i want to see my previous order i want to see my open orders and i want to see my cancelled order these are the three scenarios are available so what i'm going to do that i'm going to create a feature file over here and let's write this particular feature file i simply say that this is my order dot let's see i'm writing feature over here okay and then i'm going to write some one feature that is my feature i'm going to give this is my amazon order page and i simply write some description about you can write some description also some documentation also because let's see other stakeholders they are going to check this particular feature file and your client is also going to check this particular feature file it will be very much readable format what kind of feature that you're going to check so i'm going to see that okay in order to check my order details as a registered user i want specify the features of details page right so this is about the motivation for this particular feature and then i'm going to write multiple scenarios so remember one thing for all the scenarios let's say i'm writing three scenarios the precondition is that user should be on login i mean users should be on this order details page and user has to log in and has to navigate to this particular order page over here all right so on the basis of that i have already written three scenarios i'm just going to copy paste and i'm going to show you that how exactly i have written now see these are the three scenarios let's see i have written all right and the first scenario something like this that check previous order details register user exist and then a user is on amazon login page user enters username then enters password and then clicking on the login button and navigates to the order page and then checks the previous order a details over here and my scenario is check previous order details check open order details and the check canceled order or details over here right and if you notice one thing almost these steps are exactly same that user navigates to order page same step i have written over here in the same steps i have written over here if you see this particular a feature file i have three scenarios likewise let's see tomorrow i have 100 scenarios or 10 scenarios i'm gonna write in this particular feature file it is not that readable because repetitive steps you have written and it's not systematically written over here where we can avoid number of lines unnecessary lines to ride again and again for each and every scenario that's why guys we have to introduce one keyword that is called background keyword so let's see how exactly background keyword will work now what i'm going to do that if you see this particular feature file and then i'm going to write all the common steps in the background keyword so what you see that guys all these feature steps are flagged now you can see all these steps are flagged why sometimes what happens that okay this plugin doesn't work properly so you just close it and open it again or what you do right click on your project and go to maven and simple update the project when you update the project okay see let's you can see that yeah the moment you update the project all the feature file steps are flagged it means there is no return step definition against these feature files now what i'm going to do that i have to write first of all improve this particular a feature file with the background keyword so i'm going to write one background keyword over here so i simply write background and what kind of background that you want to write so i'll just copy all these guys from here to here cut it from here all these steps over here and i simply say then user navigates to the order page and then my first scenario is check previous order details so i have to click on this particular orders a tab over here right so i simple write one line over here that then user i would say when user clicks on user clicks on orders a link and then check the previous order details same thing i'm gonna delete this and same thing i'm gonna delete for the third scenario as well and i simply write when i use when user clicks on the second link is that let's see open orders so i simply say that when user clicks on open orders link user checks the open order a details over here and the final step i'm going to write it like this okay make sure proper alignment and the indentation is very important user clicks on cancelled order see this cancel order so i simply click on cancelled order links then user checks the canceled order details over here now you see this particular feature file this is more elegant more readable and then each and every scenario is just focusing on what exactly you want to test over here and unnecessary common steps i mean all the common steps we don't need to write unnecessary again and again for each and every scenario so the property of the background is when you run it it will be executed before each and every scenario so background will be executed if you have 10 scenarios it will be executed 10 times before each and every scenario right now in order to define the step definition i'm going to run this particular order dot feature file and then so that i can get some a definition over here so simple run configuration and then select this particular audio dot feature from this particular project and click on run when you run it you go to console and here all these step definitions you will be getting copy these all these definition and then simple create one definition file over here in your step definition package and i simply say that okay these are my orders okay steps i'm going to write and then copy paste all these methods import all these annotations and i'm going to remove all these throw exception like that okay later on when you write selenium code or any test automation code that you want to write you can write it remember guys here it's up to you what kind of tool you want to use it's not compulsory that you have to use selenium selenium is just a library to automate the browser and then here you can use any code so that's why we say that cucumber is nothing related to a selenium over here you can write your feature file and then it's up to you which tool you are going to automate okay you are going to use to automate now let's see i'll simply return all these uh steps over here in the form of java methods and then i'm going to call this audit.feature run this particular auto dot feature once again run configuration and then run it again and now you see the output the output is exactly same that we were expecting you can see that each and every scenario automatically these four five steps which were written in the background section automatically got attached over here you can see that like this same thing for second scenario and the third scenario for cancelled order automatically got attached saying three scenarios 24 steps got passed right guys so that's why easily it's readable your client and your stakeholders and your manual testers also or the po also if someone is referring this particular documentation in the form of feature file they can easily see that okay how exactly the business logic defined for that particular these particular scenarios this is the background for the common conditions remember one thing guys background should be written in a very you should not write very lengthy background couple of three four five lines you can write and then make it vivid and short and lightweight according to the cucumber documentation and according to the best practices for the feature files background should be very short and limited do not write very complex steps over here in lengthy steps in the background perfect likewise you can design so many use cases are there let's see for every home page let's see i'll go to the home page over here on amazon i want to check multiple things over here to check all these let's see home based details i have to log in so log in will be the background step for me and then i can write fresh is visible or not or something like this it's visible or not i can do that right now let's do one thing let's create one a test runner against it and then let's run it from the runner also so i'm going to create a class that's that's fine i'm going to create order uh test.java and i'm going to write some definitions cucumber options over here let's say i'll pick it from my previous class copy this and simple paste it over here and i'm simply saying that okay instead of uber dot feature i simply write order dot feature i don't want to use any tags so let it be like this and then let's run your order test.java and when you run from autotest.java also you can see that absolutely working fine these three scenarios your background steps automatically got linked over here and then three scenarios are absolutely working fine later on you can write the logic in your step definition whenever you want to use selenium or something like this you can write your driver.find element and different other utilities methods you can call it over here directly so i'm not covering selenium as of now guys this is just to show you what exactly the meaning of background keyword and if you open this junit runner report also here you can see check previous order details open order details and canceled order details you can write the same thing if you really want to run it from the maven so i'll go to maven go to terminal we are already in this particular package gimbal practices and i simply write maven test and let's see so with maven test also you see that it's absolutely working fine check previous order details like that open order cancel order and you can see the report over here like that okay so this is about the background keyword guys we will see one more use case of the background keyword in hooks that i'll tell you later so thank you so much guys thanks for watching this particular video i'll upload this particular video i mean upload this particular entire documentation along with the feature file along with the code and everything in my git repository and i'll share the link in the description of this video please go and check it over there and practice accordingly and if you have any interesting scenario that you have written you can provide your git repository link in the comment section thank you so much guys thanks for watching naveen automation labs please subscribe to the channel if you are liking these cucumber videos and other videos on my channel i'll be coming with a new video in the next session that will be more interesting things about cucumber till then take care and keep watching naveen automation apps
Info
Channel: Naveen AutomationLabs
Views: 18,949
Rating: undefined out of 5
Keywords: Background keyword in Cucumber, cucumber BDD, cucumber BDD with background keyword, cucumber feature file, cucumber with selenium, cucumber 6 latest videos, cucumber tutorials, cucumber by naveen, naveen automationlabs
Id: ZPEzIogLnrM
Channel Id: undefined
Length: 12min 49sec (769 seconds)
Published: Thu Nov 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.