001/*
002 * (C) Copyright 2013 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.fragment;
020
021import java.util.Arrays;
022import java.util.List;
023import java.util.concurrent.TimeUnit;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.functionaltests.AbstractTest;
028import org.openqa.selenium.By;
029import org.openqa.selenium.NoSuchElementException;
030import org.openqa.selenium.WebDriver;
031import org.openqa.selenium.WebElement;
032import org.openqa.selenium.support.ui.FluentWait;
033import org.openqa.selenium.support.ui.Wait;
034
035import com.google.common.base.Function;
036
037/**
038 * Fragment representing the gadgets container.
039 *
040 * @since 5.7.3
041 */
042public class GadgetsContainerFragment extends WebFragmentImpl {
043
044    private static final Log log = LogFactory.getLog(GadgetsContainerFragment.class);
045
046    public static final String GADGETS_CONTAINER_ID = "gwtContainerDiv";
047
048    public GadgetsContainerFragment(WebDriver driver, WebElement element) {
049        super(driver, element);
050    }
051
052    public WebElement waitForGadgetsLoad() {
053        return waitForGadgetsLoad("nxDocumentListData,content");
054    }
055
056    public WebElement waitForGadgetsLoad(final String mandatoryElements) {
057        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(AbstractTest.LOAD_TIMEOUT_SECONDS,
058                TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
059        return wait.until(new Function<WebDriver, WebElement>() {
060            @Override
061            public WebElement apply(WebDriver driver) {
062                WebElement container = getElement();
063                // iterate through all frames, and ensure opensocial ones are
064                // loaded, and expect at least one opensocial frame
065                boolean oneFound = false;
066                List<WebElement> framesList = driver.findElements(By.xpath("//iframe"));
067                if (framesList != null && !framesList.isEmpty()) {
068                    List<String> mandatory = Arrays.asList(mandatoryElements.split(","));
069                    for (WebElement frame : framesList) {
070                        String frameName = frame.getAttribute("name");
071                        if (frameName == null || !frameName.startsWith("open-social")) {
072                            continue;
073                        }
074                        log.debug(String.format("Found one GWT gadget frame named '%s' ", frameName));
075                        oneFound = true;
076                        boolean loaded = false;
077                        driver.switchTo().defaultContent();
078                        driver.switchTo().frame(frame);
079                        for (String mand : mandatory) {
080                            try {
081                                driver.findElement(By.id(mand));
082                                loaded = true;
083                                log.debug(String.format("Gadget frame '%s' mandatory element '%s' loaded", frameName,
084                                        mand));
085                            } catch (NoSuchElementException e) {
086                                loaded = false;
087                                log.debug(String.format("Gadget frame '%s' not loaded yet, "
088                                        + "mandatory element '%s' not found", frameName, mand));
089                                break;
090                            }
091                        }
092                        if (!loaded) {
093                            log.debug(String.format("Gadget frame '%s' not loaded yet", frameName));
094                            driver.switchTo().defaultContent();
095                            return null;
096                        }
097                        log.debug(String.format("Gadget frame '%s' loaded", frameName));
098                        driver.switchTo().defaultContent();
099                    }
100                }
101                if (oneFound) {
102                    return container;
103                }
104                log.debug("No gadget frame loaded yet");
105                return null;
106            }
107        });
108    }
109
110    public boolean isGadgetLoaded(String gadgetTitle) {
111        return getGadgetTitleElement(gadgetTitle) != null;
112    }
113
114    public WebElement getGadgetTitleElement(String gadgetTitle) {
115        WebElement gtwContainer = waitForGadgetsLoad();
116        List<WebElement> gadgets = gtwContainer.findElements(By.className("dragdrop-draggable"));
117        for (WebElement gadget : gadgets) {
118            WebElement title = gadget.findElement(By.className("header"));
119            if (title.getText().contains(gadgetTitle)) {
120                return title;
121            }
122        }
123        throw new NoSuchElementException(gadgetTitle);
124    }
125
126    public boolean isTaskGadgetEmpty(String gadgetTitle) {
127        WebDriver driver = switchToFrame(gadgetTitle);
128        boolean res = driver.findElement(By.id("nxDocumentListData")).getText().contains(
129                "Your dashboard is empty. There are no tasks that require your intervention.");
130        // switch back to parent page after that
131        driver.switchTo().defaultContent();
132        return res;
133    }
134
135    public WebDriver switchToFrame(String gadgetTitle) {
136        WebElement title = getGadgetTitleElement(gadgetTitle);
137        WebElement parent = title.findElement(By.xpath("parent::*"));
138        driver.switchTo().defaultContent();
139        return driver.switchTo().frame("open-social-" + parent.getAttribute("id"));
140    }
141
142}