Class ExecutedJavaScript

java.lang.Object
com.vaadin.browserless.ExecutedJavaScript
All Implemented Interfaces:
Serializable

public final class ExecutedJavaScript extends Object implements Serializable
The JavaScript the server has sent to the browser of a UI during a browserless test.

There is no browser, so JavaScript scheduled with executeJs, callJsFunction or a declared JavaScript definition does not run. Browserless Test plays the client instead: at every client boundary - a round trip, a component lookup, the end of a simulated user interaction - it takes the pending JavaScript of the UI, the way a server response would, and simulates what it can (server-side Focusable.focus() and blur(), for example).

A test that wants to check what was sent starts a capture with capture(), runs the steps to check, however many round trips they take, and then queries the capture:

 ExecutedJavaScript executed = ExecutedJavaScript.capture();
 test(save).click();

 assertEquals(List.of(List.of("Saved")),
         executed.getFunctionCalls(dialog, "open"));
 

Each UI created by Browserless Test also has a capture that was started with the UI, before its first view was navigated to, so it holds everything the UI was sent. The test classes and BrowserlessUIContext return it from getExecutedJavaScript(). Everyone who gets it shares the same instance, so it is read-only: no one can stop it, clear it or take anything out of it, and the lists its queries return, like those of any capture, cannot be modified. Answering a call with ExecutedJavaScript.Execution.resolve(Object) or ExecutedJavaScript.Execution.reject(String) is visible to everyone who looks at that call, as it is the browser's answer to the application.

A capture holds everything sent to the browser of its UI after it was started or last cleared; JavaScript still pending when it starts is sent first and is not part of it. A capture runs until stop() or for as long as the UI lives. Querying it does not consume anything, and several captures of the same UI each see everything sent while they are active. A query sends whatever is still pending first, so JavaScript scheduled just before is included.

A test that plays the browser step by step can call clear() after reading a step, so that the next query only sees what is sent afterwards:

 List<List<Object>> pages = executed.getFunctionCalls(viewer, "scrollToPage");
 executed.clear();
 

A call whose result the application waits for, with then(...) or toCompletableFuture(...), can be answered with ExecutedJavaScript.Execution.resolve(Object) or ExecutedJavaScript.Execution.reject(String), as the browser would. A ReturnChannelRegistration passed to the JavaScript can be called with ExecutedJavaScript.Execution.invokeReturnChannel(Object...), as the JavaScript would call it in the browser.

JavaScript scheduled for an element that is not visible is not sent, as in a browser, and is captured only once the element becomes visible.

See Also:
  • Method Details

    • capture

      public static ExecutedJavaScript capture()
      Starts capturing the JavaScript sent to the browser of UI.getCurrent().
      Returns:
      the capture, not null
      Throws:
      IllegalStateException - if there is no current UI
      See Also:
    • capture

      public static ExecutedJavaScript capture(UI ui)
      Starts capturing the JavaScript sent to the browser of the given UI. JavaScript that is still pending is sent first and is not captured.
      Parameters:
      ui - the UI, not null
      Returns:
      the capture, not null
    • stop

      public void stop()
      Stops capturing, after first sending whatever is still pending. What was captured so far can still be queried. Stopping a capture that is already stopped does nothing.
      Throws:
      IllegalStateException - if this is the read-only capture that a UI got when it was created, which runs for as long as the UI lives
    • clear

      public void clear()
      Forgets everything captured so far, so that the queries only see what is sent to the browser afterwards. JavaScript that is still pending is not sent first, so JavaScript scheduled just before, for example by a handler that ExecutedJavaScript.Execution.resolve(Object) ran, is seen by the next query. Executions that were already returned can still be answered. Clearing does not stop the capture.
      Throws:
      IllegalStateException - if this is the capture that a UI got when it was created, which is shared and cannot be cleared
    • getExecutions

      public List<ExecutedJavaScript.Execution> getExecutions()
      Gets everything sent to the browser since the capture started or was last cleared, and until it was stopped, in the order the browser received it, whichever element it was run on; getExecutions(Component) and getExecutions(Element) keep only what was run on one element, and getExecutions(Class, Consumer) only the calls of one method of a JavaScript definition.
      Returns:
      the sent JavaScript, not null
    • getExecutions

      public List<ExecutedJavaScript.Execution> getExecutions(Component component)
      Gets what was sent to the browser for the given component's element since the capture started or was last cleared, and until it was stopped, in the order the browser received it. Unlike getExecutions(), JavaScript run on other elements is left out. JavaScript run on the page belongs to the UI, so passing the UI gets it.
      Parameters:
      component - the component whose element the JavaScript was run on, not null
      Returns:
      the JavaScript sent for the component, not null
      See Also:
    • getExecutions

      public List<ExecutedJavaScript.Execution> getExecutions(Element element)
      Gets what was sent to the browser for the given element since the capture started or was last cleared, and until it was stopped, in the order the browser received it. Unlike getExecutions(), JavaScript run on other elements is left out. JavaScript run on the page belongs to the element of the UI.
      Parameters:
      element - the element the JavaScript was run on, not null
      Returns:
      the JavaScript sent for the element, not null
      See Also:
    • getExecutions

      public <T extends JsInvokers> List<ExecutedJavaScript.Execution> getExecutions(Class<T> definitionType, Consumer<T> method)
      Gets what was sent to the browser for the calls of one method of a JavaScript definition, the interface that Element.executeJs(Class) is called with, in the order the browser received it. Each execution has the call itself (ExecutedJavaScript.Execution.getJsCall()) and can be answered the way the browser would, with ExecutedJavaScript.Execution.resolve(Object) or ExecutedJavaScript.Execution.reject(String), for a call whose result the application waits for. Unlike getExecutions(), everything else is left out. The method is named by calling it, the way the application calls it, so that renaming it updates the test and a method that the definition does not have fails to compile:
       executed.getExecutions(WakeLockJs.class, WakeLockJs::request).get(0)
               .resolve(null);
       

      The call only names the method: it is not sent, and its arguments are ignored, so any value of the right type will do. Calls are matched by the name of the method, so overloads of one name are not told apart.

      Type Parameters:
      T - the JavaScript definition type
      Parameters:
      definitionType - the JavaScript definition, not null
      method - calls the method of the definition to get the executions of, once, on the implementation it is given, not null
      Returns:
      the executions of the calls, not null
      Throws:
      IllegalArgumentException - if the type is not a JavaScript definition, or if method does not call exactly one method of it
      See Also:
    • getCalls

      public <T extends JsInvokers> List<JsCall> getCalls(Class<T> definitionType, Consumer<T> method)
      Gets the calls of one method of a JavaScript definition, the interface that Element.executeJs(Class) is called with, that were sent to the browser, in the order the browser received them. To answer a call, use getExecutions(Class, Consumer) instead. The method is named by calling it, the way the application calls it, so that renaming it updates the test and a method that the definition does not have fails to compile:
       executed.getCalls(WakeLockJs.class, WakeLockJs::request);
       executed.getCalls(ScrollJs.class, js -> js.scrollTo(0, 0));
       

      The call only names the method: it is not sent, and its arguments are ignored, so any value of the right type will do. Calls are matched by the name of the method, so overloads of one name are not told apart.

      Type Parameters:
      T - the JavaScript definition type
      Parameters:
      definitionType - the JavaScript definition, not null
      method - calls the method of the definition to get the calls of, once, on the implementation it is given, not null
      Returns:
      the calls, not null
      Throws:
      IllegalArgumentException - if the type is not a JavaScript definition, or if method does not call exactly one method of it
      See Also:
    • getFunctionCalls

      public List<List<Object>> getFunctionCalls(Component component, String functionName)
      Gets the arguments of each call of the named client-side function on the given component's element, made with Element.callJsFunction(String, Object...), in the order the browser received them.
      Parameters:
      component - the component whose element the function was called on, not null
      functionName - the name of the function as it was passed to callJsFunction, not null
      Returns:
      the arguments of each call, not null