001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Anahide Tchertchian
016 */
017package org.nuxeo.functionaltests.fragment;
018
019import java.util.Arrays;
020import java.util.List;
021import java.util.concurrent.TimeUnit;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.functionaltests.AbstractTest;
026import org.openqa.selenium.By;
027import org.openqa.selenium.NoSuchElementException;
028import org.openqa.selenium.WebDriver;
029import org.openqa.selenium.WebElement;
030import org.openqa.selenium.support.ui.FluentWait;
031import org.openqa.selenium.support.ui.Wait;
032
033import com.google.common.base.Function;
034
035/**
036 * Fragment representing the gadgets container.
037 *
038 * @since 5.7.3
039 */
040public class GadgetsContainerFragment extends WebFragmentImpl {
041
042    private static final Log log = LogFactory.getLog(GadgetsContainerFragment.class);
043
044    public static final String GADGETS_CONTAINER_ID = "gwtContainerDiv";
045
046    public GadgetsContainerFragment(WebDriver driver, WebElement element) {
047        super(driver, element);
048    }
049
050    public WebElement waitForGadgetsLoad() {
051        return waitForGadgetsLoad("nxDocumentListData,content");
052    }
053
054    public WebElement waitForGadgetsLoad(final String mandatoryElements) {
055        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(AbstractTest.LOAD_TIMEOUT_SECONDS,
056                TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
057        return wait.until(new Function<WebDriver, WebElement>() {
058            @Override
059            public WebElement apply(WebDriver driver) {
060                WebElement container = getElement();
061                // iterate through all frames, and ensure opensocial ones are
062                // loaded, and expect at least one opensocial frame
063                boolean oneFound = false;
064                List<WebElement> framesList = driver.findElements(By.xpath("//iframe"));
065                if (framesList != null && !framesList.isEmpty()) {
066                    List<String> mandatory = Arrays.asList(mandatoryElements.split(","));
067                    for (WebElement frame : framesList) {
068                        String frameName = frame.getAttribute("name");
069                        if (frameName == null || !frameName.startsWith("open-social")) {
070                            continue;
071                        }
072                        log.debug(String.format("Found one GWT gadget frame named '%s' ", frameName));
073                        oneFound = true;
074                        boolean loaded = false;
075                        driver.switchTo().defaultContent();
076                        driver.switchTo().frame(frame);
077                        for (String mand : mandatory) {
078                            try {
079                                driver.findElement(By.id(mand));
080                                loaded = true;
081                                log.debug(String.format("Gadget frame '%s' mandatory element '%s' loaded", frameName,
082                                        mand));
083                            } catch (NoSuchElementException e) {
084                                loaded = false;
085                                log.debug(String.format("Gadget frame '%s' not loaded yet, "
086                                        + "mandatory element '%s' not found", frameName, mand));
087                                break;
088                            }
089                        }
090                        if (!loaded) {
091                            log.debug(String.format("Gadget frame '%s' not loaded yet", frameName));
092                            driver.switchTo().defaultContent();
093                            return null;
094                        }
095                        log.debug(String.format("Gadget frame '%s' loaded", frameName));
096                        driver.switchTo().defaultContent();
097                    }
098                }
099                if (oneFound) {
100                    return container;
101                }
102                log.debug("No gadget frame loaded yet");
103                return null;
104            }
105        });
106    }
107
108    public boolean isGadgetLoaded(String gadgetTitle) {
109        return getGadgetTitleElement(gadgetTitle) != null;
110    }
111
112    public WebElement getGadgetTitleElement(String gadgetTitle) {
113        WebElement gtwContainer = waitForGadgetsLoad();
114        List<WebElement> gadgets = gtwContainer.findElements(By.className("dragdrop-draggable"));
115        for (WebElement gadget : gadgets) {
116            WebElement title = gadget.findElement(By.className("header"));
117            if (title.getText().contains(gadgetTitle)) {
118                return title;
119            }
120        }
121        throw new NoSuchElementException(gadgetTitle);
122    }
123
124    public boolean isTaskGadgetEmpty(String gadgetTitle) {
125        WebDriver driver = switchToFrame(gadgetTitle);
126        boolean res = driver.findElement(By.id("nxDocumentListData")).getText().contains(
127                "Your dashboard is empty. There are no tasks that require your intervention.");
128        // switch back to parent page after that
129        driver.switchTo().defaultContent();
130        return res;
131    }
132
133    public WebDriver switchToFrame(String gadgetTitle) {
134        WebElement title = getGadgetTitleElement(gadgetTitle);
135        WebElement parent = title.findElement(By.xpath("parent::*"));
136        driver.switchTo().defaultContent();
137        return driver.switchTo().frame("open-social-" + parent.getAttribute("id"));
138    }
139
140}