PART 2 - Cucumber Java BDD Hybrid Framework - Page Object Model (POM) - Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is navini welcome back to naveen automation labs so in the previous video guys we have already seen the design of page object model that we were talking about with cucumber now let's start with this framework designing part and one by one we will pick all the different components we will design so today what exactly i'm gonna do that i'm going to so let me first of all open this particular uh design that we have created already and today we will create a couple of layers like uh we will create one login feature we will create uh onehooks.java we will create a step definition one page layer and everything so this uh one within one day we can cover everything guys so it will take four to five sessions to cover all the things different flows and everything so have some patience you just follow what exactly i'm doing it and then on top of that you can create n number of scenarios as per your requirement or for your application also you can implement that for application point of view i'll be using this application so that you will get the feel of some e-commerce use cases also so this is a shopping website where you can log in and you can search for the product and then add two card and typical shopping experience you can get it so we will try to automate this right and uh now what we have do we have to create a first maven project so let's start so let's go to the file and go to the project over here and click on the maven project click on next and click on next over here and here you have to select one filter you have to select one artifact id so let me just select it that is maven type quick start 1.4 you have to select if you have different version like 1.0 or 1.1 that doesn't matter but make sure that artifact id is this our type quick start you have to select click on next and then you have to give your group id and the artifact id so let's see i'm giving a group id as a cucumber pom i prefer this name quick um palm series and then the artifact id i'm giving something same thing over here like that or whatever the group id artifact id write some proper name over there accordingly you can write it and you simply click on finish so here you see that okay keeping my palm series got created src main java src does java we will be creating our own packages so i don't need that so simple you can delete these packages delete this dummy package that got created we don't need them and i quickly opened my pom.xml file and here we have to add a couple of things and different uh uh dependencies and plugins we have to add so let's add certain dependencies over here so first dependency first of all that i'll replace this entire project properties i'll be creating my own properties so i'll be sharing this particular entire project in my github so you can go there and check it over there and you just follow the exactly same thing that i'm doing it i've already copy paste it and first of all i'm using a java version this 1.8 junit version this cucumber version this and then compiler plugin and the surefire plugin also and then we need certain dependencies so we need a cucumber java dependency cucumber j unit selenium j unit web driver manager and we need one more adapter to execute our test cases sorry uh to produce our test results over there with the help of extend report cucumber 6 adapter also we need that so uh all those dependencies i'm going to add one by one you can find these dependency on uh okay maybe one also but these most of the dependencies you guys are already aware of it so let me remove this dependency section and i just simply go to this guy so when we were doing some earlier practice also with selenium we use selenium java 3.141 web driver manager to take care about the binaries and this is the adapter app is talking about so which is provided by this tech dot grasshopper and cucumber 6 adapter we will be using it to generate the spark report and html extend report we will see that version is this scope test over here and other than that we are using cucumber java cucumber version actually is available from you can see the dollar cucumber version so actually this property we have already defined over here 6.9.0 i'm using it and cucumber j unit we are using with respect to cucumber version that is this and cucumber j unit sorry j unit version we are using this 4.13.1 so everything is set up over here you just simple ctrl shift f for the formatting and press ctrl s it will add and build the project and all the dependencies will be added under maven dependency section over here you can see that after that what we have do we have to add some plugins so by default let's see some default plugins you are already available over here you can see that under build and this is a plugin management so let me delete and remove all these plugins from here from the build section now the build section is empty and we have to add a couple of plugins first we will add the compiler plugin then we have to add the surefire plugin and then we have to add maven failsafe plugin for the parallel execution so i'm going to add those plugins as well so those plugins are already with me actually so i don't want to waste my time i just quickly copy paste those plugins so uh you can also do the same thing don't try to type it manually guys just simple paste it over here like that okay so you can see that we have this uh compiler plugin okay compiler plugin is already written so let me remove this from here compiler plugin is to compile your code to compile your test code and the library code available in main java a short file plug-in is to execute your test cases and uh then we have phase save plugin we will talk about it later how exactly we can configure parallel equal to methods thread count equal to four like that we can do that so i'll tell you about this uh failsafe plugin later so you just simple uh control shift f and then simple save it so this is a pom.xml file this is the first thing that you have to do that and you simple close it when you close it you see that okay java 1.7 is still available with across so what you have to do right click on it and simply go to maven and update the project and you click on ok so your project will be updated with jvk 1.8 it means all the dependencies and everything and the path of your build path everything is set up now after that what you do you right click on it and don't forget to convert your project into a cucumber project so you have to go to configure and configure to convert to kikima project over here you can see that so now this is a maven cucumber java project actually so you can see that it's converting you can see the progress over here and build the project kimber palm series like that so it will take few seconds yeah it's done and now you can see that this is green icon over here i cannot zoom in but try to see that green with maven this is maven cucumber java project now with 1.8 library and other external library that we are using it over here now i'll go to first we have to create the folder structure according to the design that we have that we have picked so the feature file and step definition file it should be created under your src test java so i'll be going to my src desk java right click on it go to and create a package over here let's see uh the package name i'm going to write these are the let's see com. features package that i have created and under that we will be creating a feature file so let me create first quick file over here that is let's see we are going to implement login.feature first so login.feature that we have created and we will add the feature or scenarios in some time so let me just close it then i i'm going to create one more uh package over here for the step definitions so i simply write okay yeah this is my step definitions is the package name and under this particular package name we have to create the step definition so i'm going to create a class so let's see this is my login uh steps right and simple create this particular class steps.java will be created over here like that i'll do one thing let me rename it instead of we simply say login page dot feature and let me write l capital login page dot feature that looks more okay better name let me just select it and delete it after that what we have to do we have to go to the src main java we have to create our page layer you can see that the page layer should be created under src main java because they are nothing related to your test cases test cases should be written scenario should be written in your feature file and the test cases should be written in your step definition file so i'll do one thing i'll be writing src test java and create a separate package once again and i simply write comma sorry not in src desktop src main java and create a package com dot let's see the pages over here click on finish and whatever the page that you are going to create right click on it and create a class and i simply write let's see my login page dot java so here we will be creating the page library and page object model encapsulation locators everything we will be defining over here and one thing you remember guys i told you that all the feature file actually should be created under src test resources not even src test java it's not a good practice so better we do one thing we move our a feature file also in a separate resource folder so what you have to do right click on your project go to new create a different source source folder and write some name like this src test resources actually and you click on finish and under src test resources what you just need to do that right click on it or you just do one thing copy this particular package from here and then paste it over here and you delete your feature package from here so now our src test resources we have this package login dot feature is available over here that's fine and we have src test java with my step definition file over here looks good then i'll be creating certain other things let's see you want some utilities so i'll be creating a package over here under main java let's see com dot qa dot util package i'll be creating and let's see we will be creating some utilities like let's see i'm going to create one utility that is element util i'll be creating my own wrappers and functions over there so we will see in some okay in upcoming chapters how to create these utilities and other than that guys what we have to do we have to create some other things also so we can do one thing let's create a package over here and the package name is let's see we have to create my driver factory class from where you're actually a driver is coming so we have to create this driverfactory.java so what i'm going to do that we can create simple com.qa dot a factory class patch we can create and right click on it and create a class over here my class name it will be driver factory simple create driver factory.java over here got created under some utility we can write some constants also so i'll be creating my constants.java we will create some final static constants over there so yeah let's write constant.java also we will be doing that and then what we have to do under src test resources i have to create one config package also so let's create a package and my package name is a config under this config package i simply go to new and create a file i have to create one config.properties file so i simply write config dot properties file properties like on which browser you want to run so let's say i want to run on a chrome so i'll be writing a browser is equal to chrome over here like that fine and we will add some other properties later so let it be like this other than that what we have do guys that we have to add runner file right so i'll go to my src test java and we have to create a separate a package for that so how to do this simple right click on it and go to new and create a package you remember that we have to add this my test runner a junit class so that we can add the cucumber options and all those things from there so let's say i simply tried this is my test runner class click on finish and here we will be having certain uh okay creating a class over here and my class name i'm giving let's see my test runner i'll do one thing let's see this is my uh just change the package name press f2 and i simple say that these are my test runners okay now let's do better under this particular test runner we have this class mytestrunner.java okay and then later on we will keep adding multiple things and you know for other configuration another whenever it's required we will keep adding that but this is the basic initial structure that we have to follow now let's go to the login feature that we have created let me just control a delete that we have to create our own feature so i was doing some testing for some other application so i'll do one thing i'll just copy paste the same feature over here same scenario so that we won't waste time unnecessary write the same plain sentence once again again and again so uh what you can do is that um let's see this is my feature login page feature same thing you can do it over here that this is my login page for this application and then we have to check couple of things first of all that okay what is the title of the page so login page title user is on login page user gets the title of the page and page title should be let's see what exactly the page title the page title is login hyphen my store so what we can do is we can go to the view page source and from here we can copy this particular title and this is what i'm expecting that uh log in my store right then forgot password link i just want to verify that user is on the login page so given then forgot password link should be displayed so you see over here that we have this uh forgot your password link should be displayed over there so that's why i've written forgot i'll write it for god your password link should be displayed this is my second scenario that i have written third scenario is what login with correct credentials so i'm not writing any negative test cases right now login with simple positive test case credentials user in again on login page user enters the username so i have created a separate username for this application you can pick this particular url please try to create your own username password guys please don't use that otherwise they will block this email id so what you can do is for me i can use this particular username and selenium i think we need to check the password i have already created let me quickly cross check selenium at the rate one two three four five and let's see yeah so instead of one two three four five you have to write at the rate one two three four five then user clicks on login button user gets the title of the page and page title should be my account because after clicking on login button you can modify like this user gets the title of the home page and home page title i would write or let it be like this okay let it be like this title should be what the home page title so my account hyphen my store so i'll do one thing i'll go to the view page source once again and i simply write let's see my account and my store and i simply say that okay yeah this is the title of the next page whatever you are landing right so you can see that all these uh steps are flagged and guys you can write your in your own language so you don't need to follow the exact same format it's up to you but you must have seen my previous video that best practices to write given when then and the scenarios so please follow that uh you please try to use as much as and but then also see when and i'm using to end up uh keywords over here two steps with the end over here like that don't write when when when five times and then ten times like that it's it's not that readable and looks a little noisy so feature file got created and after that i have to create my step definition so we will run this particular feature file simple go to run as uh run configuration first of all that go to the new configuration and we have to select that project that is uh cucumber palm series and we will go to the scripture my palm series uh project over there so it will go to my workspace let's go to the workspace over here and uh workspace and cucumber palm series src then test resources uh then we have com feature login page dot feature that we have created open it apply and then simple run it so you have to give the path of the feature file and what you have to do is step definition got created over here so simple copy all these definitions which got created copy this three scenario three undefined scenario obviously we have not created any step definition now step definition class we have created this login step.java and instead of login steps let me write login page steps actually it's not for only for login it's completely page steps we have to write so let me just rename it and recompile it and simple copy paste login page steps.java and then you have to import all the annotations like that so yep this is good now what i'm going to do that under this couple of things guys we have to do that right we have login page dot feature okay then this is then actually it's showing to duplicate that is user gets the title of the page so user gets a title of the page and user get a title of the page so title of the home page let me write okay and the same we can write it over here so that it will not create any duplicate method and i simply write underscore home underscore page perfect yeah now it's looking good okay because there are actually two steps for common title of the page and here we are getting title of the home page anyways now i'm not writing any selenium code as of now because we have to start the driver and everything initialize the driver with the help of driverfactory.java so i quickly go to my driverfactory.java and here we have to write a utility or a function which will return the driver so what i'm going to do that i'm simply writing one um web driver over here and this will be a public web driver so that it can be accessed by a driver factory so let me write initially let's see with public and driver over here and what i'm going to do let me import this particular web driver from selenium import pad and i'll be using the concept of thread local because tomorrow we want to execute in the parallel mode so we have to use a concept of thread local and thread local is a java 8 feature so what i'm going to do that i create one public static thread local instance i'm going to write what type of generics i have to give a web driver over here because i want to generate webdriver with a thread local i want to initialize the web driver with the help of thread local and i simply say that okay yeah this is my reference name is equal to thread local driver is equal to new thread local you can write it and semicolon perfect after that you have to create one method so let's create a method over here which will actually will initially it is let's see void and i simply say that okay initialize underscore driver method on the basis of what so this guy says that okay you give me the browser and i will initialize the value of the web driver on the basis of this browser property and let's see so far i'm simply writing initially system dot auto printer later on we can add the loggers and i simply say that that browser value is whatever the value that you are passing we will print it on the console first and then i'm writing one if condition over here that is a browser dot equals to what later on we can maintain some constants or maybe some enum i currently right now i'm directly writing this chrome over here later on we can create one enum value or uh or enum class we can create and directly from there also we can maintain or some constant also we can maintain but let it be like this right now let's not focus on that thing let's design the skeleton and the foundation first if it is equal to chrome then i have to use my web driver manager dot chrome driver dot setup and then what we have to do thread local driver dot set and what is your driver value so i'll be using new chrome driver over here right so thread local driver actually will give you a method set method and get method so set meter means now new chrome driver the object of the chrome driver will be created and it will be set with thread local drivers let me import this chrome driver sorry okay so when you initialize your chrome driver please set it with the thread local driver so it will create a local a driver copy for the specific thread over there so whenever someone is going to set automatically chrome driver will be ascent with the thread local driver and through the local driver is actually having the instance of a web driver over here perfect okay so this is how we have to achieve the parallel execution with thread local i'll show you practically also you don't worry about that and then i'm gonna write one else if once again for the other browser let's see next browser i'm using you can take firefox and i simply write for firefox you have to use firefox driver.setup and we have to use a firefox driver this is done import and then let's see we have to use one safari driver also so i'll be using one safari if you really want to use safari i'm not using mac machine so i cannot show you on maybe internet explorer or other browser but let's say i'm maintaining only three for safari we don't have any binary so let me remove that and i simply write safari driver over here perfect okay and then if nothing is getting satisfied i simply write one else condition over here i can put a logger or maybe system.org as of now i simply say that please pass the correct browser a value like that and what if the browser value that you are passing lets you passing by mistake capital chrome or opera opera we are not handling it so it will print the message inside the else part okay and uh then what we have to do yeah what we have do guys so this is the method that we have written starting from here and ended over here and what i'm going to do that immediately after that we have to get the driver also so we have to create a method that is public static and this method will return a web driver reference and let's see the method name is a get driver method and here we will be using my thread local driver please give me the driver so as i told you thread local will have one get method and will have one set method so if someone is calling this particular gate driver method it will return what it will return the thread local instance in inside local what you have already initialized with new chrome driver or new firefox driver on the basis of a specific condition right and what kind of generics you are passing a web driver so whenever you are calling get it will return so let's see this return so instead of uh instead of avoid we have to write a web driver over here because see this get method is giving you a web driver because thread local is actually initialized with web driver as a generic over here right so what i'm going to do that i want to return that initialize method whatever the method that you have written this initialize driver should return a web driver also but before that let's say i want to do a couple of things let's see i will call this particular driver method dot now it will return driver so you can access find element close everything you can do that by simple right manage dot delete all the cookies and get driver let's see i have to write dot i want to maximize the window window dot maximize we are not going to use implicitly wait we will be writing uh explicitly weight utilities and i simply say that okay fine then you return the get driver so what will happen it will at the run time it will take this particular gate driver and whatever the current instance is going on of the web driver will be given so this is not a typical way of doing the web driver initialization like web driver driver equal to new chrome driver whenever you have to use thread local piece using it it will help you a lot to achieve the panel execution perfectly and all the reports and everything will be generated perfectly okay now if this guy is saying return get driver get driver is giving you what a web driver so obviously the return type of this method also instead of white should be web driver because we are returning get driver over here simple and i'll be doing one thing that i'll be writing some documentation over here that is um i simply say that this method so you give me the browser and then i will return and this get method also we can do one thing that multiple threads let's say you're running in parallel execution five threads are running together and they all will be calling this get driver method so they all the threads they should be okay in sync so i'll be writing a synchronized keyboard over here okay so it's a good practice that okay yeah synchronize keyword and i'll put a simple comment over here that perfect a simple method let's see we have written over here so this is a driver factory class that i have created and uh looks good okay now after that what we have to do after that we have to create see this this configuration file dot properties file that we have created we need to create one more file that is called config reader dot java so config reader.java according to the srp according to the single responsibility concept that every class should behave let's see for driver factory is responsible for the specific work only for driver i cannot write the same method over here that okay yeah read the properties from the configuration also for that i'll be creating a separate file over there or separate java class for that so i'll do one thing i'll go to my utility section and right click on it and let's create a class over here because it's kind of a utility for me so i simply write this is my config reader dot java and we will be reading with the help of properties file right so i'll be creating one public properties or let it be private so that is only specific to this particular class and import from the java.util package and i'm going to create a method a public layer over here so this is a kind of encapsulation if someone wants to get the prop you cannot access directly you have to call this method so i simply call a method over here that public uh void in nature and let's see initialize the prop initialize the properties and i'll be using the same property over here prop is equal to new properties i'm going to create the object of properties class right and i have to interact with this config.properties so we have to use file input stream plus so i'll be using file input stream class object let's see ip is equal to new file input stream and where exactly your configuration file is available my configuration file is available over here config.properties so right click on it go to properties and you just cop pick from src test over here no need to pick the complete path and you simply write it like this in double quotes the path will be in double quotes put dot and then write it like this and if let's see file is not available or something like this it will throw the exception so instead of add throws declaration please handle with the try catch block so that we can do the proper error message logging and print stack trace also we can maintain that and once this is done you just need to load the properties by with the help of properties class object prop dot load and you have to pass ip over there the file input stream class object you have to pass it over there and then again if any kind of io exception or something like this add one more catch block so add catch clause surround with try so let's see i o exception or something like this it's coming simple and once this is done then you finally return the prop and this prop is having all the properties available over there if it's returning prop then instead of void what should i write its returning prop prop is properties type so we have to write properties over here fine so this is my config reader class got created and you can simply write some basic documentation that initialize or read multiple properties you can give the path over here right now let it be like this i know this is a little hard coded value we can remove this hardwood value later on but it's not that important right now but i think this will perfectly fine now and it will return what it will return it's simple say that it returns something like this let's see i have written then what else this is configuration also done my feature is also done and then what we have to see this we have to login page title scenario is written given user is on login page but we need certain preconditions over there that who will enter the url who will launch the browser certain things that we have to do that if you really want to write a test cases or scenarios for login page right so in that case we have to create one hook file over here okay the application hooks dot java that we have to create so now let's create one application hooks dot java so i'll be doing one thing under my src test java right click on it go to new and create a package and i simply create a package this is my com.qa dot or let it be like this let's see this is my application or i would say let's see app hooks you can create a package over here app hooks and what you can do is right click on it go to new and create a class remember hooks concept before and after annotations we have to use that so i simply write this is my uh given name proper name let's say this is my application hooks java class got created and in this particular application hooks this is just like a base test class that we create in test ng create your definitions or uh sorry uh pre-annotations and post annotations you can write same thing first of all that guys try to understand that for every feature let's see i'm going to execute this particular scenario before every scenario your application hooks at the right before and after it after will be executed after every scenario and before every scenario before method will be executed that we have already seen that right this is the feature of hooks but if you notice what's the precondition for this guy user is on login page it means user is on this particular page but before that we have to launch the browser and we have to enter the url somewhere right so that's why what we have to do who will launch the browser it's hook's responsibility to launch the browser otherwise what will happen otherwise you have to write the same step again and again over here right so that's why we will move that particular code in my precondition over there so i simply say that a private see is but which class is responsible to launch the browser driver factory class having this particular method initialize the driver method so what we have to do we need this particular driver factory class object so that we can call this particular method make sense so we will create the object of this class and then call this particular method from my hooks and if you remember that that's what i told you from the hooks you have to execute you have to make the connection with your driver factory where you see this connection is available from the before perfect so i'll go to my private variable that i'm going to declare that is a driver factory instance i'm going to create let's see driver factory reference i'm going to create first and i'll be maintaining one private let's see a web driver also why private because these are the web driver factory and driver these are specific to application hooks only only for this particular class and we will create some public wrappers so import this and we will give the access via public wrappers so what i'm going to do first i'm going to see this at the rate before annotation we will be using and here i can maintain multiple annotations so first annotation is that to launch the browser also who will provide the browser this method says that okay you give me the browser and the browser is available where exactly so browser is available in your config.properties file you see so we have to read this particular property so for that i'm going to create some application hook with order with the order of zero and i'm going to create a method let's see public void get property method over here something like this let's see i have written public.guide property and this add read before we have to import from where from io.cucumber.java we have to import and what we have to do we have to create the object of config reader class so that we can call this particular method guys please don't make it static okay try to avoid the static as much as possible so here again i simply say that this is my config reader and config reader reference that i have created import this particular configurator from the package from the util package config reader is equal to new config reader i'll be writing okay and then do what then with the help of this particular config reader dot that method we will be calling that is initialize the property and this initializer property will give you what it will give you one properties class reference so i can maintain one properties class reference over here so what i'm going to do i simply return i'll be writing properties proper reference i'll be creating and the same prop will behave like a holding parameter for me holding variable for me so initialize property will give you properties and then will be given to this guy simple okay and then i'll be creating my other before annotations and i quickly write the second before annotation and let's see order of one so first this guy will be executed and then this guy will be executed this is advantage you will get you can create multiple before with the order okay in cucumber junit you will be getting it and let's see the next one is that launch browser now we have to launch the browser right so how we launch the browser so for launching the browser i need this particular prop so i'll be doing what prop dot get property method i'll be using it and what is the key the key is browser see in my config.properties whatever the key browser is there the value is chrome give it to me same browser key i'll be giving to this guy and this guy will give you what a string over here so i'll be storing in a string let's see this is my browser name is equal to this i'm storing in a particular variable over here right and then do what and then i'll be creating the object of driver factory driver factory is equal to new driver factory class object and from the driver factory that object that we have created sorry a dot which method we have to call we have to call this initialize driver and then here we need to pass the browser name over here now you got it why i have done it that way because that's why we have to create prop dot get property method so that prop we have to access in a different variable we have to get in a different variable and this class will be giving you the prop reference and the entire property will be stored over here now whatever the property that you want you simply get it from there so let's see this get property and pass the browser as a key and the same browser name we have to give this guy initialize driver method but how will you call this method you have to create the object of driver factory then only you can call this method so browser name will be given done so this method is also written so initially driver method let's see it's chrome first condition is satisfied and it will launch the browser over here with a thread local perfect okay then i'll be doing what i'll be writing my at the rate after annotation and let's see after annotation i'll be writing the order of first of all zero i think i did a slightly mistake when we were talking about at the rate after and at the red before hooks here the order is reversed when you talk about at the rate after so here the order is like that order equal to zero will be executed and order equal to 1 will be executed but in case of after if you have written order 0 and order 1 order 1 will be executed first and order 0 will be executed last so that's a small thing okay i think that was a uh you know sleep of tongue that time anyways but we will rectify here so i'm going to create one after each and every scenario what you want so let's say i want to quit the browser and how will you quit so simple writing driver dot acquit over here like that right and yeah now one thing guys this driver is this particular driver which is actually pointing to null so here you will be getting null pointer exception at line number 36 so what should i do now to fix this particular problem first let me import this at the rate after to fix this particular problem when you call initialize a driver method you have to store in this particular driver so that you will get your driver over here otherwise it will give you null because this driver is actually point taking from here and this driver is not pointing to anyone it's pointing to null so null dot quit will give you null pointer so better you have to store first and use the same driver over here right now i'll be creating one more method over here guys that is at the rate after order of one which is um if in case of any scenario which is getting failed i want to take a screenshot so how to do that very simple write public avoid and let's see this is my teardown method something like this i'll be writing and uh and here we will be using the concept of scenario object you remember scenario object we created that so scenario sc just put a condition over here that if sc dot if it is scenario is getting failed then what do you want then you have to take a screenshot so i'll be writing the logic for take screenshot over here so let me quickly write so what you can simply do that whatever the scenario let's see i'm writing this is my scenario reference name let's not write sc over here scenario dot is failed that looks better i simply write scenario dot first of all give me the name and dot i'll be writing replace all and if any space is there and you simple replace by with let's say underscore and then that will behave like a screenshot name for me let it be write it like this the screenshot a name is equal to this okay i hope you guys are aware of how to take the screenshot so we have to convert the driver into typecast into take screenshot interface right so what i'm going to do that i will be putting a bracket over here and i simply write that okay yeah take a screenshot convert your driver into this okay perfect and output type and import this from see this from selenium okay so what exactly this method will do it will create the screenshot and the screenshot will be stored under this particular source path and then what we have to do after that we have to and then we have to attach the screenshot dot attach method and what is your source paths this is my source path and what type of media type you are using it so i'll be using image with png type and what is the screenshot name so i'll be writing this is a screenshot name will be attached in the whatever the extended report or whatever you are using it okay so simple three lines of code guys not that important for interview point of view but you can see that this is a typical way of taking the screenshot but yeah you have to write it scenario.attach at the end it will call the attach api and this is the context image png it will pass what is the media type and the screenshot name that's it and make sure your writing as bytes because tomorrow you want to show that the same thing in jenkins or maybe some other cicd tool so that time it won't take as output type as file so if we have seen multiple options base 64 byte or file so you can take base 64 or bytes i would prefer you can take as a byte over there anyways so this is the teardown method is ready now so my hooks are ready now the last thing is that what we have to create our pages and then we have to call those pages over here so it's a long hierarchy guys right and we have to cover within the same video so please have some patience we have to at least cover one end-to-end scenario so so far what we have done feature is ready step definition we have created a template and now it's time to create the page layer with the by locator and the constructor and the page actions and element udl i can tell you later okay in the next session so let me go to the loginpage.java where it is yeah under the main login page.java so it will be written under src main java because these are my these are not my tests loginpage.java according to the page object model we know that for every page you have to create a separate page class so that's where for the login page we have to create loginpage.java all right and then i'll be coming over here now inside my loginpage.java what we have to write so inside the loginpage.java we have to write first of all three things we have to maintain the by locator we have to maintain one constructor and then we have to create the page actions and then we will be using the concept of encapsulation over here my locators will be private and the page actions or the page method will be public in nature so how to do that so every class will be having its own private web driver a reference i'll be creating so that we can hold the driver in this particular variable so let me import this and then i'll be creating certain buy locators so first thing guys we have to create the buy locators so by locators also will be in private in nature so i'll be writing that okay yeah my first buy locator is what is the email id i simply write by dot id and let's go to this application let me sign out and id equal to email nice so this application is nice that id is already available so no need to take care about other things like dynamic id and all those things so application is very simple very straightforward then same thing for the password i'll be writing it so password is equal to i have to inspect the second guy that is password and id is this one perfect and i'll be writing over here and then doing what i have to inspect the sign in button which is actually a button over here id equal to submit login so we can take this particular id nice so i can create one more private that is a buy locator i'll be writing sign in button now the second thing is that i know this will be a lengthy chapter but i cannot divide into multiple parts otherwise you will be losing the interest so please try to understand second thing is that we have to create the constructor of the page so i simply write that as my constructor of the page class and the constructor name will remain same as the class name so let's create that that is my public constructor and this constructor says that you give me the web driver and then the same web driver will be given to this particular driver so which keyword we have to write we have to write this dot driver is equal to driver we have to write and now the third thing is that we have to write the page actions and the page action should be public in nature right so page action means the features okay so we are creating the page actions like this let's see my first page action i want to write that okay find public void and i simply say that get login page title and guys we will be using the best practices only according to the page object model that page class should not have any assertion it should assertion should be written in your either test class or the step definition class we should not write assertions in the page class that is a wrong thing and okay so we will be using the best approach over here so how will you get a title i simply say that okay fine driver dot get the title over here that's it right and then i simply say that okay you return the same title so instead of white i have to write a string over here simple then i want to check other things that is public now see it carefully i want to check that okay public void that is forgot password link exist or not or present or not forgot password link is available or not so how will you check i simply write a driver dot find element and we have to create forgot password forgot your password link text also we have to create that so here you can see that forgot your password i can use this by dot link text so let's create one more locator over here that is my private buy i simply write for god password link is equal to by dot link text and the same forward password link i have to use it over here like that and later on guys we will be writing the utility for find element also for get title also with the help of proper weight if you see the design diagram we will be creating this element util.java see this element util.java we will create separately okay what now because today we are just creating that normal page class driver.find element for what password and then do what if it is displayed then you do what then you return so is displayed method let's see will give you a boolean so instead of void i have to write a boolean over here so forgot password link also ready perfect then what i'm going to do that uh see guys if you see the feature file in the feature file it's written that somewhere over here that user enters the username password and the login button so number of steps we have to return so for every step we have to create a separate page action also definition is already created so i'll do one thing in my login page class i'll just use something like this that public avoid that please enter the username so i'll be creating a separate method name please enter the username and this enter username someone will give the username from here so i'll be writing username and this username is coming from where the username will be coming from here so from the step definition i'll be calling my loginpage.java with this particular user name or email id whatever okay it's written so let's uh come over here and yeah so this guy says that okay fine you give me the username i'll enter it so how will you enter it driver dot find element email id dot send keys and whatever the username that you are passing the same username will be given to this guy so enter username is ready same thing i'll be writing for let's see public void enter password now you must be thinking that you can create one simple one login method and then put all the steps over there i'll tell you why i'm doing this okay and then i simply writing a string a password over there and this is the let me write pwd so let's say this is pwd and password locator is this password locator i'll be writing over here done and then what we have do we have to write public void and we have to do click so i simply write click on login right so how will you click simply write driver dot find element and which locator the sign in button dot click simple so method is ready right okay looks good now what i'm gonna do that uh okay so let it be like this my page classes are ready uh how many methods we have created three plus two yeah five method we have created and the by locator is also ready now we will go to our steps and if you see the diagram my step definition which is written over here login pagestep.java will be calling the page layer see this connection this integration will okay will be there so at the red given method will be calling certain corresponding method coming from page layer so remember guys in your step definition file please do not maintain your by locators do not maintain your page methods it's a ugly programming step definition file doesn't matter what kind of selenium code that you have written selenium code should be written in your page class if you're following pattern of page object model some people what they do they write webdriver driver equal to new chrome driver and then done no that's the wrong practice you should not write your driver over here you should not call your driver apis you shouldn't okay not do that or you have to minimize it like anything you should not call driver.find element all the time for all the steps we have separate page class you call it from there maintain your object repository in the form of by locators over here so these are by locators or i would say these are my object repository also or right now what we have to do after that after that we have to go to my login steps.java and here what we need we need to call all the methods of login page and i don't want to create any inheritance thing over here although you can create extend login page what i'm going to do that i don't want to create any dependency or any parent and child relationship between these two guys i just create the object of login page class over here so i'll be creating one private and login page why i'm creating private so that no one can access this particular object only this particular class can access that so this is the right practice it's not like everything is public that's why we have to make a local uh class variable not local class variable should be private as much as possible according to the particular design pattern that we are following and i simply write let's see this is my login page is equal to new login page i'll be writing and what you need actually login page says that okay i have a constructor over here so first of all let me import this particular login page and this constructor is saying that you give me the driver right guys you give me the driver so how will you get the driver so the constructor will be called when you create the object and the driver will be getting from there from your driver factory dot get driver method remember this method getdriver method will be giving you the current at the runtime whatever the driver is going on it will give you that particular driver and when loginsteps.java will be executed but before that your hooks will be executed so here um property got initialized and your driver factory at your driver is also initialized you can see that any slider driver with the browser escrow so that's why when you come over here and when you get the driver you will be getting the same driver having the google chrome over there now you got it so get method is used to get the driver at the runtime and dot set method over here i have written to set the driver chrome driver and firefox driver like that these are the two important method get and set coming from thread local okay what we have to remember this set method is actually void in nature it does not return anything it is just to set the driver set the current thread copy okay for that particular thread local variable and same thing forget it will return see this this is used to get the driver with the thread local and if you see this get method returns the value in the current thread copy of this thread local variable okay so it will create multiple copies of the same driver but having the exact if you let's say you are running on the five multi-threading concept if you are using it so let's see if you are see try to understand the concept of thread local you have this particular of let's see these four threads are available and we have written webdriver driver equal to new chrome driver with the help of thread local so every thread will get the same copy because of thread local so these thread having the same copy of the driver so this guy will attack first google chrome this guy will attack the second test case with another google chrome like that and having the same web driver copy over there right otherwise what was happening earlier if you don't use thread local so what exactly you have to do that and we don't have any concept of test ng over here and how will you implement or write the panel execution let's say from the main one you are creating multiple threads and you have one driver over here and this driver has to be accessed by thread number one and then this guy has to wait and then only thread number two will introduce will be coming and to get this particular thread local uh to get this particular chrome driver so that was the problem i can not do the proper panel testing with this approach but thread local you can do that and then you can get the multiples instance of same thread local copy of multiple drivers over here okay so let me erase this particular drawing and let's come over here go to a login page steps once again now this is what i want when you have this login page i can call all the methods of the login page because we got the object reference and i can call all the methods of login page so which method i have to call now so we have to first of all get the title of the page so title of the page method how will you get it so title of the page i'll be getting from my login page you can see that inside my login page it's saying get login page title it will return the driver.get title so i'll come over here and i simply write this is my login page dot c get login page title and when you get it i'll store it let's see string a title is equal to this and i'll be printing it on the console that something like this that my login page title is plus a title and then here we have to write an assertion guys so i'll simply write assert dot assert true and i can simple say that okay so make sure you are asserting from importing from or dot j unit so you can write one j unit assertion assert.sr2 and what is a condition a condition let's see if i'm writing that whatever the title that you are getting contains okay i think uh title of the page we have to write all these steps so here whatever the step page title should be what see this is what i'm expecting my page title should be login my store right guys so it will be given to this particular string and i'll change the variable name i simply write it okay this is my expected title name over here all right so expected title name i'm matching over here in my assertion right now what will you do in these two methods are you getting my point right i hope it's clear because page title should be so we have to verify this thing first we have to get the title and then you have to verify over here so what i can do is that user in the login page user is on the login page so first of all if you see this inside this initialize a driver method we have not returned anything with respect to application url see it will just launch the firefox or chrome or whatever but who is entering the url so let's see for time being i'll be entering the url over here so how will you enter the url i simply write my driver factory dot get driver dot get method i'll be using it get driver will give you web driver and launch the url okay let's take this later on we will move this particular url to configuration so this step user is on the login page it means let's launch this let's okay launch the url and then after that what we have to do we have to get the title so the title let's see taking from here actually so this step i'll cut it from here and i'll just move it over here so don't be confused with cucumber guys cucumber format for every step we have one certain step definition so we have to manage the code accordingly but in test ng we can write in the same at the rate test annotation or at red before method but here we have number of steps and number of steps will create a complete scenario that's why we have to segregate the code like that now this title i want to verify over here but the title scope is this only so what should i do i can declare one a string title at the class level okay so let's see at the class label and i simply declare with private all right and i can make it static so that i can okay no need to create the object i can access this directly and i can simply say that remove this particular title now the same title i'm going to verify over here so this is also solved right now we have to check the forgot password that uh forgot password link should be displayed so how will you do that so for forgot password i simply write once again that login page and that method inside the forward password that we have already written forgot password exist or not and it will give you what it will give you a boolean so you can see that it's giving you a boolean over here so i'll be writing one assertion directly over here so i simply write assert dot assert true right guys i can simply write if this method is giving you true my test will be passed this particular step will be passed otherwise make it fail done then enter username so i'll be writing this a string this is my username and this is my password so username password is actually coming from there so you don't need to maintain your username password in your configuration.java because you are passing it from here so when you go there see this press control and coming over here username will be given to this guy so what should i do i have to write my login page dot enter the username and the same username you will be giving over here see this method you remember we created this method now you got it what is the purpose of enter username so this guy said okay you give me the username i'll be using the locator creating the web element and entering the value over there that's it so this is also done then do what same login page dot enter the password which password the password that you are passing to me the same password will be given to this guy and then finally you have to click on login button and then i'll be writing login page dot click on login that's it and we know that okay with all these are when these are my actual test step over there then is also step but inside then we always write the assertion guys so see whenever i'm writing then i'm writing the assertion when is just for a action to perform something given is again the first thing given is on the precondition kind of thing right now user gets the title of the home page so what we have to do when you uh click on the login button so let's see when we enter the username password so let's enter the username password and click on login button and then do what user is landing on the home page or let's see this is my accounts page or let's say i say home page okay and i would say that's its home page i'd say this is my accounts page actually okay this is the account page actually not the home page and i simply say that like this okay now this guy say that user gets the title of the home page so how will you get a title of the home page so uh that we have to get the title so what i'm gonna do in that case in fact what we can do is guys that is let me remove this user gets a title of the page and we can remove this actual this thing we don't need to write got it why because see this user gets the title of the page this is the same step is already written over here so why to create unnecessary two-step definitions so for the two steps we can create one common step definition see and whatever the page title is coming we can okay get the page title and then we will write the page title should be my account and my store so the same will be verified with this guy but then over here so title contains this expected title name so we have to make it generic so why do create unnecessary duplicate uh steps over there you got it right so you don't need to create extra steps methods over there right so i have deleted those methods and you can see this two steps are pointing to the same method and this step will work for the login page also and for the home page also perfect and the second guy is that page title should be my account my store so it will be given my account my store and then it'll be verifying over there and the execution will be done in this particular sequence like this okay so looks good to me and let it be like that later on we will see the page chaining model when you click on it it should return the next class page class object or something like this those things we will do it later right and now the last thing is that i know it's too much but we have to create a runner so runner file is already created my test runner and we have to add certain uh cucumber options over there right so uh i'll be quickly adding those runner actions i just copy paste it's already with me okay and here we have to import certain things import that and you have to tell where exactly your feature files so i simply write that okay srt test resources and com.feature so i'll do one thing these are my app features i'll just change this press f2 change the package name to app features it looks better let's not for uh follow com. a package name convention over here and what is your step definitions so step definition is make sure there is no spelling mistake the step definitions we have written and what are the hooks app hooks we are not using the console tag right now and pretty and then we can talk about this cucumber adapter for the extended report later but i think it should work now okay so shall we run it and let's see if any error is there we will try to rectify the error we haven't executed yet so let's see it is working or not so we have to execute this particular class right click on it and run as or let's do one thing let me remove this particular guy we want only pretty output and simple run it run as junit let's see it is working or not i hope browser should be launched yes awesome browser is getting launched it's entering the url perfect and title got checked second forgot password link and the third one is entering the username password let's see see entering the username password clicking on the sign in button perfect awesome i never expected that okay it will work in okay in single shot and see all the tests are absolutely working fine right you can see the test over here three test login page title password correct credentials now what i'm going to do that if you remember we have to generate cucumber 6 ui file right so we will create one src test resources cucumber dot properties file we will be creating so that we can see the live sorry ui report also so i'll go to my resources and uh yeah right click on it go to new and create a file over here queue my dot properties got created and this is the property that we have to add it over there okay so let's run it again otherwise we will see in the next chapter parallel thing and everything guys we will check it later not today so guys framework videos will be lengthy please try to understand that you can take a pause and uh you can practice and then start the video once again i think yeah now this time report is coming so please do i may even update the project once and i'll do one thing i'll just copy this guy and we will open a new url uh over here new window sorry see that when you paste the url right you have to remove these garbage values i hope it should be loaded yes three are absolutely fine you can see a feature file over here see this awesome right and what i can do is uh let me make one test failure deliberately and let's say it's taking the screenshot or not okay one last thing guys so i'll do one thing i'll deliberately go to my loginpage.java and what i'm going to do that for the forgot password link text i'm expecting one one one so i'm making it deliberately fail i'm expecting this forgot password text like that so let's make it fail and let's see it's taking the screenshot because uh see in the hooks if you go there this hook will be executed after before quitting the browser because order is equal to one so here it will in the reverse order guys first order one and then order zero it's saying if scenario is getting failed then only take the screenshot right so let's see it's taking the screenshot or not so um let's do one thing right click on it and run as junit once again this is the forgot password nice and the third one is that the login perfect okay so you can see the report for what password got failed over here and you can see here because obviously uh unable to locate an element with forgot password111 right now what i'm going to do i'll go to this particular report copy this particular report number and we can simple paste it over here it's 99e okay okay so one feature got failed you can see that and awesome see the screenshot great right see the screenshot guys this is so nice we don't need any extend report at all the perfect screenshots are absolutely coming fine with the proper message that okay yeah in your forgot password see this it got failed the second step got failed and with the screenshot this is amazingly good in cucumber six and see this login with the correct credentials blah blah blah like that and all the steps got passed over here super awesome right you can use i would advise you instead of extended report and everything and dependency on exchange report unnecessary better you use this particular report fine guys anyways i'm going to delete this particular report otherwise it will be published okay so this is how guys we have to write the initial framework the skeleton of the framework the foundation and on top of that next time in the next video maybe uh maybe tomorrow or something i'll be telling so other components like we have to create now element newton and then we will create some other feature file like accounts feature or home feature then uh steps and then how will we integrate log into our home feature and then home page.java we have to create that i think most of the things we have already covered today and then tomorrow in the next video i'll show you the reporting part also for the reporting part like with respect to spark report and pdf report and the html report i'll show you in the next video so thank you so much guys thanks for watching and thanks for your patience if you're really really liking it guys please subscribe to the channel and uh please share with others so that people will get to know the right thing with the right approach how to design the framework end to end and please complete this entire series it will help you a lot to design the framework exactly same concept you can use with here and there's some customization or maybe some changes or maybe some improvement definitely will be there and you can do that and you can design one end-to-end framework immediately after that so i'm really sorry for this lengthy video take care guys please subscribe to the channel put a comment over there if you're facing any issue and thank you so much i'll see you in the next video take care keep watching naveen automation apps
Info
Channel: Naveen AutomationLabs
Views: 38,631
Rating: 4.9567232 out of 5
Keywords: cucumber BDD, cucumber with selenium, cucumber with POM, cucumber with page object model, Cucumber tutorials, Cucumber integration with Selenium, Cucumber end to end with POM, Cucumber BDD framework with Selenium, Cucumber Extent Report, Cucumber Spark Report, Cucumber Web Report, Cucumber By Naveen, Naveen AutomationLabs, Latest Cucumber videos
Id: NhiIAZRoc0g
Channel Id: undefined
Length: 74min 51sec (4491 seconds)
Published: Tue Dec 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.