Selenium Cucumber Java BDD Framework 1 - Setup | Step by Step | Raghav Pal

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session I'm Raja you can find all my work on my website automation step-by-step comm in this session we are going to go step by step and we will learn how to set up our project in Eclipse so let's get started and step number one is we have to create a new maven project in Eclipse so I will go to my Eclipse and here I am on my windows and this is my Eclipse in case you are on Mac you can follow the same steps so here you can see this Eclipse is on Mac and this one is on Windows so I am going to follow this on Windows but I will tell you if there are any differences you can follow this on Windows or Mac operating system so here I have to create a new maven project that is step number one I can do a right click under the package Explorer and go to new and here I can go to others you can also go to your file and then go to new and go to others and here search for maven and you will find an option for maven project now in all the recent versions of Eclipse maven plugin is already installed and you will directly find this option just in case you are using a very old version of Eclipse then you may not find this maven and you will have to install up again separately but assuming that you are using some of the recent version I will click on maven project and say next and here I will select create simple project so that it can skip the archetype selection and say next and here I will give the group ID and artifact ID so I will say this is cucumber Java project or you can say cucumber Java selenium any name you can give here I will again say cucumber Java and I will click on finish and this will create the project now in my case I am in a working set so to create or select a working set you can go to these three dots in the package Explorer and this is optional so I will say deselect working set and you can see my project is here and now I am going to go to a working set and selector working set so I have this cucumber Java working set created you can also be just go to new and go to Java and say next and you can create a working set here now here I am going to go to this working set go to edit and then I am going to add this project key to my Java that I've just created into this working set and I will say finish and I'm going to select this working set now the reason I am doing this so that I do not see all the other projects and I can work in a focused mood so now you can see I have got this project here and here this is how the project looks like these are all the folders and the files present and here you will find a file called palm dot XML where we can add our dependencies so we have done step number one which is create a new maven project and now step number two is we will add the Mabon dependencies for cucumber java cucumber J unit J unit and selenium Java now I will add these dependencies one by one so that I can show you exactly why and where these libraries or dependencies are used in total these are the four dependencies that we will need so let me start with cucumber Java and for that you can go to Google and just search for maven central so we can add a maven dependency and here in the maven repository so this is the website here I will search for cucumber Java and now you can see we have cucumber Java from info dot cubes are you dot cucumber so the latest dependencies and libraries are present in IO dot cucumber even if I go to this cucumber from info dot cubes it will tell you that this artifact has been moved to IO dot cucumber so I will go to IO dot cucumber here I will take the library from here and here you can see all the latest versions at the time of recording this video we have this . 7.0 as the latest and stable version i will click here and now here is the maven dependency i am just going to copy this from here and go to my project and go to your palm dot xml file here and here you will find some content already here now in case you do not have the dependencies tag you can create a new dependency tag just before the project and tag so here I will say dependencies and you can see it is also showing this auto completion you can press ctrl spacebar if you do not get the auto suggestion so I have got the dependencies start tag and dependencies and tag and between these tags I will add the cucumber Java dependency which I have copied from maven central and now I will save the project and as soon as you will save the project you will see the dependencies or libraries are downloaded and you can see the progress and then here you will find a maven dependencies folder or a section here and if I now expand this you will see all the cucumber Java related dependencies and libraries and the jar files are now added and this is the advantage of having a maven project if I have to download and add all these charge files manually it will be very difficult to do everything one by one and check all the dependent jars and whenever I take this project to any new system then I will again have to do all the same process again now with maven it is very easy I can take this project anywhere I will just have to save the project or maybe build it once and it will download and setup all the jars and dependencies on its own so we have done this step we have added cucumber Java in case you want to correct the identity here you can select everything by pressing ctrl a or command a in case you are on Mac and then press ctrl I or command I in case you are on Mac and this will do the identity now I have done step number two and as of now I have only added cucumber Java that is the only library I need as of now I will tell you when to add the cucumber J unit J unit and selenium libraries so step number three is now I have to create a new folder called features under the SRC test resources folder so you can see here I have SRC test resources now here you may also have a different structure in case you have a project Explorer you can see as of now I have a package Explorer but you may also see our project Explorer and let me show you if I go here and search for project Explorer here it is and if I see this and now if I go to my cucumber Java project you can see they may be some differences although they are not much but these are two different views I am on the package Explorer and here I have SRC test resources folder now here I am going to create a new folder under this location SRC test resources you can also create this folder under the project location or the project folder as well but just to keep in sync everything I will create under the SRC test associate I will do a right-click go to new and select folder from here and here I have got a window to create a new folder and I will name this as features and say finish so you can see I have got a features folder created here so we have done step number three step number four is under the features folder we can create a new feature file login dot feature or you can name it anything so let us do that under the features folder I will do a right-click and new and I will go to file so here let me show you again right-click new file and I will name this login dot feature so this is going to be a feature file so I will say finish and you can see this is created now you can also see here I am getting a message that there is a better editor support for dot feature files from the marketplace and I can get the support or the plugin for that and if I click on OK it will take me to the Eclipse marketplace and it will show me all the possible plugins which can be used to work with feature files so here we have cucumber Eclipse plug-in and the natural plugin now just in case you do not get the message and you are not directly taken here you can always go to you can go to help help menu and you will find Eclipse marketplace here and same thing on Mac you can go to help and you will find Eclipse marketplace here so here I will just search for cucumber and you can see I am again getting those two plugins I am going to install the cucumber Eclipse plugin I will click on install and it will do the installation now this plugin will help us to work with feature files and also run our feature files with cucumber and here I will accept the license agreement you can read and accept the license agreement click on finish and you can see the progress here and you can see here is the progress bar and this is installing the software and after this we will restart eclipse so let this installation get completed and you can see and here I will just say install you can check the details if you get that and now here installation is complete and I will restart eclipse now so eclipse is restarted I will go to the workbench here and here is my project and now I will again go back to my features folder and now you can see we are getting a icon here for cucumber and this is coming from the cucumber plug-in that we just installed now I will go inside the feature file and from now onwards if you create any new feature file you will also get some sample text here so let me show you I will again go under features folder do a right-click go to new and file and I will say demo 1 dot feature you can give any file name with dot which are extension and I will say finish and here you can see in this file I have got all these examples how to create a scenario and steps so this will be useful after you add the plug-in whenever you create a new feature file you will get this examples so let me go back to my login dot feature file and we have done step number four now step number five we have already done that is we have installed the cucumber plugin from Eclipse marketplace and now step number six is we have to create the content inside the feature file so here I will start with feature and a colon and here I can provide the details also if you see it is not color coded as you see here you will see here now we have all this color coded so let me do one thing I am going to delete the old file which is login dot feature I will delete this and I'm just going to rename this demo 1 dot feature I will go to refactor rename and I will name this as login dot feature and now I am going to select everything and delete everything in this file and start from scratch so I will say feature Colin and you can see now it is also getting color coded and all this is coming from the cucumber plugin and let us say I want to create a feature for testing login functionality so this is let us say feature to test login functionality and now I will create a scenario and I will say scenario and a Colin and I will give a scenario name or feature can have multiple scenarios so I will say check login is successful with valid credentials and now I have to create the steps using the Gherkin language so for that we have the keywords given when and then so let us say I will I have to do a login scenario so I will say given user is on login page when user enters username and password and click on login button then user is navigated to the home page so this is a very simple scenario for a login functionality and now I can also I will save this first and I can do a right click and now you will find all these options pretty format find step definition recalculate steps so I will say pretty format and you can see it will correct the formatting here so we have added our content now these are all the things you can do in a feature file we have added a feature we have added scenario we have added steps we can also add a scenario outline now what is this sometimes you want to parameterize some data for example let me show you I will create a scenario and I will say let me just copy this scenario again I will copy this and paste this and let us say I have to parameterize the credentials username and password and for that I will provide this inside conical brackets like this and to get the data or values for this username and password I will say examples and colon and in the next line I will put a pipe symbol and say username these are the headers and password and then from the next line onwards I can add values for example user 1 and then I will say pass 1 then I can add more values you - pass - and I will say and I will do a right-click and say pretty format now you can see we are getting an error and that is because if you are using examples and and and if you are doing parameterization like this we have to put this as scenario outline so now this is a scenario outline and now I will save and do a right-click and say pretty format and now you will see all this formatting is done here so now this scenario will run two times with these two sets of values so whenever you want to do parameterization and want to run with multiple set of values you can create a scenario outline and create examples with data and then we can also do tagging for example I can create a tag here we can do tagging force feature for example I can say this is smoke scenario and we can also do tagging for scenarios I can say this is smoke test and so on now I will show you later where exactly we can put this tags and we can run only the selected features or scenarios this is just for example this is how you can do tagging in a feature file and then if you want to add comments you can also add comments using the hash table so you can say hash and you can put some comments for example I want to say who is the author and let us say date description etc so we can also do commenting like this now for now I am just going to delete the snare to outline for this particular example I will keep it very simple and also I am going to delete the tags and I will save this so we have done step number 6 and then step number 7 is let us try to run the feature file so I am on the feature file I will do a right-click and I will say run as and here you can see I am getting an option for cucumber and this option we are getting from the cucumber Eclipse plugin so I will click here and let us see the output so you can see I am getting so there may be some errors do not worry about that I will show you later so here this is successful so here you can see the output and Here I am getting undefined scenarios this is showing me the count of scenarios and the count of steps and here it is saying the scenario and steps are undefined and then we are getting a message here you can implement missing steps with the snippet below and it is showing us the snippet so this is actually the step definition or the glue code so you can see here we are getting the same statements user enters username and password when user clicks on login button user is navigated to the home page we are not getting given let me just check again so let me try this now I have written the given statement again and I will save and run this again run as cucumber feature and now if I go to the console you can see I am getting this so this is the step definition or the glue code and basically this means the statements that we have written here all these statements there has to be a back end code for these statements so the system or the processor will know that when I come here what is the exact code I have to execute and this is what we call as step definition or glue code so that is the next step we have to add the step definition or glue code under SRC test package under SRC test Java package now again you can add it anywhere it is your wish but to keep it consistent and with the proper guidelines we will create under SRC test Java package so we have this SRC test Java package here I can do a right click new and first I will create a new folder so I can put all the step definitions inside this folder and I will name this as step to finish you can say steps or step definitions whatever you like finish and inside the step definition now I'm going to create a class so I will do a right-click new class and I will say this is login steps you can name it anything and I will say finish so this is the class and I can increase the font using ctrl + on my keyboard or control - to decrease the font I hope this is visible and now I am just going to copy this and I think from here that I have got in the console and I will go back to my class and inside these two curly brackets of the class I am going to paste this so I have added everything here and here I have to make one correction this should be end and let me check from the feature file and yes this is end so here we have created the step definitions now you can also create this manually it is not necessary that you have to take it from the console so it is basically functions with some annotations let me show you from scratch for example I can create a simple function I will say public void verify login page you can give any name and brackets and a curly bracket start and a curly bracket stop so this is a function and we can annotate it with the cucumber keywords or annotations for example if I want that whenever the control comes here given user is on login page then this should get executed this function should get executed for that I will say head given and in brackets I'm going to use the exact same statement with that is user is on login page I will copy this and paste it here now whatever I write here let us say as of now I am just writing a print statement I am saying sys o and then ctrl spacebar on the keyboard to autocomplete the print statement that is system dot out dot println and I will just say hello world so whenever the control comes here on this statement it will go to its back-end step definition which is this and then just print out hello world on the console in real world people have the code the selenium code to navigate to the login page and similarly you can create functions for all these statements which we have already seen here so let me just delete this for now and here we have caught our functions and we can also correct the formatting I will press ctrl a to select everything and control I to correct the formatting and now if you hover over all these annotations like given you can see I am getting this option if I hover over given here I am getting this option import given from i/o dot cucumber dot Java dot en so we have to get this from this package so I will click here I will click here and you will see the import statements coming here and now the error is gone similarly I will do for when I will import from i/o dot cucumber and for and and for then so all these imports are done and we don't have any errors now also let me add some dummy statements inside these functions so when it comes here I will say print inside step user is on login page and similarly for this I will say inside step user enters username and password then here I will say inside step clicks on login button and here I will say inside step user is navigated to the homepage now in some documentation you will also find that there are some regular expressions used something like there will be a cap symbol here like this and they will be a dollar symbol here so basically we can use regular expressions in these statements so that we can match in whatever way we want and just in case you are new to regular expressions you can go to reg x1 comm here is the link the website reg x1 and you will find all the details and how to learn regular expression as well and that a gluten expression I was talking about was this starts and ends match so this is the cap and dollar symbol if I go here you can see we have a way to tighten our patterns to define the pattern that describe both the start and end of the line using the head and the dollar sign so this is how you can use it and just in case you are completely new to regular expressions you can go on my website or automation step-by-step comm and here or you will find a link for stories here go to stories and here you will find Mickey and Minnie stories and here you will see there is a story on reg X this will help you to understand drag x from scratch you can read this and now coming back so let me just keep this very simple for this session now we have added the step definition or the glue code or the backend code for our login dot feature file and we have also added the statements there so now let me try to run the feature file again I will say run as cucumber feature and let us see the control output and you can see now we are getting the results all that we have written inside step user is on login pH inside step user enters username and password all this is now getting executed so that means it is able to read our step definition or the glue code so we have done step number eight now step number nine is we can runner class and runner class is from where we can actually run our feature files with some more options so until now we are running a single feature so we can go here and do a right-click and say run s cucumber feature but then then we have multiple scenarios and feature files and we want to also create reports and other filters we want to do we have to create a runner class so the runner class will look something like this let me show you with an example I will go to my step definitions folder itself I can create another class I will do a right click new and class you can name it anything runner or I will say a test runner and say finish now here I do not have to add anything inside that class or inside these two curly brackets of the class but I have to add some cucumber annotations and options so first thing I will say is add run with and I will say cucumber dot class so I will say cucumber dot class and the second thing I will say here is add cucumber options and I can provide the location of feature files and the step definitions so I will say add cucumber options and then I will say features and glue code where I can provide the location of features and step definition I will say features equals two and here I have my features and I can do a right click on the features folder properties so I can get the relative location from the project folder which is this and I will provide this here and then I will say glue equals to and inside curly brackets and course I will give this step definitions now because this is on the same location I can directly say step there field missions which is the name of the folder now here I can also provide multiple feature files and multiple step definition locations as well and it is better to keep a single forward slash here if you are on Mac you will already get a single forward slash but if you are on Windows as well it is better to keep it like this because this will work on Mac Linux and windows everywhere if you keep the double backslash it will only work on Windows now you can see we are getting this errors and here to have these options we need j-unit to have the run with cucumber class and then for cucumber options we need cucumber j-unit maven dependency so now it's the time when we can add those two dependencies to our pom dot XML file until now we have only added cucumber Java and now I will add J unit so I will again go to maven repository and search for J unit and here we have J unit dependency and I will take the latest one at the time of recording this video which is 4.13 copy from here and paste it here so J unit is added now I will search for cucumber J unit and again we have this cucumber JVM J unit from info dot pukes which is a older one and then this is the latest one which is cucumber which is from IO dot cucumber so I will go here and get the latest stable version copy this and put this in my pom dot XML and as soon as I will save you will see this will get all the dependencies will get downloaded and added to the project so this is done you can also verify if I go to maven dependencies you will see J unit and cucumber J unit are available here and I can also correct the formatting control a and control I and let us go back to test on a dot Java and now here if I hover over this I will have an option to import from J unit dot Runner so I will do that and when I hover over cucumber options I have this option here now it says import cucumber options from IO dot combo J unit so I will have this imported and you can see now we have called this and now we have cucumber glass and for this I can import from IO dot cucumber dot J unit and cucumber and that is fine so now we have got our test in a class and I can directly run our features from here so I have already given the location of feature folder now because I have given the location of feature folder if I have multiple feature files inside this folder all will get executed as of now I have a single feature file so let me try this I will do a right-click and say run s and I'm getting an option J unit test I will click here and let us see the output so I'm getting this error initialization error let me check so it says illegal argument okay so there was a mistake here it is features so this is now right let me try again I will save this do a right click run s J unit test and you can see we are getting this J unit results which is all in green that is everything is passed and you can see this is the how J's net results looks like and here you can see on the console this is how we see the results now as I said we can have a lot of options with the cucumber runner or the test owner so let me show you here you can see monochrome true and this can be done so that you get a formatted information on the console so you can see this is how I am getting it as of now if I say here I will give a comma and I can also do this in a next line so that it is easy to read I will say monochrome and I press ctrl spacebar to auto complete and I will say true by default it is false and I will save and I will run this again and let us see the console so here you will see as of now you will not see a lot of difference but when you have some really good amount of steps which takes some time you will see the details here you will see some more details and more formatted view here so you can try that out so this is how you can use this option and then we can also have some options for reporting so we can create HTML JSON or j-unit XML reports let me show you to create reports I will say here I will say pretty and they should come under curly brackets and then a comma and then I will say HTML under curly brackets and colon and then I can provide the location where I want to store the report so let's say I want to store in the target folder here and I want to create a new folder called reports or HTML reports so I will say here target and I will say HTML reports and then curly bracket close and this seems to some error this should be plug-in I have to say plug in equals to and now this is fine so here I am creating HTML report in this folder so let me try to run this again I will say run as JJ test and this is done I will go back to my package Explorer and now you can see the target folder is here but it is still empty so for that I will refresh I will go to the folder and say refresh and now I have this target folder and here now I also have HTML reports folder and inside this I have the HTML report which is index dot HTML and you can see this is here I can also open it with the system browser or the system editor which will be the default browser on your system and here you can see everything is in green that is it is successful and pass now I can also create a JSON file or JSON report by saying JSON I will just provide the location of the file I will say target and I will say JSON reports and save and run I right click run as a unit test and so here is some error I should say this is pretty and here I will say Jason : and now I will save this right click run as a unit test and this is done if I go back and refresh here I have caught JSON reports and here if you see it has directly created the file here which was my mistake I should have given this name like report dot JSON so that it creates a folder JSON reports and then create the JSON file so I'll just try this one more time and I believe in case of JSON you will have to create the folder where you want to put the reports so let me go back and first under target I will create a new folder called JSON reports and I will have to get rid of this file first and I will create the new folder and now I will run this again and this is done if I now refresh you can see inside JSON reports we have got a JSON report and if I click this this is how it looks like so we have got a JSON here and again it is showing me some plugins which I can use to you Jason I will cancel for now and now I can also create the j-unit XML reports again I will create a new folder under target I will say this is j-unit reports and then here I will say intestinal I will say this is j-unit and I will say it should get stored in this j-unit reports folder and with the name report dot xml and save and run and if i go and refresh the folder you can see the J unit report is here and if I click here it is taking me to this J unit however I can do a right click here and say open with and I can open with o text editor as well or an XML editor so if I say XML editor you can see this is how the J unit reports looks like now here you may have a question that HTML report is fine where we can see the visual data and reports what's the use of JSON and these staging at XML reports so there may be some other tools or processes where if you may want to push your reports so that they can pass the report and maybe show some more outcome or maybe do some action based on the reports there we may need the J unit and JSON reports for example if we integrate this with continuous integration system like Jenkins there we have an option to get a J unit XML report and then Jenkins can show the report on its dashboard from the J unit report so we will see that later this is why we need different formats and we have seen this now you can also create tags here so we have seen tags earlier and that is what we can use here so here I can say in the test runner I can just say here I'll give a comma and say tags equal and I can give some tag like smoke test now whatever scenario or feature matches this tag will get executed when I run this Runner for example if I run this now I will say right click run as J unit test and if I go to the console you can see 0 scenarios and 0 steps got executed because there is no scenario or no feature with this smoke test tag so I can go to my login dot feature and I can provide this tag here to my scenario I will say smoke test or I can also provide it at a feature I can say and smoke regression smoke feature or any tag I want and now if I go and run the test runner again let me save the feature file and this I will say run s JJ test and now if I go to the console you can see this scenario got executed so this is how we can also run by filtering with the use of tags and one thing I forgot to tell you that for reporting you can actually use all the formats as well for example I can say here J unit and I can also say JSON and I can say HTML and for JSON I will provide the location of this folder like we have done earlier I will copy this and say JSON should get JSON report should get saved here and for HTML I will again give the location that is target and HTML reports so now I will try to run this again and we will have all the reports and everything is fine and if you refresh this folder you will see all the reports getting generated here so you can now understand the use of tester now we can run based on our filters whatever features we want whatever step definitions along with reporting and features like monochrome true and using tags and a lot of other things so we have done this step that is created under class and we have executed as well we have also already seen how we can create different reports HTML JSON j-unit XML and then we have also executed and verified our results so I hope all this was very useful I will suggest that you do some more you take some more examples and do some more practice if you face any issues let me know in the comment or Q&A section and I will try to reply to you as soon as I can I hope all this was very useful thank you for watching
Info
Channel: Automation Step by Step
Views: 148,295
Rating: 4.9360437 out of 5
Keywords: automation testing beginner tutorials, test automation jobs, selenium java cucumber bdd tutorials, cucumber java selenium tutorials, cucumber bdd selenium with java, cucumber bdd beginner tutorials, how to setup selenium cucumber java project, interview questions cucumber bdd
Id: 4e9vhX7ZuCw
Channel Id: undefined
Length: 43min 24sec (2604 seconds)
Published: Thu May 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.