Selenium WebDriver | Part59 | Generate Logs in Selenium WebDriver using log4j | log4j.xml

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome back to automationtestingserver.com this is selenium webdriver part 59 and today we are going to talk about how to generate log files using selenium webdriver and this is an important topic so logs are very important part of your automation testing so whenever you encounter any problem or issues then easily you can look back your code while debugging and easily you can understand the flow of the program where the problem occurs okay so this is very important topic guys so let's get started so this is what i have written what is log and why it is required so during the execution of your test case user wants some information to be captured or logged into the console information could be any detail depends on your usage or your requirements okay keeping this in mind that we are using selenium for testing so we need information which helps the user to understand the test steps or any failure during the test test case execution so you can the user should understand where the problem from where the problem comes okay so and user can understand the flow of the program as well so with the help of log 4g that is mechanism it is possible to enable logging during the selenium test execution so what is log4j so log4j is a fast flexible and reliable logging mechanism or framework written in java and it it was developed in 1996 for the first and uh for the first time it was launched in 1996 and it is distributed under the apache software license and this is used for logging mechanism for a small to large scale selenium automation projects and this this developer also uses the same log forget for logging purpose so why we are using log4j what are the advantages of log4j so first of all it is an open source api or you can say mechanism with log4j it is possible to store the flow details of our selenium automation in a file console or database and it can be used for large large as well as small projects as we have discussed in the previous slide and what are the different components of log4j so basically we have three components we have loggers we have appenders and layouts so what do you mean by loggers so it is responsible for logging the information so what are the different uh different steps to be taken to lock the further logging right for the logs so first of all we have to create the instance of logger class so there is a class called logger so we have to create the object of logger class do not worry guys i'll explain this part like how to get the loggers how to create the instance and okay and the second thing is log 4g level so we have different levels of log4j we have primarily there are five kinds of log levels we have org this level are all we have this level is logging for log everything we have debug generally we are using info because we used to print some information message informational message that highlights the progress of the application like it like at each step what is the progress we used to write using info like log dot info and then we write our message similarly we have one error fatal and off so these are the different levels and the second component is appenders so it is used to deliver log events to their destination so basically it writes the log into file or console so basically we have three types we have console appender logs to standard output so this will write your logs into console okay and the second one is file appender prints the log to some file in a text format or okay or any other format rolling file appender to a file with maximum size so basically these two are same but the only difference is this rolling file appender has some the maximum size of the file okay and the third component is layouts so it is responsible for formatting logging information in different styles how your logs will be seen by the user so that is that is uh we can that what you can that that can be decided using layouts okay and what are the different steps to be taken to create the log in selenium webdriver so we have i have written over here five steps so first of all we have to download log4jar files or get the dependency if you are using maven project okay the second is create and lock for the xml configuration file and keep it in your project directory third is create a utility class log class to initialize log 4g logs we have to create the instance of logger class so to initialize the logs create base class and configure your lock for jxml using dom configurator class so since we have this log4j.xml file so we have to initialize that right i mean we have to configure that so using dom configurator class we can easily configure that and the final step is write your test cases using test class and you can write logs inside the test cases so these are the five steps we are going to steps which we are going to perform and instead of log4j xml configuration guys you can use log4j.properties file as well both will do the same thing the only difference is here the configuration is a little bit easy so that's what i have uh i i prefer to use log4j.xml and uh but these two will do the same task will configure our lock for xml in the form of xml format okay so let's get it started my eclipse is already open here and here what i'll do i'll create a project maven project and uh just select create a simple project i'll give name over here demo project i'll name demo project one because already we have demo project and simply copy paste artifact id i'll keep it as group id okay and click on finish so here my project is created and here i have my pom.xml so what is the first step we have we need to download log for jeff jar files or get the dependency if you are using maven project so i'll at the time when i uh i'll write blog i'll write a post on this on my blog at that time will give the path how to download jar files for lock 4g so for time being since i'm using my own project so i'm going to take my own dependency okay so how you can take or you can easily i mean simply you have to write maven second log4j dependency so this is the first you can get it from here from the first link like this so i think already i have in my previous class previous project web driver concept three so what i'll do here we have pawn.xml simply i'll copy here we have block 4g so i'll copy all this xml and i'll put it over here okay so just do control s save and if you see in marvin dependencies we have this log4j is downloaded over here configured automatically by using this moment dependency okay so here you can see in this xml we have in form.xml we have the selenium java version we have testng we have this commons io this is not required so we can delete these two dependency and here we have a short so simply we can delete like this sorry from here yeah again i'll do ctrl s we have log for j so this is what we required now what is the next step uh we have to create a log4j this is done and create a oh okay sorry log4j is not done this the first step is done log for the jar files using this form xml we have uh we have this dependency over here okay so that is done the second is create and log4.xml configuration file okay so in the same project okay sorry guys we have seen last time that wrong moment dependency so here you can see the log 4g is downloaded here right we have created this new project demo project one so for log4j.xml i'll take from the previous clause only log4j.xml i'll take this complete xml and what i'll do i'll put it over here right click and we'll create a file and here i'll give log4j dot xml click on finish and in this log4.xml will put the xml file which we copied from previous class so let me just quickly explain what uh what it does okay this configuration file log4j.xml so here we have appender tag so the name of this appender is console so this will this part will write the logs onto the console okay and yeah this is the class console appender using this we'll write our logs on console the param name is target and this is system.out the value is not out so that means nothing but your console and this is pattern layout class okay using pattern layout class simply will uh this will decide like how it looks our logs will look okay so this is the format we have given over here so simply you can copy paste this xml for your purpose and i'll provide this xml in my blog guys going forward and the second is second appender we have this file appender so we'll write our logs into the file as well so for that we have called this rolling file appender so if you see earlier we have discussed in appenders we have three types right console appender file appender and rolling file appender so so far we have used this console appender we have discussed this console opener and the second one is rolling file append i'm sorry this rolling file appender okay now uh this is to just uh initialize or call those uh uh give the reference of those console and file appender over here okay so this is done this is uh the configuration file log4j.xml now what is the next step we have to see create a utility class lock to initialize log further logs so how to create the log utility class what i'll do i'll create inside this main java i'll create one package that's it calm dot utility click on finish and what i'll do from previous class previous project only i'll copy the log where is that i think we have not here here we have that log file simply i'll copy that uh class and i'll put it over here i'll explain this class what it does basically so if you see this log class okay so here we have initialized log4j logs we have initialized this logger class this is the variable and we have called this gate logger so what it does is retrieve a logger named according to the value of the name parameter so it will uh simply we have to provide the parameter of our clock class log dot class dot get name so it will pick this class name so this is how we can initialize our logger class okay using the yeah logger class these are the logger class and uh this system class okay this is the center class in the log 4g package most logging operations except configuration are done through this class so all the locking logging mechanism will be done using this logger class and simply we have written some custom methods like what is the message we want to give when we start our test case start test case so this is what we have written custom method and here is the information so i'm simply i'm calling this info method using this object this is variable okay this variable so log dot info here simply we have given some message okay and this is the test case name similarly we have given uh we called we have created this end test case and this is the message same message okay and uh we need as we have seen different levels of logging right uh like all debug info 1 error etc so all i have created uh static methods over here you can see here info i have created here info method born method error method and similar inside that i am calling the system method which is info log a message object with info level and you can see here log a message object with one level so this is for warning and this is for error log message object with error level like that so already i mean i have created the static methods for all the levels okay so this class is very simple this uh log class okay so this is done what is the next step we have to go for yeah so create base class and configure your log4j.xml now our log4j.xml is ready our log class is ready now we have to create a base class to configure this lockverse.xml so how to configure so inside this main java folder we will create another package com dot base class and in this base class i'll create class called i'll just rename it to smaller base class yeah and here inside this package i'll create base class click on finish and in this base class what i'll do i'll create one method public void let's say before sweet so we'll create testng.xml and we'll run our suite okay and i'll give annotation over here before sweet i think we have to add test in the library and import before suite from org.testing.annotations this is done and what i'll do here in before sweet method we have this class name right dom configurator so simply write dom configurator llc and we have configure method in this dom configurator class so yeah this you can take and here we have to give the file name so what the xml file name which we have created for log4jso log 4g dot xml so this is done now we'll write some logs over here how to write so our class is since we have created a static static method over here in this logs log class so simple will call using class log dot info and here i'll put some message like this is this is before sheet method like this once again this will be configured here so we have to write our message below this because before this whatever you'll write that will not be logged because our xml is uh configured in on this particular line okay so we'll write our step our logging related things over below this line and this will run at top guys the first method will run this before speed and after that whatever we'll call test classes test methods no need to configure the xml again and again okay so this is one time activity so that's what i have configured this in this before sweet annotation in this before sweet method and simply let's uh yeah let's probably void right uh after sweet after method okay and yeah here i'll write after sweet like this and simply i'll copy this log dot info and this is after sweet method so our base class is ready later on when we'll automate any application then i'll show you how to setup how to initialize the browsers and all that will do later on now let's create a test class this is base class so we'll create our test class and this src test java folder okay so right click here and here we have let's create package first com dot test click on finish and inside this package i'll create a class called test class let's set s class one and here i'll write some test cases public void test case one and uh yeah simply we'll write some logs log dot and if you remember we have created we have uh start test case and end test case method in this log class okay so we'll call these two methods from test class so let's call them start test case and here we'll give test case name which is test case 1 and log dot info we have info and right here this is test case one and at the end we'll call and test case so this is how we write our logs in test class in test methods okay and simply here we'll give this test case 1 so our the first test case is ready and i'll put annotation test over here we have to import test from origin testing dot annotations simply will call some more i will create some more test cases so let's say this is test case number two here i'll put two two and two over here this is three three and three three over here so guys our test class is also ready we have three test cases okay test case one two three and our base cos base class is also ready now let's create uh testng.xml so how to create we have this project demo project our project and right click we have testng convert to testing let's keep sweet name as sweet test them as a test only and click on finish our testng.xml is ready and here i'll create one more i'll provide one more class so we'll call our base class as well so what is the package name we have to give the exact package name base class this package name dot base class this class so everything is ready now class name is base class okay so this is our sweet file xml file testng.xml let me just recap what we have done so first of all what we have done we have created the pom.xml and add the dependency of this log4j the second step is create an x log4j.xml configuration file so this is our log4j.xml configuration file and i'll provide this xml in my blog do not worry about this one and the third is create a utility class so this is my utility class log clock class where we have initialized the log using the logger class okay and we have here we have created some custom methods like start test case and test case and uh we have uh uh for info i mean for log levels we have different methods like info one for them i have created a static method so that i can easily call in my test class and we have this create base class and configure your log4j dot xml using dom configurator so we have created base class and here we have configured this log4j.xml over here you can at last write your test case using test classes so here we have one test case testclass1 and here we have written the test cases now simply will uh execute our test testnj.xml so right click and run as testng suite just run it yeah this is done i guess so let me open this so guys here you can see my all test cases are passed one two three and if you see the console we have we can see it gives time uh date third may and this is the time and here you can see this is before sweet method okay which we have called from base class we have written this message in base class and before suite method and here you can see from test classes test class test case 1 is called ok test start test n so it is ending here for test case 2 it is ending here and this is test case 3 start and ending over here and after that this is after sweet method from after sweet method method okay so this is how you can configure logs in your project guys so now we'll see real time example let's say in base class we have we'll put our setup method to initialize what i'll do in previous driver concept we have set up method so simply i'll copy that base class i think we have after this base test yeah i'll copy this setup method before method and i'll put it over here on this in our base class so this is uh this to launch the browser and here we have application well i think this is open source cms that uh wordpress website yeah so we can use this no problem and we have to declare what driver driver a top webdriver driver and let's declare it public so we can use in our test classes as well so guys this is setup method to launch the browser every time this will be called before our test method okay simply you created the object of chrome driver so from browser we launched and will maximize the window and we navigate to this wordpress uh application okay so this is about base class we have configured this setup method now in test class i'll create and yeah this is our project and in test class i'll create another test test class i'll give name test class 2 okay earlier we have test class 1 and this is test class 2 click on finish public void wordpress test word press test okay now we need that web driver so what we'll do will simply extends base class this class from here okay yeah so we extended the base class so we can use the driver now simply what we'll do we'll inspect this username and what is the user id id is user login so we can use this driver dot find element by dot id and put the id over here dot send keys and i'll provide the username open source cms and similarly we have for password as well so id is user pass so what we'll do simply will copy paste here id and the password is the same now third would be click on login button so we'll inspect this so this is the id for login button here we'll write driver dot find element by dot id and we'll put id over here and then we have to perform click operation so this is how we can log in into the application now let's call those log methods okay so we'll first we'll call this info method so this is uh directly we can call that start test method start test case method so here we can write this wordpress test okay and yeah and after that we can call info method here we'll provide entering entering username username entering username and after the this is the name and then password and third one would be click on click on login button so three messages three logs we have written so far and after login that after clicking on the login button we'll write home page simply will write home page and uh yeah once let's get into the application and we'll write open source cms after logging into the application what we'll do [Music] let's get the current url and we'll will validate that okay so how to validate driver dot git current url we have method okay and that will give in the form of string and here we'll write url string and simply will uh will compare okay so what we can do here is we'll write assert dot second sir dot assert equals asset equals and this is our actual right so put actual over here and what we expect and this is what we expect so you can put your url here or we'll do one thing like write one more line over here string expected url and this is our url and simply i'll put url over here this expected url variable okay semicolon at the end so this is how we can validate our url the current url after getting into the application let's write some log here as well here i'll write great get url or let's say current url and after that after getting the log getting the url will validate okay so here validate the url and we are using this asset will validate the url whether it is expecting whether it is as per our expected expectation or not and at last what we'll do here will write url matches okay and at last we'll write log dot and test case what is our test case wordpress test so this is simple flow guys uh the login to the application and after logging into the application we are capturing the current url and we are just simply validating this and in each steps we have written some logs log dot info like entering username password like that now we have to configure this test class 2 in our xml as well so simply will configure that in our xml put it over here and stress class 2 so our xml is also ready and what i'll do i'll clear this now we have two test classes okay test class 1 and test class 2 test class 1 we have some simple test cases three test cases and there is one test place one test class in this one and what i'll do in base class will write after method as well so that we can close the browser every time so public void after test method yeah our teardown right tier down simply write over here after method after each and every method it will tear down the browser so we have driver dot close so i think all done we can run over and what i'll do i'll clear the logs as well from this logs file log for the dot log file okay so everything is ready so simply i'll rerun this xml again run as test in this fit so browser is launched and logging into the application now and so all cases are passed so we have four test cases three in test class one and one test case in test class two so here you can see the logs on the console for first three test cases it is logged over here and here you can see wordpress test starts and entering username entering password click on login button so we can get beautiful logs like this here you can see the time being capture home page get current url validate the url url matches okay so this is the end of this wordpress test and if you refresh this project we have this log4j.log file is being generated so here you can see the logs in this file this beautiful log right so here you can see entering username enter password click on login button so all the steps are being captured over here in the form of blocks so this is how you can use the locks in your project and let's do one thing let's let's let's say if you get any issue in the in any particular line okay let's say on this expected url i'm changing something over here okay so ideally this should not be captured this url matches so easily you can you can you can come to know like where the problem occurs okay so simply i'll rerun this testng.xml again first let's delete the logs from here so that we can understand better now let's rerun this testng.xml again browser is launched navigate to open source cms click on login button and yeah so you can see failure one out of four test cases so this is failed because of addison error expected this but we found but found this one okay expected we are expecting here we have uh we put one so that's what it is failed now if you see the logs if you go to console so these the logs created this is for testcase123 and so here you can see wordpress test entering username entering password login button home page get current url validate the url okay and after validating the url we are not getting the url matches or not that result so we can easily can uh figure it out that uh our task is getting failed after this line okay and then we can easily debug our code so this is this is how you can i'm sorry so this is how you can configure log4j in your project guys so thank you for watching my videos and thanks a lot please if you're new to this channel then please subscribe this channel thanks a lot bye
Info
Channel: Automation Testing Insider
Views: 9,093
Rating: undefined out of 5
Keywords: Selenium WebDriver, Selenium WebDriver Tutorial, Automation Testing, generate logs in selenium, generate logs using log4j.xml
Id: _ko-6fOUQXY
Channel Id: undefined
Length: 42min 23sec (2543 seconds)
Published: Wed May 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.