Selenium WebDriver- Debugging Code (Breakpoints) – Java Debugging with Eclipse

Selenium WebDriver- Debugging Code (Breakpoints) – Java Debugging with Eclipse

Most of us interested to know, how the code is getting executed. Some times, even if we write the code with out having any syntax errors also during execution we face issues which requires debugging of the code.
As Web-driver script executes fast, if we want to execute the Web-driver code line-by-line and verify the Output can be achieved using the Debug Mode in Eclipse.
Lets start. I have written a very simple example in Selenium Webdriver to retrieve the text in Facebook as highlighted in the below image.



FACEBOOK HOMEPAGE

There are no syntax errors in the code. Please try to execute the code as shown below:
Output:
Actual is not equal to Expected TC – Fail
In the above scenario the TC should pass. But we are getting response as TC-Fail.
So, how do we identify the issue. What is the reason for failure.
We have to use Debug mode to identify the root-cause for this issue.
Debug – Step1- Insert BreakPoint
Right-click at the line number 10 and select the first option – ‘Toggle Breakpoint’. It means we are inserting the Breakpoint, so we can verify the values in the variable at run time.
In our case we can verify the value stored in the ‘actual’.
ECLIPSE IDE
Step2-Run Facebook.java in Debug mode
DEBUGGING
Step3- Debug Perspective opens as shown below
Select the checkbox-‘Remember by decision’ and click on ‘Yes’ button.
DEBUG PERSPECTIVE
The Debug perspective opens, it has three tabs Variables,Breakpoints,Expressions.
DEBUG PERSPECTIVE
Out of three tabs- Expressions tab is required to view the values at run-time.In Debug perspective if you are unable to identify this tab. You to follow the steps as show below:
DEBUG PERSPECTIVE
Navigate to Expression tab and click on ‘Add new expression’ enter ‘actual’ as shown below:
DEBUG PERSPECTIVE
Click on Step-Over to move to next step. Verify the ‘actual’ value in the Expression tab.Also add few more expressions as shown below:
Actual String length-> actual.length()
Expected String length()->expected.length()

Click on Step-Over to move to next step. Verify all the values as shown below:
DEBUG PERSPECITVE
Conclusion
The length of expected String is 23.
The length of actual String is 22.

So there is an issue in expected string. In the program we provided the String expected =” See photos and updates”;
There is an unnecessary space added in the string. It should be changed to String expected =”See photos and updates”;
by removing the unwanted spaces.
Once identify the issue click on terminate button as shown below.
TERMINATE DEBUG MODE
Change the Perspective from Debug Mode to Java Mode.
DEBUG PERSPECTIVE
Remove all the breakpoints.
DEBUG PERSPECTIVE




3 comments on “Selenium WebDriver- Debugging Code (Breakpoints) – Java Debugging with Eclipse

    1. TOTAL-QA Post author

      Using the trim() function in String class we can ignore leading and trailing spaces of the string.
      Please modify the code accordingly.

      if(actual.trim().equals(expected)) {
      System.out.println("Actual is equal to Expected TC - Pass");
      }
      else
      System.out.println("Actual is not equal to Expected TC - Fail");

      Reply
  1. sathya

    if(!actual.trim().equals(expected)) {
    System.out.println(“Actual is equal to Expected TC – Pass”);
    }
    else
    System.out.println(“Actual is not equal to Expected TC – Fail”);

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *