Sunday, January 29, 2017

Troubleshooting and Debugging WebDriver Tests


Below steps would help in troubleshooting , identifying and finding the root cause of test failure.

1.       Using screenshots:

Adding code to testcase  to take screenshots before and after an action/statement. The screenshots taken can be used to analyse the state of the page before and after the action that requires troubleshooting.
2.       Running the test case in debug mode
·       The test needs to be run by using breakpoints at the line which throws the exception.
·       When the code stops executing at the breakpoint. Check that the application is in the correct state i.e. all elements are ready, you can see the element you want to work with etc.
·       Then step over the line that throws the execption.
·       If it passes then chances are you need some synchronisation code immediately prior to your statement to wait for the element to be ready. Because that is what you did when you debugged: you waited, then you executed the code. You want to add the wait into your code.

3.       Using Conditional breakpoint

Conditional breakpoints can be set by using the breakpoints view in Eclipse.  Select breakpoint properties by right-clicking on the break point. Check the conditional checkbox and write an expression that evaluates to true and the program  should get suspended during Debugging.

4.       Changing Variable values in Debug perspective

The values of variables in the tests can be changed on the fly using the Debug perspective. The top right corner of Eclipse should have the variables view from which you can select a variable name and change its value.

5.       Display View

 The Display View displays the result of evaluating an expression in the context of the current stack frame. You can evaluate and display a selection either from the editor or directly from the Display View.
You can view the Display view as a place where you can inspect all sorts of variables and boolean expressions during runtime. While your debugger is frozen on a breakpoint start typing the name of an object variable for instance and you'll get autocomplete functionality as you start calling methods or fields to reach deeper class datastructures. Then when you select/mark the portion you need to inspect or everything, the buttons on the Display view will be clickable. You can always perform the Ctrl+Shift+I shortcut on the selection to view what's the current runtime state of your selection i.e. variable, object, boolean expression etc.
Taken from: http://stackoverflow.com/questions/4499944/how-to-use-eclipses-display-view-for-debug

6.       Capturing HTML DOM Content

A technique is to capture the HTML DOM contents when a test fails. Capture the contents of the DOM and write them out to a file.
      
If you want to get page source you can get by using the following java code:

String pageSource = driver.getPageSource();
If you want to verify a particular text is present or not on the page, do as below:

boolean b = driver.getPageSource().contains("your text");

No comments:

Post a Comment