001/*
002 * (C) Copyright 2006-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 *     Damien Metzler (Leroy Merlin, http://www.leroymerlin.fr/)
018 */
019package org.nuxeo.runtime.test.runner.web;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.junit.runner.Description;
024import org.junit.runner.manipulation.Filter;
025import org.junit.runner.manipulation.NoTestsRemainException;
026import org.nuxeo.runtime.test.runner.FeaturesRunner;
027import org.nuxeo.runtime.test.runner.SimpleFeature;
028import org.openqa.selenium.WebDriver;
029import com.google.inject.Binder;
030import com.google.inject.Provider;
031import com.google.inject.Scopes;
032
033public class WebDriverFeature extends SimpleFeature {
034
035    private static final Log log = LogFactory.getLog(WebDriverFeature.class);
036
037    protected Browser browser;
038
039    protected HomePage homepage;
040
041    protected Configuration config;
042
043    protected Class<? extends WebPage> home;
044
045    @Override
046    public void initialize(FeaturesRunner runner) throws Exception {
047        browser = runner.getConfig(Browser.class);
048        homepage = runner.getConfig(HomePage.class);
049        DriverFactory factory;
050        // test here if the driver factory is specified by environment
051        String fcName = System.getProperty(DriverFactory.class.getName());
052        if (fcName != null) {
053            factory = (DriverFactory) Class.forName(fcName).newInstance();
054        } else {
055            if (browser.factory() != DriverFactory.class) {
056                factory = browser.factory().newInstance();
057            } else {
058                factory = browser.type().getDriverFactory();
059            }
060        }
061        config = new Configuration(factory);
062        config.setHomePageClass(homepage.type());
063        // get the home page and the url - first check for an url from the
064        // environment
065        String url = System.getProperty(HomePage.class.getName() + ".url");
066        if (url == null) {
067            url = homepage.url();
068        }
069        config.setHome(url);
070        try {
071            runner.filter(new Filter() {
072                @Override
073                public boolean shouldRun(Description description) {
074                    SkipBrowser skip = description.getAnnotation(SkipBrowser.class);
075                    if (skip == null) {
076                        return true;
077                    }
078                    for (BrowserFamily family : skip.value()) {
079                        if (config.getBrowserFamily().equals(family)) {
080                            return false;
081                        }
082                    }
083                    return true;
084                }
085
086                @Override
087                public String describe() {
088                    return "Filtering tests according to current browser settings";
089                }
090            });
091        } catch (ClassCastException e) {
092            // OK - just skip
093        } catch (NoTestsRemainException e) {
094            log.error(e.toString(), e);
095        }
096    }
097
098    @SuppressWarnings("unchecked")
099    @Override
100    public void configure(final FeaturesRunner runner, Binder binder) {
101        binder.bind(Configuration.class).toInstance(config);
102        binder.bind(WebDriver.class).toProvider(new Provider<WebDriver>() {
103            @Override
104            public WebDriver get() {
105                return config.getDriver();
106            }
107        });
108        if (config.getHomePageClass() != null) {
109            binder.bind(config.getHomePageClass()).toProvider(new Provider() {
110                @Override
111                public Object get() {
112                    return WebPage.getPage(runner, config, config.getHomePageClass());
113                }
114            }).in(Scopes.SINGLETON);
115        }
116    }
117
118    @Override
119    public void stop(FeaturesRunner runner) throws Exception {
120        config.resetDriver();
121        WebPage.flushPageCache();
122    }
123
124}