Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Include descriptions in some timeout exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Jan 22, 2015
1 parent 478eae5 commit 49b364d
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 70 deletions.
99 changes: 72 additions & 27 deletions functional-test/src/main/java/org/zanata/page/AbstractPage.java
Expand Up @@ -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;
Expand Down Expand Up @@ -103,6 +104,10 @@ public String getUrl() {
return driver.getCurrentUrl();
}

protected void logWaiting(String msg) {
log.info("Waiting for {}", msg);
}

public FluentWait<WebDriver> waitForAMoment() {
return ajaxWaitForSec;
}
Expand All @@ -119,7 +124,7 @@ public void waitForPage(List<By> elementBys) {
}

public Alert switchToAlert() {
return waitForAMoment().until(new Function<WebDriver, Alert>() {
return waitForAMoment().withMessage("alert").until(new Function<WebDriver, Alert>() {
@Override
public Alert apply(WebDriver driver) {
try {
Expand All @@ -131,16 +136,50 @@ public Alert apply(WebDriver driver) {
});
}

/**
* @deprecated Use the overload which includes a message
*/
@Deprecated
protected <P extends AbstractPage> P refreshPageUntil(P currentPage,
Predicate<WebDriver> predicate) {
waitForAMoment().until(predicate);
return refreshPageUntil(currentPage, predicate, null);
}

/**
* @param currentPage
* @param predicate
* @param message description of predicate
* @param <P>
* @return
*/
protected <P extends AbstractPage> P refreshPageUntil(P currentPage,
Predicate<WebDriver> predicate, String message) {
waitForAMoment().withMessage(message).until(predicate);
PageFactory.initElements(driver, currentPage);
return currentPage;
}

/**
* @deprecated Use the overload which includes a message
*/
@Deprecated
protected <P extends AbstractPage, T> T refreshPageUntil(P currentPage,
Function<WebDriver, T> function) {
T done = waitForAMoment().until(function);
return refreshPageUntil(currentPage, function, null);
}

/**
*
* @param currentPage
* @param function
* @param message description of function
* @param <P>
* @param <T>
* @return
*/
protected <P extends AbstractPage, T> T refreshPageUntil(P currentPage,
Function<WebDriver, T> function, String message) {
T done = waitForAMoment().withMessage(message).until(function);
PageFactory.initElements(driver, currentPage);
return done;
}
Expand All @@ -156,22 +195,23 @@ protected <P extends AbstractPage, T> T refreshPageUntil(P currentPage,
*/
public <T> void
waitFor(final Callable<T> callable, final Matcher<T> matcher) {
waitForAMoment().until(new Predicate<WebDriver>() {
@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<WebDriver>() {
@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;
}
}
});
});
}

/**
Expand All @@ -180,7 +220,7 @@ public boolean apply(WebDriver input) {
*/
public void waitForPageSilence() {
// Wait for jQuery calls to be 0
waitForAMoment().until(new Predicate<WebDriver>() {
waitForAMoment().withMessage("page silence").until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
int ajaxCalls;
Expand All @@ -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<WebDriver, WebElement>() {
return waitForAMoment().withMessage(msg).until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver input) {
WebElement targetElement = getDriver().findElement(elementBy);
Expand All @@ -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<WebDriver, WebElement>() {
return waitForAMoment().withMessage(msg).until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver input) {
WebElement targetElement = parentElement.findElement(elementBy);
Expand All @@ -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<WebDriver, WebElement>() {
return waitForAMoment().withMessage(msg).until(
new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver input) {
return getDriver().findElement(elementBy);
Expand All @@ -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<WebDriver, WebElement>() {
return waitForAMoment().withMessage(msg).until(new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver input) {
return parentElement.findElement(elementBy);
Expand Down
58 changes: 33 additions & 25 deletions functional-test/src/main/java/org/zanata/page/BasePage.java
Expand Up @@ -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<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
return menuItem.isDisplayed();
}
});
waitForAMoment().withMessage("displayed: " + menuItem).until(
new Predicate<WebDriver>() {
@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();
}

Expand Down Expand Up @@ -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<WebDriver>() {
String msg = "Project search list contains " + expected;
logWaiting(msg);
waitForAMoment().withMessage(msg).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
return getProjectSearchAutocompleteItems().contains(expected);
Expand All @@ -281,29 +284,34 @@ public List<String> 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<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
List<WebElement> 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<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
List<WebElement> 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<WebDriver>() {
String msg = "Clickable tab: " + tab;
waitForAMoment().withMessage(msg).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
waitForPageSilence();
Expand Down
39 changes: 21 additions & 18 deletions functional-test/src/main/java/org/zanata/page/CorePage.java
Expand Up @@ -81,7 +81,7 @@ protected void clickAndExpectErrors(WebElement button) {
public boolean apply(WebDriver input) {
return getErrors().size() > 0;
}
});
}, "errors > 0");
}

public List<String> getErrors() {
Expand Down Expand Up @@ -115,7 +115,7 @@ public List<String> getErrors(final int expectedNumber) {
public boolean apply(WebDriver input) {
return getErrors().size() == expectedNumber;
}
});
}, "errors = " + expectedNumber);
return getErrors();
}

Expand All @@ -126,8 +126,9 @@ public boolean apply(WebDriver input) {
* @return The full list of visible errors
*/
public List<String> expectError(final String expected) {
log.info("Expect error {}", expected);
waitForAMoment().until(new Predicate<WebDriver>() {
String msg = "expected error: " + expected;
logWaiting(msg);
waitForAMoment().withMessage(msg).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
return getErrors().contains(expected);
Expand All @@ -144,20 +145,22 @@ public String getNotificationMessage() {
}

public boolean expectNotification(final String notification) {
log.info("Wait for notification {}", notification);
return waitForAMoment().until(new Function<WebDriver, Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
List<WebElement> messages = getDriver()
.findElement(By.id("messages"))
.findElements(By.tagName("li"));
List<String> notifications = new ArrayList<String>();
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<WebDriver, Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
List<WebElement> messages = getDriver()
.findElement(By.id("messages"))
.findElements(By.tagName("li"));
List<String> notifications = new ArrayList<String>();
for (WebElement message : messages) {
notifications.add(message.getText().trim());
}
return notifications.contains(notification);
}
});
}

public void assertNoCriticalErrors() {
Expand Down

0 comments on commit 49b364d

Please sign in to comment.