001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.functionaltests;
020
021import java.util.concurrent.TimeUnit;
022
023import org.openqa.selenium.JavascriptExecutor;
024import org.openqa.selenium.NoSuchElementException;
025import org.openqa.selenium.WebDriver;
026import org.openqa.selenium.support.ui.FluentWait;
027import org.openqa.selenium.support.ui.Wait;
028
029import com.google.common.base.Function;
030
031/**
032 * @since 6.0
033 */
034public class AjaxRequestManager {
035
036    protected JavascriptExecutor js;
037
038    protected boolean active;
039
040    protected int count;
041
042    public AjaxRequestManager(WebDriver driver) {
043        super();
044        js = (JavascriptExecutor) driver;
045        reset();
046    }
047
048    protected void reset() {
049        active = false;
050        count = 0;
051    }
052
053    /**
054     * @since 7.2
055     */
056    public void begin() {
057        StringBuilder sb = new StringBuilder();
058        sb.append("if (window.ajaxListenerSet === undefined) {");
059        sb.append("window.ajaxListenerSet = true;");
060        sb.append("window.NuxeoTestFaces = function() {");
061        sb.append("  var e = {};");
062        sb.append("  e.jsf2AjaxRequestStarted = false;");
063        sb.append("  e.jsf2AjaxRequestFinished = false;");
064        sb.append("  e.jsf2AjaxRequestActiveCount = 0;");
065        sb.append("  e.increment = function() {");
066        sb.append("    e.jsf2AjaxRequestStarted = true;");
067        sb.append("    e.jsf2AjaxRequestFinished = false;");
068        sb.append("    e.jsf2AjaxRequestActiveCount++;");
069        sb.append("  };");
070        sb.append("  e.decrement = function() {");
071        sb.append("    e.jsf2AjaxRequestActiveCount--;");
072        sb.append("    if (e.jsf2AjaxRequestActiveCount == 0) {");
073        sb.append("      e.jsf2AjaxRequestFinished = true;");
074        sb.append("    }");
075        sb.append("  };");
076        sb.append("  e.finished = function() {");
077        sb.append("    return e.jsf2AjaxRequestStarted && e.jsf2AjaxRequestFinished;");
078        sb.append("  };");
079        sb.append(" return e");
080        sb.append("}();");
081        sb.append("if (typeof jsf !== 'undefined') {");
082        sb.append("  jsf.ajax.addOnEvent(function(e) {"
083                + "if (e.status == 'begin') {window.NuxeoTestFaces.increment();}"
084                + "if (e.status == 'success') {window.NuxeoTestFaces.decrement();}" + "})");
085        sb.append("}");
086        sb.append("}");
087        js.executeScript(sb.toString());
088    }
089
090    public void watchAjaxRequests() {
091        begin();
092    }
093
094    /**
095     * @since 7.2
096     */
097    public void end() {
098        waitUntil((new Function<WebDriver, Boolean>() {
099            @Override
100            public Boolean apply(WebDriver driver) {
101                Boolean res = (Boolean) js.executeScript("return window.NuxeoTestFaces.finished();");
102                return res;
103            }
104        }));
105    }
106
107    public void waitForAjaxRequests() {
108        end();
109    }
110
111    /**
112     * @since 7.1
113     */
114    public void waitForJQueryRequests() {
115        waitUntil(new Function<WebDriver, Boolean>() {
116            @Override
117            public Boolean apply(WebDriver driver) {
118                Boolean res = (Boolean) ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0;");
119                return res;
120            }
121        });
122    }
123
124    /**
125     * Wait for any pending request of the javascript client. Compatible with client 0.24.0 or greater.
126     *
127     * @since 8.2
128     */
129    public void waitForJsClient() {
130        waitUntil(new Function<WebDriver, Boolean>() {
131            @Override
132            public Boolean apply(WebDriver driver) {
133                final StringBuilder sb = new StringBuilder();
134                sb.append("if (window.isNxJsClientActive === undefined) {");
135                sb.append("  window.isNxJsClientActive=function() {");
136                sb.append("    var result=false;");
137                sb.append("    jQuery('nuxeo-connection').each(function() {");
138                sb.append("      if(this.active){return result=true;}");
139                sb.append("    });");
140                sb.append("    return result;");
141                sb.append("  }");
142                sb.append("};");
143                sb.append("return window.isNxJsClientActive();");
144                Boolean res = (Boolean) ((JavascriptExecutor) driver).executeScript(sb.toString());
145                return !res;
146            }
147        });
148    }
149
150    private void waitUntil(Function<WebDriver, Boolean> function) {
151        Wait<WebDriver> wait = new FluentWait<WebDriver>(AbstractTest.driver).withTimeout(
152                AbstractTest.LOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS).pollingEvery(
153                AbstractTest.POLLING_FREQUENCY_MILLISECONDS, TimeUnit.MILLISECONDS).ignoring(
154                NoSuchElementException.class);
155        wait.until(function);
156    }
157}