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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Facebook { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("https://www.Facebook.com"); String actual = driver.findElement(By.xpath("//span[text()=' See photos and updates ']")).getText(); System.out.println(actual); String expected = " See photos and updates"; if(actual.equals(expected)) { System.out.println("Actual is equal to Expected TC - Pass"); } else System.out.println("Actual is not equal to Expected TC - Fail"); } } |
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’.
Step2-Run Facebook.java in Debug mode
Step3- Debug Perspective opens as shown below
Select the checkbox-‘Remember by decision’ and click on ‘Yes’ button.
The Debug perspective opens, it has three tabs Variables,Breakpoints,Expressions.
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:
Navigate to Expression tab and click on ‘Add new expression’ enter ‘actual’ as shown below:
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:
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.
Change the Perspective from Debug Mode to Java Mode.
Remove all the breakpoints.
After I debug and followed all the above steps I am still seeing the script is failing
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");
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”);