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.admincenter;
020
021import org.openqa.selenium.By;
022import org.openqa.selenium.WebDriver;
023import org.openqa.selenium.WebElement;
024
025/**
026 * Wizard and Connect use frames and callback pages to communicate. So focusing the right frame can be tricky because,
027 * for example we never want to do any test on the callback pages.
028 *
029 * @author Tiry (tdelprat@nuxeo.com)
030 */
031public class IFrameHelper {
032
033    public static final String WE_IFRAME_URL_PATTERN = "/site/connectClient";
034
035    public static final int NBTRY = 10;
036
037    public static boolean focusOnWEIFrame(WebDriver driver) {
038        return focusOnWEIFrame(driver, NBTRY);
039    }
040
041    protected static void wait(int nbSeconds) {
042        try {
043            Thread.sleep(nbSeconds * 1000);
044        } catch (InterruptedException e) {
045            Thread.currentThread().interrupt();
046            throw new RuntimeException(e);
047        }
048    }
049
050    private static boolean focusOnWEIFrame(WebDriver driver, int nbTry) {
051
052        if (!driver.getCurrentUrl().contains(WE_IFRAME_URL_PATTERN)) {
053            if (nbTry > 0) {
054                try {
055                    WebElement iFrame = driver.findElement(By.id("connectIframe"));
056                    if (iFrame != null) {
057                        driver.switchTo().frame(iFrame);
058                        return true;
059                    } else {
060                        wait(2);
061                        return focusOnWEIFrame(driver, nbTry - 1);
062                    }
063                } catch (Throwable e) {
064                    System.out.println("Retry to find IFrame on page " + driver.getCurrentUrl());
065                    wait(2);
066                    return focusOnWEIFrame(driver, nbTry - 1);
067                }
068            } else {
069                return false;
070            }
071        } else {
072            return true;
073        }
074    }
075
076}