001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 */
017package org.nuxeo.functionaltests;
018
019import java.lang.reflect.Field;
020
021import org.openqa.selenium.WebDriver;
022import org.openqa.selenium.support.pagefactory.AjaxElementLocator;
023import org.openqa.selenium.support.pagefactory.DefaultElementLocator;
024import org.openqa.selenium.support.pagefactory.ElementLocator;
025import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
026
027/**
028 * Element locator factory that creates normal or time-delayed locators depending on the presence of the
029 * {@link SlowLoading} field annotation.
030 */
031public class VariableElementLocatorFactory implements ElementLocatorFactory {
032
033    protected final WebDriver driver;
034
035    protected final int timeOutInSeconds;
036
037    public VariableElementLocatorFactory(WebDriver driver, int timeOutInSeconds) {
038        this.driver = driver;
039        this.timeOutInSeconds = timeOutInSeconds;
040    }
041
042    @Override
043    public ElementLocator createLocator(Field field) {
044        if (field.getAnnotation(SlowLoading.class) != null) {
045            return new AjaxElementLocator(driver, field, timeOutInSeconds);
046        } else {
047            return new DefaultElementLocator(driver, field);
048        }
049    }
050
051}