0

How do I propagate the listeners on the next loaded page using UI4J?

https://github.com/ui4j/ui4j

For example, The below will navigate to Google and attach to all input tags a click listener, but I would like to have the binds reattached after searching and on every page loaded on the "input" tags.

import com.ui4j.api.browser.BrowserEngine;
import com.ui4j.api.browser.BrowserFactory;
import com.ui4j.api.browser.Page;
import com.ui4j.api.dom.Element;

public class AutoWeb {
    public static void main(String[] args) {
        // get the instance of the webkit
        BrowserEngine browser = BrowserFactory.getWebKit();

        // navigate to Google
        Page page = browser.navigate("http://www.google.com");

        // show the browser page
        page.show();

        // bind the "input" tags
        // This should apply for all "input" tags on any page.
        for (Element element: page.getWindow().getDocument().queryAll("input")) {
            element.bindClick((handler) -> {
                System.out.println("Clicked!");
            });
        }
    }
}

If I use an Interceptor, how do I access the Page Document from the afterLoad() method in order to re-apply the bindings?

Interceptor interceptor = new Interceptor() {

    @Override
    public void beforeLoad(Request request) {
    }

    @Override
    public void afterLoad(Response response) {
        // Apply the bindings on after load.
    }
};

PageConfiguration pageConfiguration = new PageConfiguration(interceptor);

Page page = browser.navigate("http://www.google.com", pageConfiguration);
JasonTolotta
  • 150
  • 2
  • 11
  • I found bindClick(eventHandler) after an event and the ability to add an interceptor to a page configuration. That should do it. – Andrew Scott Evans Jul 15 '16 at 18:51
  • After that try page.getDocument().bind("click",handler); – Andrew Scott Evans Jul 15 '16 at 19:08
  • `page.getDocument().bind("click",handler);` I'll try this. – JasonTolotta Jul 20 '16 at 01:08
  • @AndrewScottEvans I attempted to use the interceptor, but I am hard pressed to figure out how to access the DOM Document from inside the `afterLoad()` method. – JasonTolotta Jul 20 '16 at 02:31
  • I am assuming that it is impossible somehow to bind after the action to load in this case? Even in the interceptor, it should be possible to just access the page as it is a global resource. Have you tried making the interceptor an inner class with a global page variable and accessing the page? I know it is against most coding conventions but seems to be the solution. You could also update a map with current or expected urls attached to pages for multiple loads. – Andrew Scott Evans Jul 20 '16 at 16:08
  • @AndrewScottEvans I have given up on attempting to fight with UI4J. Thanks for the suggestions, I will leave this question up for future reference. I may come back to it later. – JasonTolotta Jul 27 '16 at 07:26
  • If looking for a decent testing environment HtmlUnit is always popular. There is also selenium, just don't start more than one without placing each in a container via docker (perhaps mesos) as it leaks like all hell. – Andrew Scott Evans Jul 27 '16 at 16:15

0 Answers0