001    /*
002     * Copyright 2006-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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    
017    package edu.samplu.common;
018    
019    import org.junit.rules.TestName;
020    import org.openqa.selenium.WebDriver;
021    import org.openqa.selenium.chrome.ChromeDriverService;
022    
023    import java.io.File;
024    import java.util.concurrent.TimeUnit;
025    
026    /**
027     * @author Kuali Rice Team (rice.collab@kuali.org)
028     */
029    
030    public class WebDriverUtil {
031    
032        public static int DEFAULT_IMPLICIT_WAIT_TIME = 30;
033        public static int SHORT_IMPLICIT_WAIT_TIME = 1;
034        public static final String REMOTE_DRIVER_SAUCELABS_PROPERTY = "remote.driver.saucelabs";
035    
036        /**
037         *
038         * @param username
039         * @param url
040         * @return
041         * @throws Exception
042         */
043        public static WebDriver setUp(String username, String url) throws Exception {
044            return setUp(username, url, null, null);
045        }
046    
047        /**
048         * Setup the WebDriver test, login and load the tested web page
049         *
050         * @throws Exception
051         */
052        public static WebDriver setUp(String username, String url, String className, TestName testName) throws Exception {
053            WebDriver driver = null;
054            if (System.getProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY) == null) {
055                driver = ITUtil.getWebDriver();
056    //        } else {
057    //            SauceLabsWebDriverHelper saucelabs = new SauceLabsWebDriverHelper();
058    //            saucelabs.setUp(className, testName);
059    //            driver = saucelabs.getDriver();
060            }
061            driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
062            driver.get(url);
063            driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
064            return driver;
065        }
066    
067        public static void checkForIncidentReport(WebDriver driver, String locator) {
068            checkForIncidentReport(driver, locator, "");
069        }
070    
071        public static void checkForIncidentReport(WebDriver driver, String locator, String message) {
072            ITUtil.checkForIncidentReport(driver.getPageSource(), locator, message);
073        }
074    
075        public static ChromeDriverService createAndStartService() {
076            String driverParam = System.getProperty(ITUtil.HUB_DRIVER_PROPERTY);
077            // TODO can the saucelabs driver stuff be leveraged here?
078            if (driverParam != null && "chrome".equals(driverParam.toLowerCase())) {
079                if (System.getProperty("webdriver.chrome.driver") == null) {
080                    if (System.getProperty("remote.public.chrome") != null) {
081                        System.setProperty("webdriver.chrome.driver", System.getProperty("remote.public.chrome"));
082                    }
083                }
084                ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
085                        .usingChromeDriverExecutable(new File(System.getProperty("remote.public.chrome")))
086                        .usingAnyFreePort()
087                        .build();
088                return chromeDriverService;
089            }
090            return null;
091        }
092    }