Module org.testfx

Class DebugUtils


  • public final class DebugUtils
    extends Object
    Utility class for displaying additional info, running code, or capturing an image of a test whenever a test fails using FxAssert.verifyThat(Node, Matcher) or its related methods.

    It is possible to create completely customized error messages by chaining functions together using compose(Function[]) or one can use the standard provided handlers such as informedErrorMessage(FxRobot), informedErrorMessage(FxRobot, String), etc. All indent parameters in methods are used to specify the spacing to insert in order to offset values. The default indent value is three spaces (e.g. " ").

    When a test fails, an image of the screen at the time of failure can be captured and saved to a PNG file. DebugUtils provides support for a number of image sizes: the full screen, a window, an area of the screen (i.e. bounds), or an individual node. Use the convenience methods prefixed by "save" (e.g. saveScreenshot(String)) to capture and save images. The image path will be printed using insertContent(String, Object) and will appear in the error message. For your own custom handler, use saveTestImage(Function, Supplier, String), one of the "capture"-prefixed methods, and a Supplier<Path> that determines where to save the image to.

    This class uses the concept of combining functions together in order to add additional information to the error message of an AssertionError thrown by a failing test.

    Example:

    
     // Template:
     compose(
          // insert a header for the section of error information
          insertHeader(headerText),
          // insert the specific content that falls under this header
          insertContent(contentHeading, content),
          insertContent(contentHeading2, contentIterable),
          insertContent(contentHeading3, contentArray),
          insertContent(contentHeading4, contentStream),
    
          // insert a header for the section of error information
          insertHeader(headerText),
          // insert the specific content that falls under this header
          insertContent(contentHeading5, contentStream2),
          insertContent(contentHeading6, contentStream3));
    
     // Example:
     compose(
          insertHeader("Context:"),
          showKeysPressedAtTestFailure(this),
          showMouseButtonsPressedAtTestFailure(this),
          showFiredEvents(),
          saveScreenshot());
    
     // is equivalent to:
    
     informedErrorMessage(this);
    
     // as it uses all of the default arguments.