Module org.htmlunit

Class DebuggingWebConnection

java.lang.Object
org.htmlunit.util.WebConnectionWrapper
org.htmlunit.util.DebuggingWebConnection
All Implemented Interfaces:
AutoCloseable, WebConnection

public class DebuggingWebConnection extends WebConnectionWrapper
Wrapper around a "real" WebConnection that uses the wrapped connection to perform the actual work and saves all received responses in the temp directory with an overview page.

This may be useful during development to understand what is being browsed.

Example:
 final WebClient client = new WebClient();
 final WebConnection connection = new DebuggingWebConnection(client.getWebConnection(), "myTest");
 client.setWebConnection(connection);
 
In this example an overview page will be generated under the name myTest/index.html in the temp directory and all received responses will be saved into the myTest folder.

This class is only intended as a debugging aid during development.
Author:
Marc Guillemot, Ahmed Ashour, Ronald Brill
  • Constructor Details

    • DebuggingWebConnection

      public DebuggingWebConnection(WebConnection webConnection, String dirName) throws IOException
      Wraps a web connection to have a report generated of the received responses.
      Parameters:
      webConnection - the web connection that does the real work
      dirName - the name of the directory to create in the temp folder to save received responses; if this folder already exists, it will be deleted first
      Throws:
      IOException - if there is a problem writing the files
  • Method Details

    • getResponse

      public WebResponse getResponse(WebRequest request) throws IOException
      Calls the wrapped web connection and saves the received response. Submits a request and retrieves a response. The default behavior of this method is to return WebConnection.getResponse(WebRequest) on the wrapped connection object.
      Specified by:
      getResponse in interface WebConnection
      Overrides:
      getResponse in class WebConnectionWrapper
      Parameters:
      request - the request
      Returns:
      the response to the request defined by the specified request
      Throws:
      IOException - if an IO error occurs
    • uncompressJavaScript

      protected WebResponse uncompressJavaScript(WebResponse response)
      Tries to uncompress the JavaScript code in the provided response.
      Parameters:
      response - the response to uncompress
      Returns:
      a new response with uncompressed JavaScript code, or the original response if uncompression fails
    • addMark

      public void addMark(String mark) throws IOException
      Adds a mark that will be visible in the HTML result page generated by this class.
      Parameters:
      mark - the text of the mark
      Throws:
      IOException - if a problem occurs writing the file
    • saveResponse

      protected void saveResponse(WebResponse response, WebRequest request) throws IOException
      Saves the response content in the temp directory and adds it to the summary page.
      Parameters:
      response - the response to save
      request - the request used to obtain the response
      Throws:
      IOException - if a problem occurs writing the file
    • isUncompressJavaScript

      public boolean isUncompressJavaScript()
      Returns whether JavaScript responses should be uncompressed before saving.
      Returns:
      true if JavaScript responses should be uncompressed; default is true
    • setUncompressJavaScript

      public void setUncompressJavaScript(boolean decompress)
      Sets whether responses recognized as JavaScript should be uncompressed. Uncompressing is useful for debugging when the original script is minified to a single line, as it produces more readable output and more useful error messages.
      Parameters:
      decompress - true if JavaScript responses should be uncompressed