From 08da24ba7d225f3af17a04f379554d505460bfb8 Mon Sep 17 00:00:00 2001 From: Tomasz Zabkowicz Date: Wed, 25 Jan 2012 14:03:01 +0000 Subject: [PATCH] Changes in WebClientUtils --- src/main/java/pl/labno/bernard/htmlunified/WebClientUtils.java | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 20 deletions(-) diff --git a/src/main/java/pl/labno/bernard/htmlunified/WebClientUtils.java b/src/main/java/pl/labno/bernard/htmlunified/WebClientUtils.java index f27e747..f57d4c3 100644 --- a/src/main/java/pl/labno/bernard/htmlunified/WebClientUtils.java +++ b/src/main/java/pl/labno/bernard/htmlunified/WebClientUtils.java @@ -21,51 +21,63 @@ import java.util.Map; public class WebClientUtils { private static long defaultCheckInterval = 500; + private static long defaultTimeout = 10000; - public static long getDefaultCheckInterval() { + public static long getDefaultCheckInterval() + { return defaultCheckInterval; } - public static void setDefaultCheckInterval(long defaultCheckInterval) { + public static void setDefaultCheckInterval(long defaultCheckInterval) + { WebClientUtils.defaultCheckInterval = defaultCheckInterval; } - public static long getDefaultTimeout() { + public static long getDefaultTimeout() + { return defaultTimeout; } - public static void setDefaultTimeout(long defaultTimeout) { + public static void setDefaultTimeout(long defaultTimeout) + { WebClientUtils.defaultTimeout = defaultTimeout; } - public static int waitForJSJob(String message, WebClient webClient) { + public static int waitForJSJob(String message, WebClient webClient) + { return waitForJSJob(message, webClient, webClient.waitForBackgroundJavaScript(10) - 1); } - public static int waitForJSJob(String message, WebClient webClient, int initialJobCount) { + public static int waitForJSJob(String message, WebClient webClient, int initialJobCount) + { return waitForJSJob(message, webClient, initialJobCount, defaultTimeout); } - public static int waitForJSJob(WebClient webClient, int initialJobCount, int timeout) { + public static int waitForJSJob(WebClient webClient, int initialJobCount, int timeout) + { return waitForJSJob(null, webClient, initialJobCount, timeout); } - public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, long timeout) { + public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, long timeout) + { return waitForJSJob(message, webClient, initialJobCount, timeout, defaultCheckInterval); } - public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, int timeout) { + public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, int timeout) + { return waitForJSJob(message, webClient, initialJobCount, timeout, defaultCheckInterval); } - public static int waitForJSJob(String message, WebClient webClient, int initialJobCount, long timeout, long checkInterval) { + public static int waitForJSJob(String message, 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!"); + 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" + (message == null ? "" : " for " + message) + " took: " + (System.currentTimeMillis() - startTime) + "ms"); @@ -77,9 +89,11 @@ public class WebClientUtils { * * @param suggestion suggestionBox element * @param column column of suggestionBox to extract text from + * * @return list of suggestions */ - public static Map getSuggestions(HtmlElement suggestion, int column) { + public static Map getSuggestions(HtmlElement suggestion, int column) + { final Map suggestions = new HashMap(); final HtmlElement suggestElement = suggestion.getElementById(suggestion.getId() + ":suggest"); @SuppressWarnings("unchecked") @@ -95,7 +109,8 @@ public class WebClientUtils { return suggestions; } - public static void forceWait(int timeout) { + public static void forceWait(int timeout) + { final long startTime = System.currentTimeMillis(); do { try { @@ -109,7 +124,8 @@ public class WebClientUtils { } while (startTime + timeout > System.currentTimeMillis()); } - public static void executeAjaxReRenderedScripts(HtmlPage page) { + public static void executeAjaxReRenderedScripts(HtmlPage page) + { final DomNodeList scripts = page.getElementsByTagName("script"); /** * We cannot iterate over html DomNodeList cause it depends on sibling relationship which we will modify. @@ -137,14 +153,16 @@ public class WebClientUtils { } } - public static void setColorPickerValue(WebClient webClient, HtmlElement colorPicker, String hex) throws IOException { + public static void setColorPickerValue(WebClient webClient, HtmlElement colorPicker, String hex) throws IOException + { HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); colorPicker.click(); ((HtmlInput) page.getElementById(colorPicker.getId() + "-colorPicker-hex")).setValueAttribute(hex); page.getElementById(colorPicker.getId() + "-colorPicker-popup").getHtmlElementsByTagName("button").get(0).click(); } - public static void setColorPickerValue(WebClient webClient, HtmlElement colorPicker, int red, int green, int blue) throws IOException { + public static void setColorPickerValue(WebClient webClient, HtmlElement colorPicker, int red, int green, int blue) throws IOException + { HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); colorPicker.click(); ((HtmlInput) page.getElementById(colorPicker.getId() + "-colorPicker-rgb-r")).setValueAttribute(Integer.toString(red)); @@ -154,28 +172,68 @@ public class WebClientUtils { } @SuppressWarnings("unchecked") - public static List getTableRows(HtmlTable table) { + public static List getTableRows(HtmlTable table) + { return (List) table.getByXPath(".//*[contains(@class,'rich-table-row')]"); } + /** + * Checks if a specified cell in given table contains searched text. + * + * @param table HtmlTable element in which text is searched. + * @param cellNumber Number of column of the table in which text is searched. + * @param searchedText Text to be searched + * + * @return True if text was found, false otherwise. + */ + public static boolean contains(HtmlTable table, int cellNumber, String searchedText) + { + List rows = getTableRows(table); + for (HtmlTableRow row : rows) { + if (row.getCell(cellNumber).getTextContent().contains(searchedText)) { + return true; + } + } + return false; + } + @SuppressWarnings("unchecked") - public static List getScheduleEventTitles(HtmlElement schedule) { + public static List getScheduleEventTitles(HtmlElement schedule) + { return (List) schedule.getByXPath(".//*[@class='fc-event-title']"); } - public static void switchScheduleToNextMonth(HtmlElement schedule) throws IOException { + public static void switchScheduleToNextMonth(HtmlElement schedule) throws IOException + { ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-next')]").get(0)).click(); } + public static void switchScheduleToMonthlyView(HtmlElement schedule) throws IOException + { + ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-month')]").get(0)).click(); + } + + public static void switchScheduleToWeeklyView(HtmlElement schedule) throws IOException + { + ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-agendaWeek')]").get(0)).click(); + } + + public static void switchScheduleToDailyView(HtmlElement schedule) throws IOException + { + ((HtmlElement) schedule.getByXPath(".//*[contains(@class,'fc-button-agendaDay')]").get(0)).click(); + } + /** * IMPORTANT: Because of bugs in htmlunit and jsfunit which make schedule component render improperly this method should be used only to retrieve * cell elements from the first row of schedule grid. Any operations on cell elements from other rows are likely to cause error. * * @param schedule schedule element * @param dayOfMonth number of day of month representing cell you wish to retrieve + * * @return list of schedule cells */ - public static HtmlTableDataCell getScheduleDayCell(HtmlElement schedule, int dayOfMonth) { + public static HtmlTableDataCell getScheduleDayCell(HtmlElement schedule, int dayOfMonth) + { return (HtmlTableDataCell) schedule.getByXPath(".//td[contains(@class,'fc-day') and not(contains(@class,'fc-other-month'))]").get(dayOfMonth - 1); } } -- libgit2 0.24.0