Showing
1 changed file
with
78 additions
and
20 deletions
... | ... | @@ -21,51 +21,63 @@ import java.util.Map; |
21 | 21 | public class WebClientUtils { |
22 | 22 | |
23 | 23 | private static long defaultCheckInterval = 500; |
24 | + | |
24 | 25 | private static long defaultTimeout = 10000; |
25 | 26 | |
26 | - public static long getDefaultCheckInterval() { | |
27 | + public static long getDefaultCheckInterval() | |
28 | + { | |
27 | 29 | return defaultCheckInterval; |
28 | 30 | } |
29 | 31 | |
30 | - public static void setDefaultCheckInterval(long defaultCheckInterval) { | |
32 | + public static void setDefaultCheckInterval(long defaultCheckInterval) | |
33 | + { | |
31 | 34 | WebClientUtils.defaultCheckInterval = defaultCheckInterval; |
32 | 35 | } |
33 | 36 | |
34 | - public static long getDefaultTimeout() { | |
37 | + public static long getDefaultTimeout() | |
38 | + { | |
35 | 39 | return defaultTimeout; |
36 | 40 | } |
37 | 41 | |
38 | - public static void setDefaultTimeout(long defaultTimeout) { | |
42 | + public static void setDefaultTimeout(long defaultTimeout) | |
43 | + { | |
39 | 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 | 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 | 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 | 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 | 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 | 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 | 74 | int jobs; |
64 | 75 | long startTime = System.currentTimeMillis(); |
65 | 76 | do { |
66 | 77 | jobs = webClient.waitForBackgroundJavaScript(checkInterval); |
67 | 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 | 82 | } while (jobs > initialJobCount); |
71 | 83 | System.out.println("Waiting" + (message == null ? "" : " for " + message) + " took: " + (System.currentTimeMillis() - startTime) + "ms"); |
... | ... | @@ -77,9 +89,11 @@ public class WebClientUtils { |
77 | 89 | * |
78 | 90 | * @param suggestion suggestionBox element |
79 | 91 | * @param column column of suggestionBox to extract text from |
92 | + * | |
80 | 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 | 97 | final Map<String, HtmlTableCell> suggestions = new HashMap<String, HtmlTableCell>(); |
84 | 98 | final HtmlElement suggestElement = suggestion.getElementById(suggestion.getId() + ":suggest"); |
85 | 99 | @SuppressWarnings("unchecked") |
... | ... | @@ -95,7 +109,8 @@ public class WebClientUtils { |
95 | 109 | return suggestions; |
96 | 110 | } |
97 | 111 | |
98 | - public static void forceWait(int timeout) { | |
112 | + public static void forceWait(int timeout) | |
113 | + { | |
99 | 114 | final long startTime = System.currentTimeMillis(); |
100 | 115 | do { |
101 | 116 | try { |
... | ... | @@ -109,7 +124,8 @@ public class WebClientUtils { |
109 | 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 | 129 | final DomNodeList<HtmlElement> scripts = page.getElementsByTagName("script"); |
114 | 130 | /** |
115 | 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 | 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 | 158 | HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); |
142 | 159 | colorPicker.click(); |
143 | 160 | ((HtmlInput) page.getElementById(colorPicker.getId() + "-colorPicker-hex")).setValueAttribute(hex); |
144 | 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 | 166 | HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); |
149 | 167 | colorPicker.click(); |
150 | 168 | ((HtmlInput) page.getElementById(colorPicker.getId() + "-colorPicker-rgb-r")).setValueAttribute(Integer.toString(red)); |
... | ... | @@ -154,28 +172,68 @@ public class WebClientUtils { |
154 | 172 | } |
155 | 173 | |
156 | 174 | @SuppressWarnings("unchecked") |
157 | - public static List<HtmlTableRow> getTableRows(HtmlTable table) { | |
175 | + public static List<HtmlTableRow> getTableRows(HtmlTable table) | |
176 | + { | |
158 | 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 | 200 | @SuppressWarnings("unchecked") |
162 | - public static List<HtmlSpan> getScheduleEventTitles(HtmlElement schedule) { | |
201 | + public static List<HtmlSpan> getScheduleEventTitles(HtmlElement schedule) | |
202 | + { | |
163 | 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 | 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 | 227 | * IMPORTANT: Because of bugs in htmlunit and jsfunit which make schedule component render improperly this method should be used only to retrieve |
172 | 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 | 230 | * @param schedule schedule element |
175 | 231 | * @param dayOfMonth number of day of month representing cell you wish to retrieve |
232 | + * | |
176 | 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 | 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