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        }
046    }
047
048    private static boolean focusOnWEIFrame(WebDriver driver, int nbTry) {
049
050        if (!driver.getCurrentUrl().contains(WE_IFRAME_URL_PATTERN)) {
051            if (nbTry > 0) {
052                try {
053                    WebElement iFrame = driver.findElement(By.id("connectIframe"));
054                    if (iFrame != null) {
055                        driver.switchTo().frame(iFrame);
056                        return true;
057                    } else {
058                        wait(2);
059                        return focusOnWEIFrame(driver, nbTry - 1);
060                    }
061                } catch (Throwable e) {
062                    System.out.println("Retry to find IFrame on page " + driver.getCurrentUrl());
063                    wait(2);
064                    return focusOnWEIFrame(driver, nbTry - 1);
065                }
066            } else {
067                return false;
068            }
069        } else {
070            return true;
071        }
072    }
073
074}