diff --git a/functional-test/src/main/java/org/zanata/page/AbstractPage.java b/functional-test/src/main/java/org/zanata/page/AbstractPage.java index d73826319b..8a8e68fb7f 100644 --- a/functional-test/src/main/java/org/zanata/page/AbstractPage.java +++ b/functional-test/src/main/java/org/zanata/page/AbstractPage.java @@ -28,6 +28,7 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; +import org.hamcrest.StringDescription; import org.openqa.selenium.*; import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory; @@ -103,6 +104,10 @@ public String getUrl() { return driver.getCurrentUrl(); } + protected void logWaiting(String msg) { + log.info("Waiting for {}", msg); + } + public FluentWait waitForAMoment() { return ajaxWaitForSec; } @@ -119,7 +124,7 @@ public void waitForPage(List elementBys) { } public Alert switchToAlert() { - return waitForAMoment().until(new Function() { + return waitForAMoment().withMessage("alert").until(new Function() { @Override public Alert apply(WebDriver driver) { try { @@ -131,16 +136,50 @@ public Alert apply(WebDriver driver) { }); } + /** + * @deprecated Use the overload which includes a message + */ + @Deprecated protected

P refreshPageUntil(P currentPage, Predicate predicate) { - waitForAMoment().until(predicate); + return refreshPageUntil(currentPage, predicate, null); + } + + /** + * @param currentPage + * @param predicate + * @param message description of predicate + * @param

+ * @return + */ + protected

P refreshPageUntil(P currentPage, + Predicate predicate, String message) { + waitForAMoment().withMessage(message).until(predicate); PageFactory.initElements(driver, currentPage); return currentPage; } + /** + * @deprecated Use the overload which includes a message + */ + @Deprecated protected

T refreshPageUntil(P currentPage, Function function) { - T done = waitForAMoment().until(function); + return refreshPageUntil(currentPage, function, null); + } + + /** + * + * @param currentPage + * @param function + * @param message description of function + * @param

+ * @param + * @return + */ + protected

T refreshPageUntil(P currentPage, + Function function, String message) { + T done = waitForAMoment().withMessage(message).until(function); PageFactory.initElements(driver, currentPage); return done; } @@ -156,22 +195,23 @@ protected

T refreshPageUntil(P currentPage, */ public void waitFor(final Callable callable, final Matcher matcher) { - waitForAMoment().until(new Predicate() { - @Override - public boolean apply(WebDriver input) { - try { - T result = callable.call(); - if (!matcher.matches(result)) { - matcher.describeMismatch(result, - new Description.NullDescription()); + waitForAMoment().withMessage(StringDescription.toString(matcher)).until( + new Predicate() { + @Override + public boolean apply(WebDriver input) { + try { + T result = callable.call(); + if (!matcher.matches(result)) { + matcher.describeMismatch(result, + new Description.NullDescription()); + } + return matcher.matches(result); + } catch (Exception e) { + log.warn("exception", e); + return false; + } } - return matcher.matches(result); - } catch (Exception e) { - log.warn("exception", e); - return false; - } - } - }); + }); } /** @@ -180,7 +220,7 @@ public boolean apply(WebDriver input) { */ public void waitForPageSilence() { // Wait for jQuery calls to be 0 - waitForAMoment().until(new Predicate() { + waitForAMoment().withMessage("page silence").until(new Predicate() { @Override public boolean apply(WebDriver input) { int ajaxCalls; @@ -204,9 +244,10 @@ public boolean apply(WebDriver input) { * @return target WebElement */ public WebElement waitForWebElement(final By elementBy) { - log.info("Waiting for element ready {}", elementBy.toString()); + String msg = "element ready " + elementBy; + logWaiting(msg); waitForPageSilence(); - return waitForAMoment().until(new Function() { + return waitForAMoment().withMessage(msg).until(new Function() { @Override public WebElement apply(WebDriver input) { WebElement targetElement = getDriver().findElement(elementBy); @@ -226,9 +267,10 @@ public WebElement apply(WebDriver input) { */ public WebElement waitForWebElement(final WebElement parentElement, final By elementBy) { - log.info("Waiting for element ready {}", elementBy.toString()); + String msg = "element ready " + elementBy; + logWaiting(msg); waitForPageSilence(); - return waitForAMoment().until(new Function() { + return waitForAMoment().withMessage(msg).until(new Function() { @Override public WebElement apply(WebDriver input) { WebElement targetElement = parentElement.findElement(elementBy); @@ -248,9 +290,11 @@ public WebElement apply(WebDriver input) { * @return target WebElement */ public WebElement waitForElementExists(final By elementBy) { - log.info("Waiting for element exists {}", elementBy.toString()); + String msg = "element exists " + elementBy; + logWaiting(msg); waitForPageSilence(); - return waitForAMoment().until(new Function() { + return waitForAMoment().withMessage(msg).until( + new Function() { @Override public WebElement apply(WebDriver input) { return getDriver().findElement(elementBy); @@ -267,9 +311,10 @@ public WebElement apply(WebDriver input) { */ public WebElement waitForElementExists(final WebElement parentElement, final By elementBy) { - log.info("Waiting for element exists {}", elementBy.toString()); + String msg = "element exists " + elementBy; + logWaiting(msg); waitForPageSilence(); - return waitForAMoment().until(new Function() { + return waitForAMoment().withMessage(msg).until(new Function() { @Override public WebElement apply(WebDriver input) { return parentElement.findElement(elementBy); diff --git a/functional-test/src/main/java/org/zanata/page/BasePage.java b/functional-test/src/main/java/org/zanata/page/BasePage.java index b896b431d4..0cc2833b0a 100644 --- a/functional-test/src/main/java/org/zanata/page/BasePage.java +++ b/functional-test/src/main/java/org/zanata/page/BasePage.java @@ -104,14 +104,16 @@ private void clickNavMenuItem(final WebElement menuItem) { getDriver().findElement(By.id("nav-main")) .findElement(By.tagName("a")).click(); } - waitForAMoment().until(new Predicate() { - @Override - public boolean apply(WebDriver input) { - return menuItem.isDisplayed(); - } - }); + waitForAMoment().withMessage("displayed: " + menuItem).until( + new Predicate() { + @Override + public boolean apply(WebDriver input) { + return menuItem.isDisplayed(); + } + }); // The notifications can sometimes get in the way - waitForAMoment().until(ExpectedConditions.elementToBeClickable(menuItem)); + waitForAMoment().withMessage("clickable: " + menuItem).until( + ExpectedConditions.elementToBeClickable(menuItem)); menuItem.click(); } @@ -263,8 +265,9 @@ public ProjectsPage submitSearch() { } public BasePage waitForSearchListContains(final String expected) { - log.info("Wait for Project search list contains {}", expected); - waitForAMoment().until(new Predicate() { + String msg = "Project search list contains " + expected; + logWaiting(msg); + waitForAMoment().withMessage(msg).until(new Predicate() { @Override public boolean apply(WebDriver input) { return getProjectSearchAutocompleteItems().contains(expected); @@ -281,29 +284,34 @@ public List getProjectSearchAutocompleteItems() { public ProjectVersionsPage clickSearchEntry(final String searchEntry) { log.info("Click Projects search result {}", searchEntry); + String msg = "search result " + searchEntry; WebElement searchItem = - waitForAMoment().until(new Function() { - @Override - public WebElement apply(WebDriver driver) { - List items = - WebElementUtil.getSearchAutocompleteResults( - driver, "general-search-form", - "projectAutocomplete"); - - for (WebElement item : items) { - if (item.getText().equals(searchEntry)) { - return item; + waitForAMoment().withMessage(msg).until( + new Function() { + @Override + public WebElement apply(WebDriver driver) { + List items = + WebElementUtil + .getSearchAutocompleteResults( + driver, + "general-search-form", + "projectAutocomplete"); + + for (WebElement item : items) { + if (item.getText().equals(searchEntry)) { + return item; + } + } + return null; } - } - return null; - } - }); + }); searchItem.click(); return new ProjectVersionsPage(getDriver()); } public void clickWhenTabEnabled(final WebElement tab) { - waitForAMoment().until(new Predicate() { + String msg = "Clickable tab: " + tab; + waitForAMoment().withMessage(msg).until(new Predicate() { @Override public boolean apply(WebDriver input) { waitForPageSilence(); diff --git a/functional-test/src/main/java/org/zanata/page/CorePage.java b/functional-test/src/main/java/org/zanata/page/CorePage.java index 3e8d2aab58..a5d287dd51 100644 --- a/functional-test/src/main/java/org/zanata/page/CorePage.java +++ b/functional-test/src/main/java/org/zanata/page/CorePage.java @@ -81,7 +81,7 @@ protected void clickAndExpectErrors(WebElement button) { public boolean apply(WebDriver input) { return getErrors().size() > 0; } - }); + }, "errors > 0"); } public List getErrors() { @@ -115,7 +115,7 @@ public List getErrors(final int expectedNumber) { public boolean apply(WebDriver input) { return getErrors().size() == expectedNumber; } - }); + }, "errors = " + expectedNumber); return getErrors(); } @@ -126,8 +126,9 @@ public boolean apply(WebDriver input) { * @return The full list of visible errors */ public List expectError(final String expected) { - log.info("Expect error {}", expected); - waitForAMoment().until(new Predicate() { + String msg = "expected error: " + expected; + logWaiting(msg); + waitForAMoment().withMessage(msg).until(new Predicate() { @Override public boolean apply(WebDriver input) { return getErrors().contains(expected); @@ -144,20 +145,22 @@ public String getNotificationMessage() { } public boolean expectNotification(final String notification) { - log.info("Wait for notification {}", notification); - return waitForAMoment().until(new Function() { - @Override - public Boolean apply(WebDriver driver) { - List messages = getDriver() - .findElement(By.id("messages")) - .findElements(By.tagName("li")); - List notifications = new ArrayList(); - for (WebElement message : messages) { - notifications.add(message.getText().trim()); - } - return notifications.contains(notification); - } - }); + String msg = "notification " + notification; + logWaiting(msg); + return waitForAMoment().withMessage(msg).until( + new Function() { + @Override + public Boolean apply(WebDriver driver) { + List messages = getDriver() + .findElement(By.id("messages")) + .findElements(By.tagName("li")); + List notifications = new ArrayList(); + for (WebElement message : messages) { + notifications.add(message.getText().trim()); + } + return notifications.contains(notification); + } + }); } public void assertNoCriticalErrors() {