WebClientUtils.java
3.56 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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);
}
public static int waitForJSJob(WebClient webClient, int initialJobCount) {
return waitForJSJob(webClient, initialJobCount, defaultTimeout);
}
public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout) {
return waitForJSJob(webClient, initialJobCount, timeout, defaultCheckInterval);
}
public static int waitForJSJob(WebClient webClient, int initialJobCount, long timeout, long checkInterval) {
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 " + timeout + " 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;
}
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());
}
}