Learn How to DEBUG | Java IntelliJ | How To Master Debugging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody hope that you are all doing fine and that you are following our youtube channel today we will study how to master debugging the debugging process can be tiresome and also a lot of developers neglect the debugging process but in my opinion this is one of the most powerful tools in any developer arsenal we'll start with the agenda of today first we'll go over breakpoints then we'll add some watch and we'll talk about variables during the debug process we'll dive deep into the debugging process we'll study how to evaluate an expression how to change values during runtime and what are some common pitfalls that we should all avoid breakpoints are the basic building blocks of any debugging process i demonstrate the debugging process using intellij if you're using pycharm if you're writing in python it should be identical because it's the same company that developed both of those ids if you're using eclipse or any other id it should be fairly similar i'll start with a project that we have developed during our from zero to selenium selenium for beginner sessions if you haven't watched these videos please click on the link down below so as you can see here i have a test which should login to a gmail with a valid login in intellij whenever you want to add a breakpoint you just need to click next to the line number of course there is no meaning to add a breakpoint here so i can't do it you can you can see here that intellij doesn't allow that but if i'll press here now i have a breakpoint you see that now the line is painted in red and i have the red dot here if i want to remove it i just need to re-click it what do you think would happen if i'll click right here let's start by finding out we'll start by instead of run we'll choose debug now the software was executed and you can see here that we stopped on line number 21 because this is the first breakpoint that our code encountered so i'll stop the run and as you can see you can either put several break points let's say that i want to stop here and then i will want to jump to this line so everything in between would be executed with no break points and then i will stop at line 33. one important thing to remember that we can also add break points inside other methods here you can see the constructor of the gmail sign-in page if i'll head over to the constructor and i will add a breakpoint here so let's go back one second yeah so here i will stop at line 21 i will continue executing my code but when line 26 would be executed it will call the gmail sign-in page and since i've added the breakpoint here it will stop we'll see it in a second if you are not sure where are all of the breakpoints that you've added when they are located you can head over to run and here you have the view breakpoint you can also use the shortcut in my case it's mac and it's shift command f8 so you can see the list of all the breakpoints here you can add some you can remove some those are breakpoints that i had previously you can see that they are we have a hollow dot and here we have a full dot and you can see here that whenever i press the checkbox it became a whole circle let's remove this one you can see now it was removed and i'll remove all the previous ones so currently i have three breakpoints two of them are located here in the gmail sign-in page test and the other one is located in the gmail in page you can see also that that you can add some extra info to each of the breakpoints you can suspend if you can choose a specific thread regarding this breakpoint you can have some conditions you can log it and you can have all of the other stuff you won't go over all of this but i do encourage you to play around with that and to see if you if you encounter something new if you have succeeded in doing something cool with your break point please share it down in the comment section down below and we can discuss it together so now after we have our breakpoints now let's go over the debugging process now i have this test and i'll press the debug button and you can see here that the code has stopped right here currently i don't have any variable but i can also add some variable to watch you can see here that i have the result variable and you can see here that he didn't find any local variable named result i want to show you something i will just continue at the execution then we'll go back to it now i've stopped in the in line number 33 you can see here that we had this variable named result because we have those glasses here you can see that i've added a watch to this variable that way whenever you add a new watch you can see here by pressing the new watch and then you can see that i can add any variable that i want you can start following this variable during the code execution sometimes it will be defined sometimes its value might be changed so it's very useful to edit the other variables here are variables that are occurring during the execution those are automatic watches you can see here for instance that this represent the instance of this class the report is the extent report object that i declared in line 21 the spark is was declared on line 22 and the test was declared here in line 24. also i've initialized another object in line 26 and this is represented here and also the result this is the automatic result because it was defined here in line number 28 it's the same as this one so this is the variable sections by the way you can ask yourself why the username field is not defined here you can you can see here we cannot find the local variable username field in order to solve this mystery i will solve it for you guys let's go back to the gmail sign-in page and since we had we had this breakpoint here our code stopped in this class in line number 23 and you can see this class has a member called username field it was defined and then i skipped it and it was still here in the variable list by the way if you don't see the list you should look here maybe your debug window is unopened you need to open it here and also maybe it was pointed to the console so if you don't see it please click the debug now let's go to the core of this thing i'll rerun it now we've stopped here in line number 21. we have several options the first one is step over or f8 you can use it with a keyboard it's very useful whenever you debug your code to use the keyboard shortcut so step over will lead me to the next line step into if i'll step into it will go to the this declaration the extent report it will go inside the method or in this case the constructor that it's calling in whatever choosing for step into you can choose sometimes whenever you initialize an object the constructor is also initializing another object so if you want to force step into you can choose which one of them you want to force step into but we won't deal with that i think it's it's less useful step out is whenever you dive deep into the step into and you want to go out to the one that was calling this method you need to use the step out the drop frame is very useful but you should only use it for simple variables because if you're doing for instance a variable that deletes something from the database or you initialize an object and you change one of its fields the drop frame won't let you go back because it's something that cannot be undone so if you're doing like some calculation or using simple variables you can use the drop frame to go back and you can re-run it again and the run to courser is very useful because you can move your cursor you can see here now my cursor here is on line number 23 and if i'll click this one it will execute the code until it will reach my courser let's try to drop the frame now you can see that we have this is the this object the gmail sign-in test class the report was declared in line number 21 the spark on 22 now we're here on line number 23. let's try to drop the frame yeah and we got an error like i told you because we've initialized those variables another useful tool you can find here if you click resume program so everything will be executed until our code will bump in the next break point if it won't have any break one let's say i will remove that by the way you can remove and add breakpoints while you're executing you can see here it does work and it works great so you can do it if you have some problem in reaching a breakpoint because of something that was added after compilation you won't have this v you would have an x but you can see that i can add and remove as many as breakpoint as i'd like so if i'll click continue my code will be executed you don't see the actual code because it's been executed in the second screen and now i've stopped here so this is the continue let's try on stepping over now i'll step over i expect that the code will stop here in line number 36 so i'll step over and now we have this gmail sign-in page insert password let's say that this is the correct password i'll click step into and now i'm inside this method which is called insert password under the gmail sign in page and also you can see here that i have the selenium one two three four this is like a preview of the value that i have in the variable i can also mouse over it and i have this option you can see the type of the variable and everything and also i can see it here in my variable pane let's also step into if i'll step into here what do you think would happen because this one is a method of selenium it's not something that i've wrote so i'll step into this one and now i'm inside send keys of selenium you can also debug that so you can step over and you can go inside this loop and everything and if you want to go back so you'll step out of this one and we've got back to our insert password which is a method that we've wrote not something of selenium now i'll step out again and i'm here again in the gmail sign in tests and then we can step over and in the end if i don't want to step into or step over any other of those other line of lines of code i can just click resume program and since i don't have any other breakpoint everything will just continue executing and this test would fail because i've inserted the wrong password so we have a timeout because it's try to locate an element and it wasn't successful another useful tool that you have in intellij i found some similarities in stuff that you can find in eclipse or visual studio but i do believe that the evaluate expression of intellij is the best that you can find so let's try to execute our code again and you see i stepped over all of the lines now i'm in line number 28. i still haven't executed this and i still doesn't have the result in my hand so let's say that i want to execute this code without affecting the results i just want to let's say a sneak peek to see what what would happen if i will execute it so let's mark it and we can do one of two things here we can click here the evaluate expression we also have the options f8 in mac and we can right click on it and choose from this menu the evaluate expression now everything was copied and i need to click evaluate now i will get the result of this one the result is true by the way you can play with that just because i've copied this one it doesn't mean that i cannot do other stuff so if i'll click the dot i have all the options that i have in the gmail sign-in page let's try to choose this method the wait for password field and let's try to evaluate now we're trying to evaluate and you can see here that the method through an exception because we try to wait for something that wasn't really here we can have the message and everything but the cool thing about the evaluate expression that nothing was affected in my code so you can play with that and you can see what what is happening of course that if we'll go back to the original expression the insert username i will show you my second screen so you can see here that the this value was already inserted you can see here so if i'll try to run it again you can see that it inserted the value just again so if you're evaluating an expression and it does some kind of a print to the screen and you it does really happen so it in the memory it won't change anything because this value of the result wasn't even defined yet so here i can just refer to the insert username so how can i get out of this let's go to this method i will put a breakpoint here and just as a reminder you can see the screen has two of these so here i can see that this web element would be defined so it was defined you can see here it's still like this and what i can do is to get this variable that was defined here to click on it evaluate expression and since it's a web element i can ask it to clear and it was cleared now i can continue executing my code i will remove the breakpoint and now the code was executed and the result got a return of true value so evaluate expression is a very powerful tool i do encourage you to use it as much as you can another cool thing that you can do while executing your code let's go back for a second let's say that this line would be executed the result and i want to test what would happen if i'll get a false value and since we all know that this will return a true value so let's try and see what would happen so i will continue now i expect to stop at line number 29 you can see that the result value here is true if i'll mark this variable and click evaluate expression i can change the value because i'm now assigning by using the equality mark i can say this one equals false then i can evaluate and you can see that the real value was changed to false i've changed it during execution in that way i can test what would happen if this value was false because every time i ran the program everything was correct so now the assertion should fail let's go over the assertion and we can continue executing now you can see here that the insert username field has failed it was expected to be true but it was false by the way you can play with that as much as you would like let's try to do something even more sophisticated let's go to the insert text method which is under our base page i remind you again if you haven't watched the lectures of the from zero to selenium for beginners you can see find all of this code over there so i will stop here in the base page let's run it i will continue in order to get to this method now you can see here that i have my web element which represent this username field this one and then i have that text to insert the text to insert is from zero to selenium at gmail.com now let's say that i step over it i will send the keys now i'm trying to get the text after and the text after you can see here the text after is identical to this one so let's do some manipulation let's say that the text after equals almost the same one copy value and let's say it equals gmail.com il instead of com now though and also you see that i'm going to assign this value to this variable let's close it now you can see those two values are not identical and since i'm returning the comparison which equals between this one and this one i will get a false value so it now the result you can see here that the result is false just because i play with it it's very useful whenever you want to debug your code and while executing you want to change some values it will be very useful to use this one of course this test now will fail one of the main pitfalls that you have while executing a code is the weight fields you can see here that i've added the weight password field while i was writing this test because whenever i click something in gmail let's say let's go back can see here that the password field doesn't appear instantly so because i debugged the code correctly i came to the conclusion that i need wait for password field because i need this one to appear before i continue whenever i click this one i need the password field to appear if it doesn't appear yet i can't click on it so if i'll comment out this line of code and i will try let's do it without debug and i will try to run it you see here that no such element when i try to insert the password i got this arrow because there wasn't an element like that because i wanted to have the wait for password field so if i was debugging by the way and i had this breakpoint here no i have this one as well let's remove this breakpoint and i will continue now you can see here that my code has stopped and the password field was loaded of course that if i'll try to step over it now see it was successful i haven't got this exception what i'm trying to say that a lot of times especially when not including weights some tests would pass while debugging and then you'll you'll be happy you will say that your test is perfect and nothing would fail in because you've tested all the fields and then you'll find out that in the real world whenever you execute it and there are some let's say network issues and the fields are taking more time to load your test would fail so this is the most common pitfall when using debug you should always execute your code without any breakpoints after that another pitfall is that you can change some values especially whenever you saw before that i've changed the value using a valid expression sometimes you change values that are coming from let's say an api and you can put another value you can delete a value and also whenever you try to read or write to a database whenever you use this evaluate expression and you use something that is external to your program you might change it as well so you should be careful whenever using the evaluate expression and changing values while running that's all for today guys i hope that you've enjoyed i really enjoyed making this lecture for you if you haven't subscribed to our channel please do it now and also please share your thoughts if you have any suggestion or you want to ask any kind of question please do it in the comments section down below and we'll meet again soon
Info
Channel: Automation by Rain the Dog
Views: 12,883
Rating: undefined out of 5
Keywords: debugger tutorial, debugger, how to use a debugger, using a debugger, debugger guide, debugging, debugging python, code debugging, debug, what is debugging, how to debug, debug intellij, debug java, debugging java, intellij, debug pycharm, debugging pycharm, greenlearners, green learner, debugging in software engineering, intellij idea debug, debugging in intellij tutorial, debugging in intellij shortcuts, debugging in intellij for begginers, intellij debugger tutorial
Id: HBlKsk5npWY
Channel Id: undefined
Length: 27min 59sec (1679 seconds)
Published: Mon Nov 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.