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 org.openqa.selenium.JavascriptExecutor; 022import org.openqa.selenium.NoSuchElementException; 023import org.openqa.selenium.WebDriver; 024 025import com.google.common.base.Function; 026 027/** 028 * @since 6.0 029 */ 030public class AjaxRequestManager { 031 032 protected JavascriptExecutor js; 033 034 protected boolean active; 035 036 protected int count; 037 038 public AjaxRequestManager(WebDriver driver) { 039 super(); 040 js = (JavascriptExecutor) driver; 041 reset(); 042 } 043 044 protected void reset() { 045 active = false; 046 count = 0; 047 } 048 049 /** 050 * @since 7.2 051 */ 052 public void begin() { 053 StringBuilder sb = new StringBuilder(); 054 sb.append("if (window.ajaxListenerSet === undefined) {"); 055 sb.append("window.ajaxListenerSet = true;"); 056 sb.append("window.NuxeoTestFaces = function() {"); 057 sb.append(" var e = {};"); 058 sb.append(" e.jsf2AjaxRequestStarted = false;"); 059 sb.append(" e.jsf2AjaxRequestFinished = false;"); 060 sb.append(" e.jsf2AjaxRequestActiveCount = 0;"); 061 sb.append(" e.increment = function() {"); 062 sb.append(" e.jsf2AjaxRequestStarted = true;"); 063 sb.append(" e.jsf2AjaxRequestFinished = false;"); 064 sb.append(" e.jsf2AjaxRequestActiveCount++;"); 065 sb.append(" };"); 066 sb.append(" e.decrement = function() {"); 067 sb.append(" e.jsf2AjaxRequestActiveCount--;"); 068 sb.append(" if (e.jsf2AjaxRequestActiveCount == 0) {"); 069 sb.append(" e.jsf2AjaxRequestFinished = true;"); 070 sb.append(" }"); 071 sb.append(" };"); 072 sb.append(" e.finished = function() {"); 073 sb.append(" return e.jsf2AjaxRequestStarted && e.jsf2AjaxRequestFinished;"); 074 sb.append(" };"); 075 sb.append(" return e"); 076 sb.append("}();"); 077 sb.append("if (typeof jsf !== 'undefined') {"); 078 sb.append( 079 " jsf.ajax.addOnEvent(function(e) {" + "if (e.status == 'begin') {window.NuxeoTestFaces.increment();}" 080 + "if (e.status == 'success') {window.NuxeoTestFaces.decrement();}" + "})"); 081 sb.append("}"); 082 sb.append("}"); 083 js.executeScript(sb.toString()); 084 } 085 086 public void watchAjaxRequests() { 087 begin(); 088 } 089 090 /** 091 * @since 7.2 092 */ 093 public void end() { 094 waitUntil((new Function<WebDriver, Boolean>() { 095 @Override 096 public Boolean apply(WebDriver driver) { 097 Boolean res = (Boolean) js.executeScript("return window.NuxeoTestFaces.finished();"); 098 return res; 099 } 100 })); 101 } 102 103 public void waitForAjaxRequests() { 104 end(); 105 } 106 107 /** 108 * @since 7.1 109 */ 110 public void waitForJQueryRequests() { 111 waitUntil(new Function<WebDriver, Boolean>() { 112 @Override 113 public Boolean apply(WebDriver driver) { 114 Boolean res = (Boolean) ((JavascriptExecutor) driver).executeScript("return jQuery.active == 0;"); 115 return res; 116 } 117 }); 118 } 119 120 /** 121 * Wait for any pending request of the javascript client. Compatible with client 0.24.0 or greater. 122 * 123 * @since 8.2 124 */ 125 public void waitForJsClient() { 126 waitUntil(new Function<WebDriver, Boolean>() { 127 @Override 128 public Boolean apply(WebDriver driver) { 129 final StringBuilder sb = new StringBuilder(); 130 sb.append("if (window.isNxJsClientActive === undefined) {"); 131 sb.append(" window.isNxJsClientActive=function() {"); 132 sb.append(" var result=false;"); 133 sb.append(" jQuery('nuxeo-connection').each(function() {"); 134 sb.append(" if(this.active){return result=true;}"); 135 sb.append(" });"); 136 sb.append(" return result;"); 137 sb.append(" }"); 138 sb.append("};"); 139 sb.append("return window.isNxJsClientActive();"); 140 Boolean res = (Boolean) ((JavascriptExecutor) driver).executeScript(sb.toString()); 141 return !res; 142 } 143 }); 144 } 145 146 private void waitUntil(Function<WebDriver, Boolean> function) { 147 Locator.waitUntilGivenFunctionIgnoring(function, NoSuchElementException.class); 148 } 149}