- protected CompletionStage<Object> evaluate(String js, Object... args) {
- CompletableFuture<Object> res = pageLoaded.thenApply((ready) -> {
- if (!ready)
- throw new IllegalStateException("Component is not initialised.");
- Object result = browser.evaluate(String.format(Locale.ROOT, js, args));
- return result;
- });
- return res.minimalCompletionStage();
+ protected Object evaluate(String js, Object... args) {
+ assert browser.getDisplay().equals(Display.findDisplay(Thread.currentThread())) : "Not the proper UI thread.";
+ if (!readyStage.isDone())
+ throw new IllegalStateException("Methods returning a result can only be called after UI initilaisation.");
+ // wait for the context to be ready
+// boolean ready = readyStage.join();
+// if (!ready)
+// throw new IllegalStateException("Component is not initialised.");
+ Object result = browser.evaluate(String.format(Locale.ROOT, js, args));
+ return result;
+ }
+
+ protected void execute(String js, Object... args) {
+ if (readyStage.isDone()) {
+ boolean success = browser.execute(String.format(Locale.ROOT, js, args));
+ if (!success)
+ throw new RuntimeException("JavaScript execution failed.");
+ } else {
+ Supplier<Boolean> toDo = () -> {
+ boolean success = browser.execute(String.format(Locale.ROOT, js, args));
+ return success;
+ };
+ preReadyToDos.add(toDo);
+ }