Class ExecutedJavaScript
- All Implemented Interfaces:
Serializable
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classOne piece of JavaScript sent to the browser. -
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutedJavaScriptcapture()Starts capturing the JavaScript sent to the browser ofUI.getCurrent().static ExecutedJavaScriptStarts capturing the JavaScript sent to the browser of the given UI.voidclear()Forgets everything captured so far, so that the queries only see what is sent to the browser afterwards.Gets the calls of one method of a JavaScript definition, the interface thatElement.executeJs(Class)is called with, that were sent to the browser, in the order the browser received them.<T extends JsInvokers>
List<JsCall> Gets the calls of one method of a JavaScript definition, the interface thatElement.executeJs(Class)is called with, that were sent to the browser, in the order the browser received them.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)andgetExecutions(Element)keep only what was run on one element.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.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.getFunctionCalls(Component component, String functionName) Gets the arguments of each call of the named client-side function on the given component's element, made withElement.callJsFunction(String, Object...), in the order the browser received them.voidstop()Stops capturing, after first sending whatever is still pending.
-
Method Details
-
capture
Starts capturing the JavaScript sent to the browser ofUI.getCurrent().- Returns:
- the capture, not null
- Throws:
IllegalStateException- if there is no current UI- See Also:
-
capture
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 thatExecutedJavaScript.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
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)andgetExecutions(Element)keep only what was run on one element.- Returns:
- the sent JavaScript, not null
-
getExecutions
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. UnlikegetExecutions(), 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
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. UnlikegetExecutions(), 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:
-
getCalls
Gets the calls of one method of a JavaScript definition, the interface thatElement.executeJs(Class)is called with, that were sent to the browser, in the order the browser received them.The method is named as a string, so a misspelled or renamed name matches nothing rather than failing;
getCalls(Class, Consumer)names it by calling it, which the compiler checks.- Parameters:
definitionType- the JavaScript definition, not nullmethodName- the name of the called method, not null- Returns:
- the calls, not null
- See Also:
-
getCalls
Gets the calls of one method of a JavaScript definition, the interface thatElement.executeJs(Class)is called with, that were sent to the browser, in the order the browser received them. 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 nullmethod- 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 ifmethoddoes not call exactly one method of it- See Also:
-
getFunctionCalls
Gets the arguments of each call of the named client-side function on the given component's element, made withElement.callJsFunction(String, Object...), in the order the browser received them.- Parameters:
component- the component whose element the function was called on, not nullfunctionName- the name of the function as it was passed tocallJsFunction, not null- Returns:
- the arguments of each call, not null
-