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 *     bstefanescu
018 */
019package org.nuxeo.runtime.test.runner.web;
020
021import org.openqa.selenium.WebDriver;
022
023/**
024 * WebDriver test configuration that can be configured either from system
025 * properties or for annotations.
026 *
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class Configuration {
030
031    /**
032     * The current driver
033     */
034    protected WebDriver driver;
035
036    /**
037     * Custom factory to create the driver
038     */
039    protected DriverFactory factory;
040
041    /**
042     * Port to use for parameter replacement in the home URL.
043     *
044     * @since 10.10
045     */
046    protected int port;
047
048    /**
049     * Initial URL (the one to be used by the home page)
050     */
051    protected String home;
052
053    /**
054     * The home page class
055     */
056    protected Class<? extends WebPage> homePageClass;
057
058    public Configuration(DriverFactory factory) {
059        this.factory = factory;
060    }
061
062    protected WebDriver createDriver() {
063        return factory.createDriver();
064    }
065
066    protected void disposeDriver(WebDriver driver) {
067        factory.disposeDriver(driver);
068    }
069
070    public BrowserFamily getBrowserFamily() {
071        return factory.getBrowserFamily();
072    }
073
074    public void setFactory(DriverFactory factory) {
075        resetDriver();
076        this.factory = factory;
077    }
078
079    public DriverFactory getFactory() {
080        return factory;
081    }
082
083    protected static final String PORT_PLACEHOLDER = "${PORT}";
084
085    /**
086     * Sets the port to use for parameter replacement in the home URL.
087     *
088     * @since 10.10
089     */
090    public void setPort(int port) {
091        this.port = port;
092    }
093
094    public void setHome(String url) {
095        home = url;
096    }
097
098    public String getHome() {
099        String url = home;
100        if (url != null && url.contains(PORT_PLACEHOLDER)) {
101            url = url.replace(PORT_PLACEHOLDER, String.valueOf(port));
102        }
103        return url;
104    }
105
106    public void setHomePageClass(Class<? extends WebPage> homePageClass) {
107        this.homePageClass = homePageClass;
108    }
109
110    public Class<? extends WebPage> getHomePageClass() {
111        return homePageClass;
112    }
113
114    public void home() {
115        if (getHome() != null && driver != null) {
116            driver.get(getHome());
117        }
118    }
119
120    public WebDriver getDriver() {
121        if (driver == null) {
122            driver = createDriver();
123            home();
124        }
125        return driver;
126    }
127
128
129    public void resetDriver() {
130        if (driver != null) {
131            driver.quit();
132            disposeDriver(driver);
133            driver = null;
134        }
135    }
136
137}