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

Commit

Permalink
Enhance AbstractPage and AjaxCounterBean
Browse files Browse the repository at this point in the history
- Count outstanding timeouts with AjaxCounterBean too
- Use new WebDriverWait for each wait, instead of reusing
  FluentWait<WebDriver>
- Add AbstractPage.getExecutor() to minimise casting
- Add NamedPredicate for better failure messages
  • Loading branch information
seanf committed Feb 13, 2015
1 parent 0f3d6b4 commit 7776982
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 28 deletions.
17 changes: 9 additions & 8 deletions functional-test/src/main/java/org/zanata/page/AbstractPage.java
Expand Up @@ -46,13 +46,11 @@
@Slf4j
public class AbstractPage {
private final WebDriver driver;
private final FluentWait<WebDriver> ajaxWaitForSec;

public AbstractPage(final WebDriver driver) {
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10),
this);
this.driver = driver;
ajaxWaitForSec = WebElementUtil.waitForAMoment(driver);
waitForPageSilence();
}

Expand All @@ -75,6 +73,10 @@ public WebDriver getDriver() {
return driver;
}

public JavascriptExecutor getExecutor() {
return (JavascriptExecutor) getDriver();
}

public String getUrl() {
return driver.getCurrentUrl();
}
Expand All @@ -84,7 +86,7 @@ protected void logWaiting(String msg) {
}

public FluentWait<WebDriver> waitForAMoment() {
return ajaxWaitForSec;
return WebElementUtil.waitForAMoment(driver);
}

/**
Expand Down Expand Up @@ -200,16 +202,15 @@ protected int getExpectedBackgroundRequests() {
}

/**
* Wait for any AJAX calls to return.
* Wait for any AJAX/timeout calls to return.
*/
public void waitForPageSilence() {
// Wait for AJAX calls to be 0
// Wait for AJAX/timeout calls to be 0
waitForAMoment().withMessage("page silence").until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
Long ajaxCalls = (Long) ((JavascriptExecutor) getDriver())
.executeScript(
"return XMLHttpRequest.active");
Long ajaxCalls = (Long) getExecutor().executeScript(
"return XMLHttpRequest.active + window.timeoutCounter");
if (ajaxCalls == null) {
if (log.isWarnEnabled()) {
String url = getDriver().getCurrentUrl();
Expand Down
8 changes: 3 additions & 5 deletions functional-test/src/main/java/org/zanata/page/BasePage.java
Expand Up @@ -232,14 +232,12 @@ public <P> P goToPage(String navLinkText, Class<P> pageClass) {
* @param locator
*/
public void clickLinkAfterAnimation(By locator) {
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("arguments[0].click();", getDriver()
getExecutor().executeScript("arguments[0].click();", getDriver()
.findElement(locator));
}

public void clickLinkAfterAnimation(WebElement element) {
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("arguments[0].click();", element);
getExecutor().executeScript("arguments[0].click();", element);
}

public HelpPage goToHelp() {
Expand Down Expand Up @@ -331,7 +329,7 @@ public boolean apply(WebDriver input) {
}

public String getHtmlSource(WebElement webElement) {
return (String) ((JavascriptExecutor) getDriver()).executeScript(
return (String) getExecutor().executeScript(
"return arguments[0].innerHTML;", webElement);
}

Expand Down
6 changes: 3 additions & 3 deletions functional-test/src/main/java/org/zanata/page/CorePage.java
Expand Up @@ -202,7 +202,7 @@ public void defocus() {
public void defocus(By elementBy) {
log.info("Force unfocus");
WebElement element = getDriver().findElement(elementBy);
((JavascriptExecutor) getDriver()).executeScript("arguments[0].blur()", element);
getExecutor().executeScript("arguments[0].blur()", element);
}

/* The system sometimes moves too fast for the Ajax pages, so provide a
Expand All @@ -217,11 +217,11 @@ public void slightPause() {
}

public void scrollIntoView(WebElement targetElement) {
((JavascriptExecutor) getDriver()).executeScript(
getExecutor().executeScript(
"arguments[0].scrollIntoView(true);", targetElement);
}

public void scrollToTop() {
((JavascriptExecutor) getDriver()).executeScript("scroll(0, 0);");
getExecutor().executeScript("scroll(0, 0);");
}
}
Expand Up @@ -65,8 +65,7 @@ public ProjectTranslationTab setValidationLevel(String optionName,
waitForElementExists(validationsList),
By.id(optionElementID));

((JavascriptExecutor) getDriver())
.executeScript("arguments[0].click();", option);
getExecutor().executeScript("arguments[0].click();", option);
slightPause();
return new ProjectTranslationTab(getDriver());
}
Expand Down
Expand Up @@ -77,8 +77,7 @@ private boolean isCopyFromVersionAvailable() {
}

private void clickCopyFromCheckbox() {
((JavascriptExecutor) getDriver())
.executeScript("arguments[0].click();",
getExecutor().executeScript("arguments[0].click();",
waitForWebElement(copyFromPreviousVersionChk)
.findElement(By.tagName("span")));

Expand Down
Expand Up @@ -22,7 +22,6 @@

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.zanata.page.projectversion.VersionBasePage;
Expand Down Expand Up @@ -62,8 +61,7 @@ public VersionTranslationTab setValidationLevel(String optionName,
By.id("settings-translation-validation-form"))
.findElement(By.id(optionElementID));

((JavascriptExecutor) getDriver())
.executeScript("arguments[0].click();", option);
getExecutor().executeScript("arguments[0].click();", option);
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
Expand Down
@@ -0,0 +1,40 @@
/*
* Copyright 2014, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.page.utility;

import com.google.common.base.Predicate;

/**
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*/
public abstract class NamedPredicate<T> implements
Predicate<T> {
private final String name;

public NamedPredicate(String name) {
this.name = name;
}

@Override
public String toString() {
return "predicate: " + name;
}
}
Expand Up @@ -39,6 +39,7 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.zanata.page.WebDriverFactory;

import static java.util.concurrent.TimeUnit.SECONDS;
Expand Down Expand Up @@ -145,10 +146,7 @@ public List<String> apply(@Nullable TableRow from) {

public static FluentWait<WebDriver> waitForSeconds(WebDriver webDriver,
int durationInSec) {
return new FluentWait<WebDriver>(webDriver)
.withTimeout(durationInSec, SECONDS)
.pollingEvery(1, SECONDS)
.ignoring(NoSuchElementException.class,
return new WebDriverWait(webDriver, durationInSec).ignoring(
// TODO is ignoring this safe?
StaleElementReferenceException.class);
}
Expand Down
14 changes: 13 additions & 1 deletion zanata-war/src/main/java/org/zanata/action/AjaxCounterBean.java
Expand Up @@ -52,8 +52,20 @@ public class AjaxCounterBean {
" };\n" +
" this._send.apply(this, arguments);\n" +
" }\n" +
" window.timeoutCounter = 0;\n" +
" window.originalSetTimeout = window.setTimeout;\n" +
" window.setTimeout = function(func, delay, params) {\n" +
" window.timeoutCounter++;\n" +
" window.originalSetTimeout(window.timeoutCallback, delay, [func, params]);\n" +
" }\n" +
" window.timeoutCallback = function(funcAndParams) {\n" +
" window.timeoutCounter--;\n" +
" func = funcAndParams[0];\n" +
" params = funcAndParams[1];\n" +
" func(params);\n" +
" }\n" +
" }\n" +
"})(XMLHttpRequest)" +
"})(XMLHttpRequest)\n" +
"</script>\n";
public String getAjaxCounterScript() {
String propName = "zanata.countAjax";
Expand Down

0 comments on commit 7776982

Please sign in to comment.