Building Selenium Automation Framework from scratch in 30 minutes! || TestNG || Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i hope you're doing great and are taking care of your health in discovery times in this video we will learn how to build an automation framework from scratch there are a lot of people who have just started doing automation and are really struggling onto how to make an automation framework and nowadays a lot of companies have put an automation down in their interview process in which you need to build an automation framework and using that framework you need to automate a particular flow so keeping that in mind i'm making this video the framework that we will be making will be based on page object model it will support multiple browser execution and it will be generating an extent report whenever the test execution completes along with these it will also take the screenshot whenever a test case fails and it will also retry the failed test cases so now let's get into it so let's start by making a new project so i'm going to new then project and i would be making the project as a maven project so yes let me name the group id as com.sameer and artifact id which would be our project id or the project name as youtube framework so this is fine this is fine finish and yeah okay so our project is ready now let me enable the auto import because i will be adding dependencies in this project so as we can see our source main java in the source test java already ready here and i've already taken all the dependencies that i would be using in this framework making so i have pasted it down here so that it saves our time so i'm copying it from here and i'm pasting it here let me remove this and yes this is now ready so i have added very basics dependencies here so this one is for test ng this one is for selenium and this is for the extent reporting that we will be generating after the execution is completed and this is for as we are making the project as the maven project so we will be compiling it by using the maven command so this is for that we have mentioned the maven compiler plugin here and in the configuration under the suit xml test i have mentioned the testng.xml file which i will be making the project through which we can also run the project now let's start making the folders that we will be requiring in the framework making so i'll be making three folders here one will be drivers inside which we will be adding the chrome driver and the firefox driver one folder for these screenshots inside which we will be saving the screenshots whenever the test case fail and the last one will be the folder for reports so here we will be generating the report and will be putting under this particular folder now let's make the testng file through which we will be running our code and we will be mentioning the [Music] uh the test that we will be running or the class that we will be running so let me add the testng.xml file to get the chrome driver and the firefox driver you can go to their website and then you can directly download it from here so for chrome driver you can download it from here depending on the crop version that you are using so if you go inside you will be getting all the types of chrome driver okay for linux mac and windows machine and same for the firefox you can go to their website and then you can download the latest version of the firefox driver so i have downloaded it now i will be pasting it into my drivers folder which will be here so here we will be making a page object model which means every page will be having a class of its own so let's say there are two pages one is home page and one is login page so everything which i will be doing on the home page will be having a class of its own and everything that i will be performing on the login page will be having a class of its own so now i will be making three packages under the source main java our first package will be named as page events under which we will be having different classes for different pages and we will be operating on those my second package will be having the name as page objects inside which we will be adding all the experts ids or all the identifiers and my third package will be of the name utils so everything that we will that we will be using across the project will be inserted under this particular package i will not be using this resource so i will be deleting this and inside the source test java i will be making one class which is my base test so now we have the whole skeleton of our framework ready so now let's jump to the coding part now let's start writing our base test so here i will be using four annotations for test ng annotations which will be before test before method after method and after test so let me just make a method inside this and let me just put a very basic name here and save for these annotations this will be before method method and this will be after method method and this will be after test method now let's make our webdriver so public static web driver driver so this web driver i will be initializing under my before method annotation and i will be quitting it in the after method annotation so let me just do that for the after method okay and now i will be making a common method to set up the driver which will be public void setup driver and this method will be taking a value of the browser name okay so if let's say if my browser name is equals ignore case chrome i will be initializing the chrome driver else if my browser name equals to firefox i will be initializing the firefox driver and else that will be my default case or manual case i will be initializing the chrome driver here this should be firefox so now let's put the values under this system dot set property webdriver.chrome.driver and we need to pass the path of the chrome driver which will be system dot get property user dot direct which will be giving the path of this particular directory of the project plus file separator i'm using file separator here because we basically use different annotation for the linux machine or for the windows machine or for mac so file separator will be working in all the operating system plus drivers which is the folder that we made plus again file dot separator plus the chrome driver this is done now let's do driver equal to new chrome driver so this will be initializing our chrome driver here and same goes for firefox we need to mention geeko here and this should be firefox driver and my default case is for the chrome so i will be pasting the same here okay so this is also done so now i will be calling this method from the before method so let me call it from here and i will be sending the browser name here so this browser name we will be picking it up from the sng file for that i need to use the add parameters annotation and inside that i need to mention the key through which we will be picking this particular value so this should be browser name and then we will be parameterize this particular method so we will be picking up the value from the sng which will be getting inserted here and then which will be pass into this particular method and accordingly the driver will be initiated so after initializing the driver let's maximize the window which will be driver dot manage dot window dot maximize it will be maximizing the window and then let's open the url that we need to open using the get command driver.get command and inside this i will be basically passing the value so we will be using utils package here and we will be inserting the url into a particular class here so i will be making it as an interface because all the data types that we will be putting it inside the interface will be automatically public static and final so let me make it as constant and then let me put a string value as the url so this will be so we will be basically using amazon's website to automate our particular single flow so i am putting the url as satps www.amazon.com okay so so here now i am basically putting the value as constant start url let's put the implicit weight as well here driver dot minus dot times out dot implicitly wait and let's put the value as 10 and the unit as timeout dot seconds so everything related to the driver is now done now let's start working on the extent report that we will be generating whenever the test execution completes for the extent report i have already coded all the lines so i will be just pasting down into my project so we will be initializing the report here i am importing all the classes that are required okay and in the before test i will be initializing the report these are very basic commands you can find it on internet on multiple websites so i've given the path as the user directory plus the reports project that we have made and then we will be generating report as automationreport.html and then we are putting the document title report title we are setting the theme and then these are all just additional information which we are putting into the report and in the before method i will be creating a test so let me put it on here so for the test method we basically need to pass the value here which will be method and then test method let me just import the class here as well okay so it will be basically picking up whichever method is getting executed and then it will be creating a test according to that particular method name and in the after method we will be basically putting on the results that we will be getting from that particular method name so let me put it on here and then we need to basically get the results using i test result and let me name it as result so these are all just additional values which we can basically add to our particular reporting so yes so if the result is a success we will be logging it as a pass if it's a failure we will be logging it as a failure okay and then we are just putting all the color as red and green and in the after test we will be basically flushing out the report which is extend dot flush so everything related to the reporting and related to our base test now is ready so as we will be taking this screenshot whenever the test case fails now let's work on the suit listener class so here i will be making a class with the name of suit listener and this class will be basically implementing the i test listener so as soon as you implement it it will be telling you to implement all the methods so on the on test failure we will be basically taking this screenshot now let's first make a file name string so this will be basically the name that we will be putting on that particular screenshot so let me take the path first file dot separator and we will be putting the screenshot under the screenshots folder so this will be screenshots plus separator and then let's give the name as i dot get testresult.getmethod.getmethod name so we will be getting the screenshot with the method name now let's take this screenshot using the screenshot method which is take screenshot and here we will be getting the driver bases dot driver dot get screen shot as which will be output dot file now let's copy the screenshot which will be file you tills dot copy file let me just zoom in first okay so this will be f which is the file which we have taken here and then it will be new file and inside that we will be giving the file name plus dot png which is the accenture of that particular screenshot so let me surround it with a try catch block so whenever a test case fails we will be taking this screenshot and then we will be mentioning the name as the method name plus dot png along with this we will be retrying the field test case as well so whenever a test case fails we will be retrying it and the count of retry can also be managed okay so i will be making class here and i am naming it as retry analyzer and this retry analyzer class will be basically implementing the i retry analyzer interface so let me implement the method okay i will be taking a count here which will be starting from zero and i will be taking a retry count here so this retry count will be the count on which you want to retry a particular test case which got failed okay so if i'm putting it as one the test case will be retried once if it's two it will be retried twice okay and inside this particular method we need to write the condition for it so while this count is less than the retry count i will be incrementing the count and then return true and this retry analyzer class will be managed by the suit listener class so if i open this i need to implement this root listener class with the i annotation transformer okay and i am implementing the method so this is the method which will be calling the data analyzer inside this i need to mention the method name which is i test annotation dot analyzer and then i need to give the class name which is retry analyzer dot class so whenever a test case fails it will be going to this particular retry analyzer class and then it will be retrying that particular failed test case so now let's make a class inside which we will be writing the methods which will be returning the element on the basis of the identified type so i am making a new class here and i am naming it as element fetch and i am making a method here which will be returning me the web element get web element and it will be having two parameters which first one will be the identifier type and the other one will be the identifier value this is also string okay and inside this i will be making a switch case which will be taking the value of the identifier type and my first case is id so if my identifier type is of id i will be returning the value as is this dot driver dot find element pi dot id and inside this i will be passing the value of it this is my first case and like this i will be having multiple cases here so it can be css so it will be returning the value as pi dot css selector it can be tag name so here it will be tagged in as well and it can be xpath so it will be returning the value by fetching the element by xpath and let me make a default case as well inside which i will be returning the value as null and similarly i will be making another method which will be returning with the list of elements on the basis of the identifier type so here just the difference will be driver.find elements instead of find element this should be get list web elements so now throughout the project whenever we will be fetching the elements we don't really need to write driver.find element again and again we will be just using this particular method and we will be passing the identifier type and the identifier values in this and it will be returning the element now let's start automating the flow for that let's go to amazon's website so this is the amazon's website and this is our home page of the amazon and if i click on the sign in button i will be getting redirected to the login page okay so here we will be operating on two pages this is the login page and this is the home page okay so now i'll be making two classes for both of these pages into my framework so in the page events i'm making a class and i make and i'm naming it as home page events in which i will be adding all the operations that i will be doing on the home page and i will be making another class here which will be the login page events that's it and then in the page objects i will be making two interfaces here because we will be adding all the x parts or all the identifiers in this particular class or interface so if i'm making it as an interface any data type that i will be declaring into it will be automatically public static final and as our expert doesn't actually change so it's better to make it as an interface so i'm naming it as home page elements and another interface with the name of login page elements now let's go back to the amazon's website so if i click on this or on this button it will be redirecting me to the login page so now let's find the expert for this button so we have the text here so we will be using that to find the xpath and the tag name is pan so it should be this so we have got the expert now let's put it on our project we go to home page elements so i'm making a string here and this will be sign in button and this is the export for this and now if i click on this i will be redirecting to the login page so i will be checking that if i've got on the login page correctly or not for that i will be checking if this login text is getting displayed or not and after that i will be entering the value into this email id field so we need the identifier for this login text and for this email id field now let's find that so we have the login text here and the tag name is h1 so the xpath should be this okay so if we have find the expert let's put it in the login page elements login text which is this and we have the email id field which has an id here so we will be copying the id email address and we have pasted on the idea now let's write the code to operate on those elements for which i will be going to the homepage events here i will be making and method click on sign in button inside which i will be initializing my element fetch class new element fetch and now i will be doing element fetch dot get web element and then i will be putting the x path here because we are identifying the sign in button by xpath and we will be picking the value from the home page elements like homepage elements dot sign in button and now i will be using the click operation here that's it now let's go to the home page events here i will be making two methods first one will be asserting that the login page is opened or not and second one will be adding the value into the email id field verify login page opened or not so whenever an element is displayed on the page we can find the element and then we can fetch the element by putting into a list okay so whenever we are fetching the list of that particular element if the size of the list is greater than 0 it means the element is present on the page okay so we will be using the same logic here to assert that the login text is getting displayed on the page or not so element fetch element fetch equal to new element fetch i'm making an object of the element fetch class and then assert dot assert true and inside this i will be fetching the list of the login element expat comma login page elements dot login text this will be returning the list of the login text and then i will be checking that its size is greater than zero or not and if not i will be giving a message that login page did not open okay so if the list of the size of that particular element is greater than zero it means the element is present on the page now let's make another method to write on that particular email id field enter email id again this line of code and then fetch dot get web elements so the email id field we fetched using id so i will be putting id here and then login page elements dot email id address dot send keys and i am putting any value here which is sameer gmail.com so that's it so these are the things that we will be doing through our automation framework now let's make a test class which will be doing all the operations and which will be having the flow of our code so i'm going to the source test java and i'm making a sample test here okay so this class will be extending the base test and inside this we will be having the added test annotation and we will be having a sample method for email entering which will devoid and inside this i will be initializing the home page events and the login page events because i need to use their methods inside this so let's do that and now i will be calling that method click on sign in button after that i will be making the login page events login page events and then i will be calling the login page events methods so first i will be verifying that the login page is open or not and then i will be entering the value so that's it now the code is ready and now comes the final step which is to write the testng.xml file so let's do that now let's go here and i've already written that so i will be copying it from here and then i'm pasting it here so here you can see i have added the listener tags inside which i mentioned the suit listener class through which it will be basically taking the screenshot and it will be applying the retry logic as well and inside the test tag i have mentioned the parameter name which is the browser name which will be basically deciding on which particular browser you want to run the code if you want to run the code on firefox you can mention the value as firefox here and otherwise you can mention it chrome and inside the classes tag i mentioned the class name which is sample test here and now everything is ready so now let's run the code i will be running this sng.xml so it will be initiating the chrome driver and then it will be clicking on the sign in button and then it will be verifying if the asset is passed or not and then entering the text okay so we can see everything is passed now and we have got the report as well here so this is an html report now let's open it in a browser let's open it in safari and see how the report looks like so this is the report that we have got and we can see the test case which is passed here we can also add the loggers here for that we can go to any particular method and we can add let's say base test dot logger dot info and we can add anything here so clicking on sign in button okay and similarly we can do it on the login page as well so i am adding another logger here so i am adding verifying that the login page opened or not and then entering the email id let's run it again and see if we have got the logos or not so the execution is completed now let's open the report so here you can see that we have got the lockers here as well and then we have the test case which is passed now let's try to fail this particular test case and let's see if the retrial logic is working or not and if you are getting the screenshots as well or not so for that i can basically go into this particular field and then i can fail this particular test case by putting the value as equal to equal to zero but because the element is there this sure this asset will be failing and this screenshot will be taken and as soon as the test case failed it will be going to the retry logic and the first test case will be marked as a skip and the second one will be marked as a pass or fail depending on its result so as we have a skip scenario as well here so let's move the skip scenario here as well in the base test so i will be adding another condition here so whenever my test case skips i will be marking it as yellow and i will be logging its status as skip okay now let's go to test ng and now let's take code and see if we are getting the screenshot or not and if the data logic is working as expected or not so it has opened the webpage it is clicking on the login page and you can see it has again opened the webpage which means the previous execution got failed now let's go to the intellij so here you can see this is ignored and this is again the retry logic in which we can see that the assertion is failed and you can see that we have got the screenshot as well here so here is the screenshot of the login page now let's open the report and see the results here so here you can see that we've got the skip test case and the field test case so our retry logic and our screenshot are working as expected let's run the code on firefox and see if the parameter logic is working or not so for that i will be entering the value as firefox here so if i go to the base test it will be going under this condition and it will be opening the gecko driver so this should be gecko driver and not controvers now let's go back to the tessenti file and let's try to run this and we can change the condition now to the correct one so that the detail logic doesn't take much time so yeah now let's run the code and see if we are able to run the same code on firefox or not so you can see that the firefox browser has been opened it has clicked on the element and then it has entered the text as well and let's try to open the report so so now we will be getting the updated report and you can see that the test case has been passed and we have got the right report and as this is a maven project you can also integrate the project with jenkins and you can run your test using the command nbn test so we have successfully made an automation framework which is based on a page object model which supports multiple browser execution and it generates an automation extend report whenever the execution completes it also takes the screenshot whenever a test case fails and it also retries the test cases which god failed the first time i hope that you have liked the video and you have learned from this video do like and share this video if you find the video helpful and do let me know the feedback in the comment sections also subscribe to my channel as i've already made a lot of videos which i will be putting in very soon thank you everyone
Info
Channel: Sameer Arora
Views: 24,082
Rating: 4.9109588 out of 5
Keywords:
Id: V840FH_-r8I
Channel Id: undefined
Length: 40min 35sec (2435 seconds)
Published: Tue Oct 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.