硒等待PrimeFaces 4.0 AJAX工艺 [英] Selenium wait for PrimeFaces 4.0 ajax to process

查看:154
本文介绍了硒等待PrimeFaces 4.0 AJAX工艺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Selenium测试需要等待Ajax请求来处理,以避免竞争条件。在PrimeFaces 3.5,你可以使用下面的方法来等待(<一href="https://$c$c.google.com/p/primefaces/source/browse/examples/trunk/showcase/src/test/java/com/prime/showcase/integration/AbstractIntegrationTest.java?r=6365"相对=nofollow>从SVN回购的PrimeFaces)直接复制:

My Selenium tests need to wait for ajax requests to process to avoid race conditions. In PrimeFaces 3.5 you could use the following method to wait (copied straight from the PrimeFaces svn repo):

private static final String JQUERY_ACTIVE_CONNECTIONS_QUERY = "return $.active == 0;";
private static final int DEFAULT_SLEEP_TIME_IN_SECONDS = 2;
private static final int DEFAULT_TIMEOUT_IN_SECONDS = 10;

protected void waitUntilAjaxRequestCompletes() {
   new FluentWait<WebDriver>(driver)
      .withTimeout(DEFAULT_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)
      .pollingEvery(DEFAULT_SLEEP_TIME_IN_SECONDS, TimeUnit.SECONDS)
      .until(new ExpectedCondition<Boolean>() {
         public Boolean apply(WebDriver d) {
            JavascriptExecutor jsExec = (JavascriptExecutor) d;
            return (Boolean) jsExec.executeScript(JQUERY_ACTIVE_CONNECTIONS_QUERY);
         }
   });
}

不幸的是这code不工作PrimeFaces 4.0,jQuery的联系似乎从未被激活。

Unfortunately this code does not work in PrimeFaces 4.0, the jQuery connections never seem to be active.

所以,问题是:我怎么等待PrimeFaces Ajax请求在4.0版本的处理

So the question is: how do I wait for PrimeFaces ajax requests to process in version 4.0?

推荐答案

PrimeFaces 4.0使用自己的AJAX事件处理程序,您可以使用下面的code:

PrimeFaces 4.0 uses its own ajax event handler, you can use the following code:

private static final String JS_JQUERY_DEFINED = "return typeof jQuery != 'undefined';";
private static final String JS_PRIMEFACES_DEFINED = "return typeof PrimeFaces != 'undefined';";
private static final String JS_JQUERY_ACTIVE = "return jQuery.active != 0;";
private static final String JS_PRIMEFACES_QUEUE_NOT_EMPTY = "return !PrimeFaces.ajax.Queue.isEmpty();";

private static final int TIME_OUT_SECONDS=10;
private static final int POLLING_MILLISECONDS=500;

private void waitForJQueryAndPrimeFaces() {
   new FluentWait(driver).withTimeout(TIME_OUT_SECONDS, TimeUnit.SECONDS)
      .pollingEvery(POLLING_MILLISECONDS, TimeUnit.MILLISECONDS)
      .until(new Function < WebDriver, Boolean >() {
         @Override
         public Boolean apply(WebDriver input) {
            boolean ajax = false;
            boolean jQueryDefined = executeBooleanJavascript(input, JS_JQUERY_DEFINED);
            boolean primeFacesDefined = executeBooleanJavascript(input, JS_PRIMEFACES_DEFINED);

            if (jQueryDefined) {
               // jQuery is still active
               ajax |= executeBooleanJavascript(input, JS_JQUERY_ACTIVE);
            }
            if (primeFacesDefined) {
               // PrimeFaces queue isn't empty
               ajax |= executeBooleanJavascript(input, JS_PRIMEFACES_QUEUE_NOT_EMPTY);
            }

            // continue if all ajax request are processed
            return !ajax;
         }
      });
}

private boolean executeBooleanJavascript(WebDriver input, String javascript) {
   return (Boolean) ((JavascriptExecutor) input).executeScript(javascript);
}

这篇关于硒等待PrimeFaces 4.0 AJAX工艺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆