Showing
1 changed file
with
78 additions
and
20 deletions
@@ -21,51 +21,63 @@ import java.util.Map; | @@ -21,51 +21,63 @@ import java.util.Map; | ||
21 | public class WebClientUtils { | 21 | public class WebClientUtils { |
22 | 22 | ||
23 | private static long defaultCheckInterval = 500; | 23 | private static long defaultCheckInterval = 500; |
24 | + | ||
24 | private static long defaultTimeout = 10000; | 25 | private static long defaultTimeout = 10000; |
25 | 26 | ||
26 | - public static long getDefaultCheckInterval() { | 27 | + public static long getDefaultCheckInterval() |
28 | + { | ||
27 | return defaultCheckInterval; | 29 | return defaultCheckInterval; |
28 | } | 30 | } |
29 | 31 | ||
30 | - public static void setDefaultCheckInterval(long defaultCheckInterval) { | 32 | + public static void setDefaultCheckInterval(long defaultCheckInterval) |
33 | + { | ||
31 | WebClientUtils.defaultCheckInterval = defaultCheckInterval; | 34 | WebClientUtils.defaultCheckInterval = defaultCheckInterval; |
32 | } | 35 | } |
33 | 36 | ||
34 | - public static long getDefaultTimeout() { | 37 | + public static long getDefaultTimeout() |
38 | + { | ||
35 | return defaultTimeout; | 39 | return defaultTimeout; |
36 | } | 40 | } |
37 | 41 | ||
38 | - public static void setDefaultTimeout(long defaultTimeout) { | 42 | + public static void setDefaultTimeout(long defaultTimeout) |
43 | + { | ||
39 | WebClientUtils.defaultTimeout = defaultTimeout; | 44 | WebClientUtils.defaultTimeout = defaultTimeout; |
40 | } | 45 | } |
41 | 46 | ||
42 | - public static int waitForJSJob(String message, WebClient webClient) { | 47 | + public static int waitForJSJob(String message, WebClient webClient) |
48 | + { | ||
43 | return waitForJSJob(message, webClient, webClient.waitForBackgroundJavaScript(10) - 1); | 49 | return waitForJSJob(message, webClient, webClient.waitForBackgroundJavaScript(10) - 1); |
44 | } | 50 | } |
45 | 51 | ||
46 | - public static int waitForJSJob(String message, WebClient webClient, int initialJobCount) { | 52 | + public static int waitForJSJob(String message, WebClient webClient, int initialJobCount) |
53 | + { | ||
47 | return waitForJSJob(message, webClient, initialJobCount, defaultTimeout); | 54 | return waitForJSJob(message, webClient, initialJobCount, defaultTimeout); |
48 | } | 55 | } |
49 | 56 | ||
50 | - public static int waitForJSJob(WebClient webClient, int initialJobCount, int timeout) { | 57 | + public static int waitForJSJob(WebClient webClient, int initialJobCount, int timeout) |
58 | + { | ||
51 | return waitForJSJob(null, webClient, initialJobCount, timeout); | 59 | return waitForJSJob(null, webClient, initialJobCount, timeout); |
52 | } | 60 | } |
53 | 61 | ||
54 | - public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, long timeout) { | 62 | + public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, long timeout) |
63 | + { | ||
55 | return waitForJSJob(message, webClient, initialJobCount, timeout, defaultCheckInterval); | 64 | return waitForJSJob(message, webClient, initialJobCount, timeout, defaultCheckInterval); |
56 | } | 65 | } |
57 | 66 | ||
58 | - public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, int timeout) { | 67 | + public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, int timeout) |
68 | + { | ||
59 | return waitForJSJob(message, webClient, initialJobCount, timeout, defaultCheckInterval); | 69 | return waitForJSJob(message, webClient, initialJobCount, timeout, defaultCheckInterval); |
60 | } | 70 | } |
61 | 71 | ||
62 | - public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, long timeout, long checkInterval) { | 72 | + public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, long timeout, long checkInterval) |
73 | + { | ||
63 | int jobs; | 74 | int jobs; |
64 | long startTime = System.currentTimeMillis(); | 75 | long startTime = System.currentTimeMillis(); |
65 | do { | 76 | do { |
66 | jobs = webClient.waitForBackgroundJavaScript(checkInterval); | 77 | jobs = webClient.waitForBackgroundJavaScript(checkInterval); |
67 | if (startTime + timeout < System.currentTimeMillis()) { | 78 | if (startTime + timeout < System.currentTimeMillis()) { |
68 | - 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!"); | 79 | + throw new RuntimeException( |
80 | + "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!"); | ||
69 | } | 81 | } |
70 | } while (jobs > initialJobCount); | 82 | } while (jobs > initialJobCount); |
71 | System.out.println("Waiting" + (message == null ? "" : " for " + message) + " took: " + (System.currentTimeMillis() - startTime) + "ms"); | 83 | System.out.println("Waiting" + (message == null ? "" : " for " + message) + " took: " + (System.currentTimeMillis() - startTime) + "ms"); |
@@ -77,9 +89,11 @@ public class WebClientUtils { | @@ -77,9 +89,11 @@ public class WebClientUtils { | ||
77 | * | 89 | * |
78 | * @param suggestion suggestionBox element | 90 | * @param suggestion suggestionBox element |
79 | * @param column column of suggestionBox to extract text from | 91 | * @param column column of suggestionBox to extract text from |
92 | + * | ||
80 | * @return list of suggestions | 93 | * @return list of suggestions |
81 | */ | 94 | */ |
82 | - public static Map<String, HtmlTableCell> getSuggestions(HtmlElement suggestion, int column) { | 95 | + public static Map<String, HtmlTableCell> getSuggestions(HtmlElement suggestion, int column) |
96 | + { | ||
83 | final Map<String, HtmlTableCell> suggestions = new HashMap<String, HtmlTableCell>(); | 97 | final Map<String, HtmlTableCell> suggestions = new HashMap<String, HtmlTableCell>(); |
84 | final HtmlElement suggestElement = suggestion.getElementById(suggestion.getId() + ":suggest"); | 98 | final HtmlElement suggestElement = suggestion.getElementById(suggestion.getId() + ":suggest"); |
85 | @SuppressWarnings("unchecked") | 99 | @SuppressWarnings("unchecked") |
@@ -95,7 +109,8 @@ public class WebClientUtils { | @@ -95,7 +109,8 @@ public class WebClientUtils { | ||
95 | return suggestions; | 109 | return suggestions; |
96 | } | 110 | } |
97 | 111 | ||
98 | - public static void forceWait(int timeout) { | 112 | + public static void forceWait(int timeout) |
113 | + { | ||
99 | final long startTime = System.currentTimeMillis(); | 114 | final long startTime = System.currentTimeMillis(); |
100 | do { | 115 | do { |
101 | try { | 116 | try { |
@@ -109,7 +124,8 @@ public class WebClientUtils { | @@ -109,7 +124,8 @@ public class WebClientUtils { | ||
109 | } while (startTime + timeout > System.currentTimeMillis()); | 124 | } while (startTime + timeout > System.currentTimeMillis()); |
110 | } | 125 | } |
111 | 126 | ||
112 | - public static void executeAjaxReRenderedScripts(HtmlPage page) { | 127 | + public static void executeAjaxReRenderedScripts(HtmlPage page) |
128 | + { | ||
113 | final DomNodeList<HtmlElement> scripts = page.getElementsByTagName("script"); | 129 | final DomNodeList<HtmlElement> scripts = page.getElementsByTagName("script"); |
114 | /** | 130 | /** |
115 | * We cannot iterate over html DomNodeList cause it depends on sibling relationship which we will modify. | 131 | * We cannot iterate over html DomNodeList cause it depends on sibling relationship which we will modify. |
@@ -137,14 +153,16 @@ public class WebClientUtils { | @@ -137,14 +153,16 @@ public class WebClientUtils { | ||
137 | } | 153 | } |
138 | } | 154 | } |
139 | 155 | ||
140 | - public static void setColorPickerValue(WebClient webClient, HtmlElement colorPicker, String hex) throws IOException { | 156 | + public static void setColorPickerValue(WebClient webClient, HtmlElement colorPicker, String hex) throws IOException |
157 | + { | ||
141 | HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); | 158 | HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); |
142 | colorPicker.click(); | 159 | colorPicker.click(); |
143 | ((HtmlInput) page.getElementById(colorPicker.getId() + "-colorPicker-hex")).setValueAttribute(hex); | 160 | ((HtmlInput) page.getElementById(colorPicker.getId() + "-colorPicker-hex")).setValueAttribute(hex); |
144 | page.getElementById(colorPicker.getId() + "-colorPicker-popup").getHtmlElementsByTagName("button").get(0).click(); | 161 | page.getElementById(colorPicker.getId() + "-colorPicker-popup").getHtmlElementsByTagName("button").get(0).click(); |
145 | } | 162 | } |
146 | 163 | ||
147 | - public static void setColorPickerValue(WebClient webClient, HtmlElement colorPicker, int red, int green, int blue) throws IOException { | 164 | + public static void setColorPickerValue(WebClient webClient, HtmlElement colorPicker, int red, int green, int blue) throws IOException |
165 | + { | ||
148 | HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); | 166 | HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); |
149 | colorPicker.click(); | 167 | colorPicker.click(); |
150 | ((HtmlInput) page.getElementById(colorPicker.getId() + "-colorPicker-rgb-r")).setValueAttribute(Integer.toString(red)); | 168 | ((HtmlInput) page.getElementById(colorPicker.getId() + "-colorPicker-rgb-r")).setValueAttribute(Integer.toString(red)); |
@@ -154,28 +172,68 @@ public class WebClientUtils { | @@ -154,28 +172,68 @@ public class WebClientUtils { | ||
154 | } | 172 | } |
155 | 173 | ||
156 | @SuppressWarnings("unchecked") | 174 | @SuppressWarnings("unchecked") |
157 | - public static List<HtmlTableRow> getTableRows(HtmlTable table) { | 175 | + public static List<HtmlTableRow> getTableRows(HtmlTable table) |
176 | + { | ||
158 | return (List<HtmlTableRow>) table.getByXPath(".//*[contains(@class,'rich-table-row')]"); | 177 | return (List<HtmlTableRow>) table.getByXPath(".//*[contains(@class,'rich-table-row')]"); |
159 | } | 178 | } |
160 | 179 | ||
180 | + /** | ||
181 | + * Checks if a specified cell in given table contains searched text. | ||
182 | + * | ||
183 | + * @param table HtmlTable element in which text is searched. | ||
184 | + * @param cellNumber Number of column of the table in which text is searched. | ||
185 | + * @param searchedText Text to be searched | ||
186 | + * | ||
187 | + * @return True if text was found, false otherwise. | ||
188 | + */ | ||
189 | + public static boolean contains(HtmlTable table, int cellNumber, String searchedText) | ||
190 | + { | ||
191 | + List<HtmlTableRow> rows = getTableRows(table); | ||
192 | + for (HtmlTableRow row : rows) { | ||
193 | + if (row.getCell(cellNumber).getTextContent().contains(searchedText)) { | ||
194 | + return true; | ||
195 | + } | ||
196 | + } | ||
197 | + return false; | ||
198 | + } | ||
199 | + | ||
161 | @SuppressWarnings("unchecked") | 200 | @SuppressWarnings("unchecked") |
162 | - public static List<HtmlSpan> getScheduleEventTitles(HtmlElement schedule) { | 201 | + public static List<HtmlSpan> getScheduleEventTitles(HtmlElement schedule) |
202 | + { | ||
163 | return (List<HtmlSpan>) schedule.getByXPath(".//*[@class='fc-event-title']"); | 203 | return (List<HtmlSpan>) schedule.getByXPath(".//*[@class='fc-event-title']"); |
164 | } | 204 | } |
165 | 205 | ||
166 | - public static void switchScheduleToNextMonth(HtmlElement schedule) throws IOException { | 206 | + public static void switchScheduleToNextMonth(HtmlElement schedule) throws IOException |
207 | + { | ||
167 | ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-next')]").get(0)).click(); | 208 | ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-next')]").get(0)).click(); |
168 | } | 209 | } |
169 | 210 | ||
211 | + public static void switchScheduleToMonthlyView(HtmlElement schedule) throws IOException | ||
212 | + { | ||
213 | + ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-month')]").get(0)).click(); | ||
214 | + } | ||
215 | + | ||
216 | + public static void switchScheduleToWeeklyView(HtmlElement schedule) throws IOException | ||
217 | + { | ||
218 | + ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-agendaWeek')]").get(0)).click(); | ||
219 | + } | ||
220 | + | ||
221 | + public static void switchScheduleToDailyView(HtmlElement schedule) throws IOException | ||
222 | + { | ||
223 | + ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-agendaDay')]").get(0)).click(); | ||
224 | + } | ||
225 | + | ||
170 | /** | 226 | /** |
171 | * IMPORTANT: Because of bugs in htmlunit and jsfunit which make schedule component render improperly this method should be used only to retrieve | 227 | * IMPORTANT: Because of bugs in htmlunit and jsfunit which make schedule component render improperly this method should be used only to retrieve |
172 | * cell elements from the first row of schedule grid. Any operations on cell elements from other rows are likely to cause error. | 228 | * cell elements from the first row of schedule grid. Any operations on cell elements from other rows are likely to cause error. |
173 | * | 229 | * |
174 | * @param schedule schedule element | 230 | * @param schedule schedule element |
175 | * @param dayOfMonth number of day of month representing cell you wish to retrieve | 231 | * @param dayOfMonth number of day of month representing cell you wish to retrieve |
232 | + * | ||
176 | * @return list of schedule cells | 233 | * @return list of schedule cells |
177 | */ | 234 | */ |
178 | - public static HtmlTableDataCell getScheduleDayCell(HtmlElement schedule, int dayOfMonth) { | 235 | + public static HtmlTableDataCell getScheduleDayCell(HtmlElement schedule, int dayOfMonth) |
236 | + { | ||
179 | return (HtmlTableDataCell) schedule.getByXPath(".//td[contains(@class,'fc-day') and not(contains(@class,'fc-other-month'))]").get(dayOfMonth - 1); | 237 | return (HtmlTableDataCell) schedule.getByXPath(".//td[contains(@class,'fc-day') and not(contains(@class,'fc-other-month'))]").get(dayOfMonth - 1); |
180 | } | 238 | } |
181 | } | 239 | } |
Please
register
or
login
to post a comment