Blame view

src/main/java/pl/labno/bernard/htmlunified/WebClientUtils.java 3.56 KB
bernard authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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) {
bernard authored
33
        return waitForJSJob(webClient, webClient.waitForBackgroundJavaScript(10) - 1);
bernard authored
34 35 36
    }

    public static int waitForJSJob(WebClient webClient, int initialJobCount) {
bernard authored
37
        return waitForJSJob(webClient, initialJobCount, defaultTimeout);
bernard authored
38 39 40
    }

    public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout) {
bernard authored
41
        return waitForJSJob(webClient, initialJobCount, timeout, defaultCheckInterval);
bernard authored
42 43
    }
bernard authored
44
    public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout, long checkInterval) {
bernard authored
45 46 47 48 49
        int jobs;
        long startTime = System.currentTimeMillis();
        do {
            jobs = webClient.waitForBackgroundJavaScript(checkInterval);
            if (startTime + timeout < System.currentTimeMillis()) {
bernard authored
50
                throw new RuntimeException("Number of JavaScript jobs doesn't drop to initial level for " + timeout + " seconds. It's memory leak in your JavaScript rather then request taking so long!");
bernard authored
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
            }
        } 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;
    }
bernard authored
77 78 79 80 81 82 83 84 85 86 87 88 89 90

    public static void forceWait(int timeout) {
        final long startTime = System.currentTimeMillis();
        do {
            try {
                final long millis = startTime + timeout - System.currentTimeMillis();
                if (millis > 0) {
                    Thread.sleep(millis);
                }
            } catch (InterruptedException ignore) {
            } catch (IllegalArgumentException ignore) {
            }
        } while (startTime + timeout > System.currentTimeMillis());
    }
bernard authored
91
}