How to Implement Logging in Selenium BDD Cucumber Framework | Log4j2.x

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone this is SMY welcome to Q automation classes so today is the class 19 and today we're going to learn how to implement the log 4G into the existing framework so why exactly log 4J right so you might be wondering we have system. out. print which locks the output to the console but what is the need of the log 4G again right so when you see the console output right so console outputs are for one particular run only right suppose you are running one script so after running that script whatever the system. print statements are there those will be printed right so those will get printed here but and now let's say uh there are thousands of the test cases okay let's say an inter tion Su you are running so you running the inter regation suit it has thousand plus test cases so what will happen there so you keep on running thousand test Cas it will take some time after some time when the execution is done you come back and see uh you can see the what happened to the fail test cases why exactly they have suppose few test cases failed so you can come and open them and you can see why exactly they have failed right so you either you can go through the console whatever the cons statements are there you can understand or you can go to the log file that has been generated right and there you can see the log related information like those those specific like print statements will be replaced with the lock statements and that will be generated and kept inside a log file okay so let's say you are not going to work on that one because you are occupied with some other work and you want your colleague to take care of that failures right to look into that failure and fix it so let's say there are th test case that has been R so there will be some 10,000 or 20,000 of the SOP statements are there so that will be logged into the console so you have to go to that particular find that particular uh test case which has been failed out of those statements and then go and copy that and give it to your colleague to look which is not a very good approach so what you can do you can share the log file location and say okay hey this is the time stamp on which it has failed you go and check so the your colleague can go and open that log file go to that particular time stamp and can understand what exactly happened at that particular time St what is the exception got raised which state script got failed right so what is the exact exception all those things will be listed and there is one more benefit of the log forj is like when we are generating the log forj right uh log files so every time we have the option to keep on appending the output to the existing log file okay so but in the console log every time you run your test script the earlier uh statements PR statements got erased and for the current run what about the SOP statements are there that are executed and shown over here but in log 4J log file okay so if we are generating a fil so you can configure that instead of rewriting the existing content it just go and happen so that it servs as a history also right so you can understand what happened a few execution back right so what what is happening with the current executions you can also have a history of the things over there so that is the main reason of the using the log 4J but how to implement the log 4J right so if you are following my tutorials in our tdd framework session right you have seen log for right so let's go to Q automation glasses classes. blogspot.in okay I'll just show you so here you go to log for. 2XL okay so we are going to use the log forj 2 because the log forj 1 there were so many vulnerabilities and it is no longer supported okay so if you go to the mavin central repository let's say Marin Central repository my repository you go and open it and you type log for over here okay so let's say log 4J okay and let's it enter so apach log 4J open this one so if you see Legacy version of the log 4J loging framework log 4G 1 has reached its end of life and is no longer officially supported it is recommended to migrate to log forj 2 so if you see up to 1. 2.17 was there in the 1.x but everywhere if you see all the versions has the vulnerabilities right so instead of that they're saying okay you go to log forj core and that is that is the log forj 2 so you can click on this one and here you can see this is so okay so let project project is used to Capt of application or our automation frame executions that's so we can the information we need and that can be shown on the console so that it easy to understand the flow or we can start keeping the log information into a file and we can see the details there as okay so the benefit of creating a log file is that at any point of the time you want to refer to the any of the execution you can do that okay and uh log 4J log 4J 1X vulnerabilities so we are going with the log 4J 2 okay so that's what we are doing here log forj core and log forj API these are the two things we are going to use okay and if you following my framework development session in the tdd framework development session you might have seen right I have there was a separate video on understanding the log for J okay I'll leave that in the comment I button you can click and go through that one so there I have shown you like how to uh download the dependencies through the pom.xml and then what are these log levels are there right so if you see there are eight log levels are there so first is the off then fatle error one info debug trace and all okay so if I'm saying off if I'm saying off so no logging at all so no logging will get generated okay nothing will get logged if I'm saying fatel so whatever I'm saying log. fatle those fatle related uh messages will be printed if I'm saying error so then fatle and error both are going to get printed if I'm saying one fatal error one if I'm saying info fatal error one and info if I'm saying debug so fatal error one info and Deb if I'm saying press then if are saying log. F suage log. error suage log. one subes log. info subes log. debu sues everything is going to get printed on the console and what about the file if are printing to the file that also going to do okay so this is the log level right so if you see first one is off that means nothing will be printed then is FAL only the log. fatle related info will get printed then error right log. error so basically if you give log. info so if you in your code you are giving log. info log. one log. error log. F all those are going to get printed okay makes sense so I will request you to go through that uh session I will share that in the IE button you can click and look into that okay so okay so now let's understand how what is this XML file right so now we said okay so we'll just download this dependencies and these are the different log levels are available but we have to use it in the code right so how exactly we can use it first we have to create log forj 2properties or log forj 2.xml there are two ways we can have the configurations okay so configuration can be done in two ways so here I'm just mentioning log for do Properties or log project 2.xml so there are the two configuration properties file either one is fine you can go with either one of them you don't have to go with the both okay so I'm just mentioning here log for J2 properties but uh actually we're using log4j2.xml okay so and the file name should be log for J2 right because we're using the log forer two versions and then we have to keep it inside the resource folder right and uh if we keep it in the resource folder the log forer is going to scan it enter project directory and find this configuration file okay and there we have to define something right so what we have to Define in log for. Exel we have to defend two things okay one is the appender what kind of the appender we're going to use so appender means where exactly you want to log in so I'm saying there is the first appender is a console appender and we're going to print to the standard output okay that is nothing but your console okay and we are going to follow some pattern in some pattern it has to generate so hour minute second and uh these are if you go we'll do one thing we'll put that and we'll go through the uh runs and we'll see and we'll compare what exactly is that this pattern okay so similarly we the file if you see I'm saying okay so go to this log folder and inside that create a file called app. log okay and inside the app. log also I'm using the same pattern okay and uh appenders appenders loggers okay so root level I'm saying info so here the hierarchy will come into the picture Okay so usually what we do in the code we most of the time we write the info right sometimes occasionally you can also add one error and fatle messages okay so if you want to add them also and you are saying info then all those information you can see on the console but usually we will say info and we will set the log level to the info so whatever we have given those those things will get printed okay so now uh let's go to intell let's go to our form. XML so let's add the dependencies so have repository okay take the valid stable one 2.3 right this is 2.3 yes okay so copy this one log for the core so go to the dependency section and here add log for the code and there is one more loog for the API so go back you can search for log for jpa also but here I can see uh log for jpa okay cool and copy this one and it here the dependency you go and save it okay clean Val comp package okay so now if you see this log 4J core and log 4J API that has been added okay so controls let's save them okay and if you see clean validate compile let's compile okay so done so now let's see uh let's make changes to our automation scripts so what changes we need to make to our automation script see log for over here it should be present here somewhere log 4J see logging log 4J 2.23 and log 4J these two got added over here right it was that ear and these two got added okay so now what I need to do let me close this one and let me go to the base TS okay so let's start implementing from the Bas step so what exactly we did in the base step okay so first we have to uh Define the loger Okay so what exactly is the logger and how we can uh Define that one uh log fure 2xm let me quickly check one more time okay fine so I have already added the log4j2.xml in the uh resource section right so this is exactly same as what I was showing you here okay so this is exactly same file okay so I just copy pasted over there so here you should have log for. 2.xml where we have the console appender and we have the file appender okay Mak sense so now we have to make changes to our uh code base so let's start with the Base Class Okay so so so first we going to the base class we have to import few things okay so what is the base class base stpes okay so in the base stpes Let's uh import first okay let's say import what we going to import we are going to import or do dot uh or AP do loging right so loging DOT log for is coming yes select that one then we have to import logger and log manager both of them we need to do so first go with the log okay and similarly we need to do the log manager so import sorry UT uh or do Apache do login. log pro. log manager okay so both the things we have imported so now these things are going to get used so now first we have to Define so what you are going to Define we're going to define the logger so let's say private static uh not static just a private logger logger Okay so logger logger so you have seen this logger is nothing but your interface right so public interface logger so for this logger interface we are going to Define a variable okay and now you cannot create the object of the loger interface so you need to go to go through the class okay so how to do that so let's uh Define this uh loger right okay so in of loger just less log okay and uh sorry so inside this now open URL so try Okay so let's go and see properties has been loaded so here we say log equals to so log manager log manager is the class okay so log manager okay dot get logger so get logger okay so inside this get logger what we have to give we have to give the class name okay so there are two ways we can give the class name either you can give the name of the class fully qualified name like I can give here like automation utilties automation utilities dot okay dobas steps okay so base steps this is one way to give and another way we'll see uh dynamically getting the class name okay when we go to the other class we will give but here why we giving that also I'll show you by changing this name to the dynamical name and we'll see what is the issue with that one okay so we'll see that later but now let's update all those system. Ral with log. Ino okay so yeah so this one should be updated to log. info info okay so log. info and here also log. info so log. info this is also done and here also log. info log. info okay driver is closed so B step is done okay so after the base step okay so CH we need to changes make the changes to the step definition okay so step definition let's go to our step definition classes and uh page object classes also we can make so let's go to the step definition and again we need to uh import those two right so let me copy this one first let's do one thing let's put this to Imports everywhere first okay so sign up or login page also I can put okay user homeage step definition okay so now print Okay so in the step definition I have already uh gave this log manager and logger and now let's define private logger log okay so let's say private uh logger log private logger log and then log equals to to the application right here I can Define so let's say log equals to what we're going to say log manager dog logger okay do get logger uh and here we can have the dynamically getting the class name right so how to get the class name dynamically let's say this dot get class get class. get name okay so get class. get name so this is going to get the name of this class and how you're going to do log manager. G loger this.g class. get name this will dynamically get the name of this class and it will show okay in the response right whatever the console we're printing okay so let's go and uh update this uh sop statement do we have any sop statements no excuse me okay so this are we can okay we have not implemented anything but here we are printing I don't want these values to go to my log file okay this is just for the credential as map I'm trying to print so okay I can have loged for here no it is loged info okay and here also log. info okay log. info okay I think we we should have started giving this uh log. info everywhere so but we have not given right so let's let's have that in the step definition okay so let's go to the top the first one is user navigation to the application right so for this St okay so user navigation to the application we can give uh let to say log. info user navigate to application user navig to application okay and uh when user click on sign up or login link right so homepage. click login link okay after that we can say uh log. info say user uh click on login link but let's say here are we writing those statements then it will be duplicated clicked on sign up loog okay so if you're saying that so here instead of saying here we can say here just say uh user trying to click on trying to click on log login link okay so here also you're saying here navigates okay open so here we can put so here user uh to navigator application okay so us Navigator the user Navigator to the application okay so just give some meaningful messages over here guys okay and here uh sign up login page should be displayed okay so now you can say log. info here give the message so verifying sign up to say sign up or login page verify sign up or login page okay and uh login page is displayed okay so similarly here you can say log. info log. info uh you can say user is closing the browser you can say uh trying to put the browser driver or something right so let's say uh double code trying to quit Okay so quit browser okay and user enters the username and password user you can say log. info log. info so Sor small log Okay small L log. info so you say user is entering user is entering username and password okay just give the meaningful messages like that okay and uh user clicks on the login button right so say user is clicking on the login button log. info okay so here you say user is clicking clicking on login button user is clicking on the login button okay user is clicking on login button okay Beauty and us is clicking on the login button okay and uh the application should navigate to the loged in user homepage okay so here uh log INF let's see sorry uh you can say log. info user is uh user verifying application is navigated right is navigated to navigated to where user homepage right user homepage user homepage hom page clicked on the log out link right so log. info let's say user is trying to click on log out link log out link okay so this is one other thing okay and here uh I'm not giving it that's okay user enters username and password user enters the username and password at the data table and here as list here as map both are same okay but I can give uh just one one like okay so log. info here we can say user is trying to enter credentials credentials okay here I can say uh using map using map and here I can say the same thing using list just copy that just for the console logging in okay doesn't really matter list okay so this is how we have uh updated our uh uh what is that uh step definition so what is the eror control s okay here okay I forgot the semicolons okay here one more okay fine all the errors are gone now okay so now the next one is uh page classes okay so we have already added to all the page classes home page so now uh okay so time to declare okay public driver driver and let's say private loger log so let's say private okay so logger I'll say log okay and capital L here okay log log and uh we have to uh initialize the log right with the dynamically capturing the class name so inside the Constructor only let's do that so you'll say log equals to so you will say log manager so log manager dot get logger right so get logger and here within that you can say right this dot get class do get name okay this is what you can see and uh moove this with log. info okay done then go to sign up or login page okay so here private web driver driver here did I make it public web driver this will be private okay sign up or login page and user home page these two places we need to implement so now here also private let's say log capital L so so logger log and uh inside the Constructor okay log equals to log manager okay dot get class sorry get logger and inside the get logger you'll say this dot G Class dog name okay and uh just uh replace system. print with log. info okay let me copy this one and we can replace the other s so statement so okay okay replace replace done so this is all done then user homepage okay so now here also private logger log and uh here log equals to let's say log manager dot get logger inside the logger you can say this dot get sorry okay so here inside the parenthesis let's say this do get class. get name okay so we are done and replace sop with log. inove okay I think we're good now okay so all the sign up or login pages is also done okay so all the pages are done so after we are done okay uh we can user homepage is also done okay so we done with everything so now the next step is let's run and see the log folder and the file in the console so you go to the log fer 2.xml and you say uh we have given the folder location is SL dot do means in the current directory just go and uh uh dot means The Parent Directory okay so go to the current this folder only inside that create a lock folder we don't have a lock folder over here right so in create a log folder and inside that app. log you should create okay so let's go and run okay M run so let's go and check our uh feature file okay login page okay user login feature so we have how many test cases one and two test cases are there okay so M and run let's run it and see so what unknown life cycle cycle at the smoke I think uh here I'm providing something edit configuration okay I don't have to give the tag okay clean test I just want to run everything so I'm saying CLE clean test okay just just [Music] run where exactly I'm missing user homepage okay my bad okay just run and see start a test script execution we'll wait for the execution to finish okay so entering valid username password logging in and log out okay fine Okay cool so all at Mo so let's see what is the runner configuration what we have given uh okay we can see the logs over here right okay fine so let's see log folder also got created but let's go to the configuration first and understand uh what is the Java test Runner okay so let's go to the runner and tax equals to smoke okay so let's go to your feature and see smoke which one is the smoke so one one is smoke okay so pass the test data in line okay you might be wondering why only that one got invoked because we're saying at the runner at the smoke so only smoke is there and if you see okay so let's understand your log forj 2.xml and what we have given and what we are seeing okay let me open this one here okay or I can show here only log4j2.xml okay so pattern pattern is personality HH that means hour so 13 hour and 59 uh minute and this is the second and this is the millisecond okay and this is coming from the main okay and info we're saying log. info right so this is giving the step definition. open login page type definition this is giving me the fully qualified name of that particular class from where it is getting invoked okay so user uh trying to navigate to the application this is the message so you have seen here is the message right so message is user is trying to log in Navigator the application this is nothing but your log info what we have given in the open login page type definition if you go to the open login page type definition the first one right so here us are trying to navigate to the application right and after that user navigated to the application navigated to the application here user navigated to the application okay so now what happened after this we went to this uh we don't have to give this one here because already for each action in the respective page class or the B steps we are giving those information right so now go to the B step. open URL so now the control is in this Bas step class and in this B Class we're seeing what is that uh here navigated to the application log. info so here you should see like it is coming from the Bas so how exactly you will see so here see automation utilities base steps navigated to the application okay and then again the control goes to the step definition and the step definition we're interacting with the homepage okay so clicked on the sign up or login link so similarly you can understand how the definition and how the respective page classes are getting called in the sequences over here okay so you can use your automation uh script okay you can use your log for implementation so let's go to the log folder and open this app. log and see the logs getting created and generated over here okay so if you see here also similarly how exactly it was there right similarly it is coming there so now uh trying to quit browser driver is called This is the first run this is the first Run related details and then when we run it one more time this is the second Run details 1403 if you see right 143 is the second one so here you can see this 1403 related uh console logs and the console L and file log also getting created and here if you see it will get appended for each execution it is not going to override so 1359 also there and 14304 also there okay so let me come commit the changes commit so let's say I'll say uh so these are all on staged right so we can stage them stage okay and uh okay so commit message is implemented Implement log for 2 okay commit so this the commit let's go to get let see Implement log for2 okay so everything is committed now so that's all to this class guys uh we'll meet in the next class if you guys have any questions or doubt please ping them in the put them in the comment I'll get back to you as soon as possible if you guys are really enjoying this session I would request you to subscribe to my channel and share this with your friend and colleagues so that they can get also get benefited out of it see you then bye-bye
Info
Channel: QA Automation Classes
Views: 320
Rating: undefined out of 5
Keywords: log4j in selenium, log4j, log4j tutorial in selenium, selenium webdriver, selenium log4j, log4j2 selenium, log4j tutorial in java, log4j tutorial, log4j in selenium webdriver, log4j jar for selenium webdriver, how to add log4j in selenium java project, log4j2 tutorial in java, log4j selenium, log4j beginner tutorial, apache log4j 2, selenium log4j fix, selenium tutorial, log4j with selenium, selenium webdriver console log, selenium what is log4j, log4j2, log4j cucumber
Id: -6svxPJf6Y0
Channel Id: undefined
Length: 32min 16sec (1936 seconds)
Published: Thu May 02 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.