001/* 002 * (C) Copyright 2015 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 7.2 033 */ 034public class JSListRequestManager { 035 036 protected JavascriptExecutor js; 037 038 protected boolean active; 039 040 protected int count; 041 042 public JSListRequestManager(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 public void begin() { 054 StringBuilder sb = new StringBuilder(); 055 sb.append("if (window.NuxeoTestJsList === undefined) {"); 056 sb.append(" window.NuxeoTestJsList = function() {"); 057 sb.append(" var e = {};"); 058 sb.append(" e.jsRequestStarted = false;"); 059 sb.append(" e.jsRequestFinished = false;"); 060 sb.append(" e.jsRequestActiveCount = 0;"); 061 sb.append(" e.increment = function() {"); 062 sb.append(" e.jsRequestStarted = true;"); 063 sb.append(" e.jsRequestFinished = false;"); 064 sb.append(" e.jsRequestActiveCount++;"); 065 sb.append(" };"); 066 sb.append(" e.decrement = function() {"); 067 sb.append(" e.jsRequestActiveCount--;"); 068 sb.append(" if (e.jsRequestActiveCount == 0) {"); 069 sb.append(" e.jsRequestFinished = true;"); 070 sb.append(" }"); 071 sb.append(" };"); 072 sb.append(" e.finished = function() {"); 073 sb.append(" return e.jsRequestStarted && e.jsRequestFinished;"); 074 sb.append(" };"); 075 sb.append(" return e"); 076 sb.append(" }();"); 077 sb.append("}"); 078 sb.append(" nuxeo.utils.addOnEvent(function(data) {"); 079 sb.append(" if (data.status == 'begin') {window.NuxeoTestJsList.increment();}"); 080 sb.append(" if (data.status == 'success') {window.NuxeoTestJsList.decrement();}"); 081 sb.append(" });"); 082 js.executeScript(sb.toString()); 083 } 084 085 public void end() { 086 waitUntil((new Function<WebDriver, Boolean>() { 087 @Override 088 public Boolean apply(WebDriver driver) { 089 Boolean res = (Boolean) js.executeScript("return window.NuxeoTestJsList.finished();"); 090 return res; 091 } 092 })); 093 } 094 095 private void waitUntil(Function<WebDriver, Boolean> function) { 096 Wait<WebDriver> wait = new FluentWait<WebDriver>(AbstractTest.driver).withTimeout( 097 AbstractTest.LOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS).pollingEvery( 098 AbstractTest.POLLING_FREQUENCY_MILLISECONDS, TimeUnit.MILLISECONDS).ignoring( 099 NoSuchElementException.class); 100 wait.until(function); 101 } 102 103}