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