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