Interface JavaScriptHandler

All Superinterfaces:
Serializable
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface JavaScriptHandler extends Serializable
Plays the browser for JavaScript that an application sends and that Browserless Test does not simulate on its own, such as reading a value that only the browser knows.

A handler is registered for the whole test environment, with BrowserlessTestConfig.javaScriptHandlers() or BrowserlessConfiguration.Builder.withJavaScriptHandlers(Class[]). Every UI the environment creates, also on a reload or in a new window, gets its own instance, created with the public no-argument constructor before the UI is initialized. The handler then sees each piece of JavaScript at the moment it is sent to the simulated browser, in order, also while a view is being navigated to, and answers it the way the browser would, so the application behaves as it does in a browser without the test doing anything:

 public class BrowserTimeZone implements JavaScriptHandler {
     @Override
     public void handle(ExecutedJavaScript.Execution execution) {
         if (execution.isWaitingForResult() && execution.getExpression()
                 .contains("resolvedOptions().timeZone")) {
             execution.resolve("Europe/Helsinki");
         }
     }
 }

 @BrowserlessTestConfig(javaScriptHandlers = BrowserTimeZone.class)
 class ScheduleViewTest extends BrowserlessTest {
 }
 

A handler is offered every piece of JavaScript and ignores what it does not simulate. ExecutedJavaScript.Execution.getUI() tells which UI the JavaScript was sent to. Handlers run after the simulations Browserless Test ships with, in the order they are registered. JavaScript that a handler makes the application schedule, from a result handler for example, is sent at the next client boundary, as with a browser. To check afterwards what was sent, use ExecutedJavaScript.capture() instead.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Reacts to a piece of JavaScript that the simulated browser has received.
  • Method Details

    • handle

      void handle(ExecutedJavaScript.Execution execution)
      Reacts to a piece of JavaScript that the simulated browser has received.
      Parameters:
      execution - the JavaScript the browser received, not null