This Tutorial will Discuss all-about Debugging Techniques in Eclipse for Java Selenium. You can learn to Add Breakpoints, Run Code in Debug Mode and Debugging Tips:
Debugging is one of the most important skills any automation tester should learn. It gives us the confidence to understand the code better.
It helps the tester identify any failure in the code, and it is also used to understand the code execution sequence while analyzing a framework.
What You Will Learn:
What Is Debugging?
In simple words, Debugging is the art of identifying and resolving an error in the developed source code by running the code in an interactive mode.
Imagine you have joined a project that already has an automation framework in place. Here, we can use the debugging skills to go through in a step by step manner and understand how the framework works, how the control goes to different utilities, test data, and report components, etc.
The following main steps are involved after we identify the source code where debugging needs to be done.
- Setting the Breakpoints.
- Running the source code in debug mode.
- Debugging the code and fixing error/updating the code.
- Stopping the debug mode execution.
We will discuss each of the above steps in detail:
Breakpoints can be added as follows:
#1) Keep the cursor on the line and click Ctrl+Shift+B. The dot at line number 31 & 35 in the screenshot below shows that a breakpoint has been added.
#2) Keep the focus on the line and double click on the line number.
#3) Select Run => Toggle Breakpoint.
#4) Keep the debug perspective mode open to view the values of Variables, Breakpoints (Select Window => Perspective => Open Perspective).
Running The Code In Debug Mode
Now the breakpoint has been set, we can run the code in debug mode.
This can be done in 2 ways as given below:
#1) Right-click the Java class and select Debug As => Java Application.
#2) Select the Debug Icon (There is a bug like icon on the toolbar).
#1) When we start debugging, the code executes automatically and stops at the first Breakpoint. In the screenshot below, the first breakpoint is added at line 27 and the second breakpoint at line 31. So, when we start the execution in debug mode, the code runs normally till line 26 and stops at 27.
In the console, we can see that the sysout given at line 26 is executed and the message is printed. The control reached the first breakpoint and is waiting for the user to take necessary action. That particular line where the control currently lies is highlighted.
In the above screenshot, we can see that there are two tabs open i.e. Variables and Breakpoints.
Breakpoint Tab: Shows the details of the lines where breakpoints have been added.
Variables Tab: This tab shows the value getting assigned to different variables in debug mode. We can see the change in values while the control moves from one line to another.
In the above screenshot, the control is at line 6 now i.e inside the method Add (int a, int b). We can see the values assigned to the variables a&b in the Variables section on the right side. Those values are 5 and 3 respectively.
Now press F6 and navigate to line 16. We can see that the variables a&b are assigned new values 10 and 2 (see below screenshot). i.e. We can validate the value that each variable holds in this section. Also, we can modify this value during debug mode to verify the results for another variable value.
F6 or Step Over Icon: This is for executing the code line by line in debug mode i.e. if a breakpoint is added to a method, then it will execute the code inside the method and control goes to the very next line after the method. (Select Run => Step Over)
F5 or Step Into: If a breakpoint is added to a method, F5 or Step Into will execute each line inside the method in debug mode and control goes to the very next line after the method.
Step Return: While we are inside the method in debug mode, clicking F6 will immediately bring the control to the next line after the method. Suppose we think of debugging the method again, maybe after changing some value inside the method, press Step Over. Then the control is brought back to the method again.
F8: From the line of the first breakpoint, the code executes line by line by pressing F5.
For example, we have completed the failure analysis at Breakpoint 1, press F8(or Resume button). Then the code executes in run mode and stops at the next breakpoint if any or when the code reaches the last line. This way, we can reduce the time it takes to complete the debugging process.
Skip Breakpoint: To skip the breakpoints during debug mode, Select Run => Skip All Breakpoints or Press Ctrl+Alt+B.
Stopping Debug Mode Execution
Debug mode execution can be forcefully stopped by selecting the Terminate button (red color square-shaped button in the toolbar) or the execution gets automatically stopped after executing the last line of the code.
Breakpoints can be removed in the following ways:
#1) Double-clicking on the line where the breakpoint has been added, removes the breakpoint.
#2) Right-click the source code line number and select Disable Breakpoint.
#3) To remove all breakpoints together, Select Run => Remove All Breakpoints.
Breakpoint Icon Meaning
#1) When a new breakpoint is added, the icon is a Blue colored ball. While in debug mode, a tick mark appears next to this blue ball icon. This means that the debug control will reach that line. If there is no tick mark during debugging, control will not reach those lines though the breakpoints have been set.
See line number 30 in the below image (right pane).
#2) After the breakpoint is added, open the Breakpoints tab under the Debug perspective and uncheck it (i.e. remove the tick mark). Then the breakpoint changes to a white ball icon. See line number 31 in the above image (right pane).
#3) Press Ctrl+Atlt+B/or Select Run => Skip All Breakpoints to skip all the added breakpoints.
#4) Breakpoint with a Question Mark: This is when there is a condition set for the breakpoint as shown in the below image. See breakpoint in line #37 (right pane).
#5) When the breakpoint is added at the Class level, then a green-colored c icon is displayed. (Line number 3 in the below image).
Select a breakpoint from the Breakpoint view in Eclipse and right-click => Select Breakpoint Properties.
As we can see in the above screenshot, the various properties include:
#1) Enabled Checkbox: This option is used to enable/disable the breakpoint.
#2) Trigger Point Checkbox: Once a trigger is set for any breakpoint, then all the other breakpoints will be suppressed and will be enabled only after one of the trigger points has been set.
Triggers added breakpoints will have a “T” icon added to it, and all the other initially suppressed breakpoints have “T” with a cut. Trigger points can be removed by right-clicking the Breakpoint view => Select Remove all Triggers.
#3) Hit Count Field: When a hit count is applied to a breakpoint, it suspends executing the nth time it is hit. Then for the breakpoint to execute, it should be initiated again or the hit count should be changed. There are two options under this i.e. Suspend thread & Suspend VM.
#4) Conditional Checkbox: A conditional breakpoint can be added so that the breakpoint execution is suspended in one of the below situations.
- When the result of the expression is true: Here the condition must be a Boolean expression. If you want the breakpoint to stop every time the condition evaluates to true, then select this option.
- When the result of the expression changes: If you want the breakpoint to stop only when the result of the condition changes, then select this option.
Breakpoints are stored in the editor (Eclipse, IntelliJ, etc.) and not in the source code, and it becomes active during the debugging session.
Types of Breakpoints are as follows:
(i) Line Breakpoint: It is set on an executable line in code.
(ii) Method Breakpoint: The execution is suspended on entering the method before any line of code is executed.
(iii) Conditional Breakpoint: This is set in such a situation when debugging if we want to suspend the execution while a certain condition is satisfied. It can be set in two ways (This can be set in the breakpoint Properties pop up).
- When a particular condition is set to true.
- When the value of the condition changes.
(iv) Exception Breakpoint: There may be a situation where the programmer gets error due to an exception while running the code. Identifying the exact condition where such exception error is thrown is a tedious job. In such situations, we can use, Exception Breakpoint.
This helps in identifying an exception while testing the code in debug mode, which would have resulted in code failure.
An exception can be set by selecting Eclipse editor Run => Add Java Exception Breakpoint or from Breakpoint Properties view => Exception breakpoint pop up opens up.
Use Step Filtering: While clicking F5 and doing debugging, the control gets moved to system libraries or JDK classes in between (E.g. System, String, etc.). This can be avoided if required by setting the Step Filtering option.
This can be set from Windows => Preferences => Java-Debug => Step Filtering.
Q #1) How to run Java in debug mode in Eclipse?
Answer: After setting the required breakpoints, click the debug icon from the IDE toolbar. This will start the code execution in Debug mode.
Q #2) What is Debugging in Eclipse?
Answer: It is a way of running the code to identify any error/bug in the source code. Please refer to the introduction section of this tutorial for details.
Q #3) What is Step into and Step Over in Eclipse?
Answer: Step into or F5 lets the control inside each line of the method one by one. Step Over or F6 will bring the control from within the method to the very next line outside the method.
Q #4) How to move to the next line while debugging in Eclipse?
Answer: When a breakpoint is added and execution is started in debug mode, the code runs in normal run mode until the first breakpoint is reached. It suspends the first breakpoint line.
Then we can do F6 to go line by line. Press F5 to go inside the line (For Example, if the breakpoint is added at a method, then control goes inside the method code). Press F8 to resume the execution in normal mode until the next breakpoint is reached if any of the code’s last line is reached.
Q #5) How to show Variables in Eclipse?
Answer: Highlight the variable and right click and Select Watch. Then the variable and its value will appear in the Expressions section.
Q #6) How to remove the Breakpoints?
It can be removed by:
- Double-clicking on the code line.
- Right-click the left editor pane and select Disable Breakpoint.
- Select the cross icon from the debugging perspective (Breakpoints view).
- Select Run from the Editor => Remove all Breakpoints.
In this tutorial, we have walked through on the significance of Debugging, Debugging Techniques, and how effectively debugging can be done. We have given the step by step details for anyone to try and master the debugging techniques.
- Debugging Flow: How to start the execution in debug mode, how breakpoints are added. How the Step Into, Step Over, Step Return options work, etc.
- Breakpoint Icon meaning: We have seen the meaning of the icon label for different types of breakpoints.
- Breakpoint Types: In this section, we learned about the various breakpoint types like Line Breakpoint, Method Breakpoint, Exception Breakpoint, etc.
We hope this in-depth tutorial helped you understand Selenium Debugging Techniques.