What is Debugging ?
While developing Selenium Automation Scripts using Java, you may encounter few situations where your developed automation script is either throwing errors or is not working as required. Now you want to go through the source code to identify the line which caused this problem. The process of locating and fixing the problems in the developed code is called Debugging.
How to Debug ?
We have to specify breakpoints in the code. Starting from the break point in the code, we can execute the code step by step to find out the line in the developed code which caused the problem. Suppose if you want to execute the complete developed code step by step, then you have to set the break point in the first line of the code. But if you want to debug the code from 10th line, then you have to set the break point at 10th line of the code. Till 9th line, the program will execute normally, but from 10th line the program can be executed step by step based on our choice.
Lets implement this on Eclipse IDE -
1. Launch Eclipse IDE, Create a new Java Project 'Project 012' as shown below -
2. Create a Class 'Class012' with the Java code as shown below -
3. Now lets debug the code to find out how the code is executed step by step in Eclipse IDE. Please follow the below steps to debug.
4. Select 'Open Perspective' option on Eclipse IDE as shown below -
5. Ensure 'Open Perspective' dialog is displayed , select 'Debug' option and click on 'OK' button as shown below -
6. Observe that the debug perspective is displayed in Eclipse IDE as shown below -
7. View the 'Breakpoints' tab in debug perspective and observe that there are no breakpoints available as shown below -
8. I would like to set a break point a line before the while( ) loop, to debug the code 'step by step' from while( ) loop in the code. To set a break point, right click on the small left margin and select 'Toggle Breakpoint' option as shown below -
9. Observe that the break point is set and displayed on the left small margin as shown below -
10. Now view the 'Breakpoints' tab and observe that the break point set in the above step is displayed as shown below -
11. Now view the Variables tab and observe that nothing is displayed in the tab as shown below -
12. Select 'Debug As' -> 'Java Application' from 'Run' Menu as shown below -
13. Observe that the code gets executed till the line before break point that we have set in the above steps and observe that the current line of execution is displayed in green color as shown below. And also the args variable of main( ) method is displayed in the variables tab as it is executed as shown below -
14. Now press 'F6' key on your keyboard to execute the line which is displayed in green color and observe that the line in green color got executed , the value of i got initialized to 1 as shown below and the line with while( ) look is now displayed in green color as shown below -
15. Again press 'F6' to execute the green color line i.e. 'while( )' loop statement and observe that the first statement in while loop is now in green color as shown below -
16. Again press 'F6' to execute the green color line i.e. println statement and observe that the print statement got executed and the value of 1 is displayed in the Console as shown below -
17. Again Press 'F6' and observe that the line in green color i.e. i++; got executed and the value of i got incremented by 1 in variables tab as shown below -
18. The first iteration of while loop is completed and now the condition in the while loop need to verified to perform the second iteration, hence the while( ) loop statement got displayed in green color as shown below -
19. In Debug perspective, you have pressed 'F6' key on your keyboard to execute the statements step by step and found how the statements execution order. Now assume that we are doing debugging the code and execute the remaining part of the code at a go. In order to execute the remaining part of execution in a single go, we have press 'F8' key on your keyboard. Press 'F8' key on your keyboard and observe that the output of the program got displayed in the console as shown below -
20. So using this method of debugging, you can find out and fix the problems in your program code by executing the statements in your program step by step. Now lets move back to the old Java Perspective from Debug perspective by following the below steps.
21. Select 'Open Perspective' option as shown below -
22. Ensure that 'Open Perspective' dialog is displayed , select 'Java' option from the dialog and click on 'OK' button as shown below -
23. Observe that the same old Java Perspective is displayed as shown below -
Please comment below to feedback or ask questions.
No comments:
Post a Comment