WebClientUtils.java 3.1 KB
package pl.labno.bernard.htmlunified;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;

import java.util.HashMap;
import java.util.Map;

public class WebClientUtils {

    private static long defaultCheckInterval = 500;
    private static long defaultTimeout = 10000;

    public static long getDefaultCheckInterval() {
        return defaultCheckInterval;
    }

    public static void setDefaultCheckInterval(long defaultCheckInterval) {
        WebClientUtils.defaultCheckInterval = defaultCheckInterval;
    }

    public static long getDefaultTimeout() {
        return defaultTimeout;
    }

    public static void setDefaultTimeout(long defaultTimeout) {
        WebClientUtils.defaultTimeout = defaultTimeout;
    }

    public static int waitForJSJob(WebClient webClient) {
        return waitForJSJob(webClient, webClient.waitForBackgroundJavaScript(10) - 1, defaultCheckInterval, defaultTimeout);
    }

    public static int waitForJSJob(WebClient webClient, int initialJobCount) {
        return waitForJSJob(webClient, initialJobCount, defaultCheckInterval, defaultTimeout);
    }

    public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout) {
        return waitForJSJob(webClient, initialJobCount, defaultCheckInterval, timeout);
    }

    public static int waitForJSJob(WebClient webClient, int initialJobCount, long checkInterval, long timeout) {
        int jobs;
        long startTime = System.currentTimeMillis();
        do {
            jobs = webClient.waitForBackgroundJavaScript(checkInterval);
            if (startTime + timeout < System.currentTimeMillis()) {
                throw new RuntimeException("Number of JavaScript jobs doesn't drop to initial level for 10000 seconds. It's memory leak in your JavaScript rather then request taking so long!");
            }
        } while (jobs > initialJobCount);
        System.out.println("Waiting took: " + (System.currentTimeMillis() - startTime) + "ms");
        return jobs;
    }

    /**
     * Returns list of suggestions from rich:suggestionBox
     *
     * @param suggestion suggestionBox element
     * @param column     column of suggestionBox to extract text from
     * @return list of suggestions
     */
    public static Map<String, HtmlTableCell> getSuggestions(HtmlElement suggestion, int column) {
        final Map<String, HtmlTableCell> suggestions = new HashMap<String, HtmlTableCell>();
        final HtmlElement suggestElement = suggestion.getElementById(suggestion.getId() + ":suggest");
        @SuppressWarnings("unchecked")
        final DomNodeList<HtmlElement> suggestionRows = suggestElement.getElementsByTagName("tr");
        for (HtmlElement row : suggestionRows) {
            @SuppressWarnings("unchecked")
            final DomNodeList<HtmlElement> cells = row.getElementsByTagName("td");
            final HtmlTableCell cell = (HtmlTableCell) cells.get(column + 1);
            suggestions.put(cell.asText(), cell);
        }
        return suggestions;
    }
}