001/*
002 * (C) Copyright 2011 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 *     Thierry Delprat
018 */
019package org.nuxeo.functionaltests.pages.wizard;
020
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.nuxeo.functionaltests.Locator;
027import org.openqa.selenium.By;
028import org.openqa.selenium.NoSuchFrameException;
029import org.openqa.selenium.StaleElementReferenceException;
030import org.openqa.selenium.TimeoutException;
031import org.openqa.selenium.WebDriver;
032import org.openqa.selenium.WebElement;
033import org.openqa.selenium.support.ui.FluentWait;
034import org.openqa.selenium.support.ui.Wait;
035
036import com.google.common.base.Function;
037
038/**
039 * Wizard and Connect use frames and callback pages to communicate. So focusing the right frame can be tricky because,
040 * for example we never want to do any test on the callback pages.
041 *
042 * @author Tiry (tdelprat@nuxeo.com)
043 */
044public class IFrameHelper {
045
046    protected static final Log log = LogFactory.getLog(IFrameHelper.class);
047
048    public static final String CONNECT_IFRAME_URL_PATTERN = "/register/";
049
050    public static final String CALLBACK_URL_PATTERN = "ConnectCallback";
051
052    public static final String CONNECT_FRAME_NAME = "connectForm";
053
054    private static void switchToIFrame(final WebDriver driver, final WebElement iframe) {
055        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(AbstractTest.LOAD_TIMEOUT_SECONDS,
056                TimeUnit.SECONDS).pollingEvery(AbstractTest.POLLING_FREQUENCY_MILLISECONDS, TimeUnit.MILLISECONDS).ignoring(
057                NoSuchFrameException.class, StaleElementReferenceException.class);
058        wait.until(new Function<WebDriver, Boolean>() {
059            @Override
060            public Boolean apply(WebDriver driver) {
061                if (iframe == null) {
062                    return driver.switchTo().defaultContent() != null;
063                } else {
064                    return driver.switchTo().frame(iframe) != null;
065                }
066            };
067        });
068    }
069
070    public static boolean focusOnConnectFrame(WebDriver driver) {
071        if (!driver.getCurrentUrl().contains(CONNECT_IFRAME_URL_PATTERN)) {
072            try {
073                WebElement connectFormIFrame = Locator.findElementWithTimeout(By.id(CONNECT_FRAME_NAME));
074                switchToIFrame(driver, connectFormIFrame);
075            } catch (TimeoutException e) {
076                log.error("Unable to find IFrame on page " + driver.getCurrentUrl());
077                return false;
078            }
079            return true;
080        }
081        return false;
082    }
083
084    public static boolean focusOnWizardPage(WebDriver driver) {
085
086        Locator.waitUntilURLNotContain(CALLBACK_URL_PATTERN);
087
088        // if we're coming from an iframe, driver.getCurrentUrl() can be empty
089        // switch back to main frame without testing the URL
090        try {
091            switchToIFrame(driver, null);
092            return true;
093        } catch (TimeoutException e) {
094            log.error("Unable to find top windows on page " + driver.getCurrentUrl());
095            return false;
096        }
097    }
098}