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.After;
020 import org.junit.AfterClass;
021 import org.junit.Before;
022 import org.junit.BeforeClass;
023 import org.openqa.selenium.By;
024 import org.openqa.selenium.WebDriver;
025 import org.openqa.selenium.chrome.ChromeDriver;
026 import org.openqa.selenium.chrome.ChromeDriverService;
027 import org.openqa.selenium.firefox.FirefoxDriver;
028 import org.openqa.selenium.firefox.FirefoxProfile;
029
030 import java.io.File;
031 import java.util.concurrent.TimeUnit;
032
033 import static org.junit.Assert.assertEquals;
034
035 /**
036 * Base class for Selenium Webdriver integration tests
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040 public abstract class WebDriverITBase {
041
042 int DEFAULT_IMPLICIT_WAIT_TIME = 30;
043 int SHORT_IMPLICIT_WAIT_TIME = 1;
044
045 public WebDriver driver;
046 static ChromeDriverService chromeDriverService;
047
048 /**
049 * Returns the URL to be used with this test
050 *
051 * @return URL of the test
052 */
053 public abstract String getTestUrl();
054
055 @BeforeClass
056 public static void createAndStartService() throws Exception {
057 String driverParam = System.getProperty("remote.public.driver");
058 if (driverParam != null && "chrome".equals(driverParam.toLowerCase())) {
059 if (System.getProperty("webdriver.chrome.driver") == null) {
060 if (System.getProperty("remote.public.chrome") != null) {
061 System.setProperty("webdriver.chrome.driver", System.getProperty("remote.public.chrome"));
062 }
063 }
064 ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
065 .usingChromeDriverExecutable(new File(System.getProperty("remote.public.chrome")))
066 .usingAnyFreePort()
067 .build();
068 chromeDriverService.start();
069 }
070 }
071
072
073 /**
074 * Setup the WebDriver test, login and load the tested web page
075 *
076 * @throws Exception
077 */
078 @Before
079 public void setUp() throws Exception {
080 driver = getWebDriver();
081 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
082
083 // Login
084 driver.get(getBaseUrlString() + getTestUrl());
085 driver.findElement(By.name("__login_user")).clear();
086 driver.findElement(By.name("__login_user")).sendKeys("admin");
087 driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
088 }
089
090 /**
091 * Tear down the WebDriver test
092 *
093 * @throws Exception
094 */
095 @After
096 public void tearDown() throws Exception {
097 if (System.getProperty("remote.driver.dontTearDown") == null ||
098 !"f".startsWith(System.getProperty("remote.driver.dontTearDown").toLowerCase()) ||
099 !"n".startsWith(System.getProperty("remote.driver.dontTearDown").toLowerCase())) {
100 driver.quit(); // TODO not tested with chrome, the service stop might need this check too
101 }
102 }
103
104 /**
105 * Tear down the WebDriver test
106 *
107 * @throws Exception
108 */
109 @AfterClass
110 public static void stopService() throws Exception {
111 if (chromeDriverService != null) {
112 chromeDriverService.stop();
113 }
114 }
115
116 /**
117 * Check if an element is present
118 *
119 * <p>
120 * This test takes a while due to the 'implicit wait' time.
121 * </p>
122 *
123 * @param by The locating mechanism of the element
124 * @return true if the element is present, false otherwise
125 */
126 public boolean isElementPresent(By by) {
127 if (driver.findElements(by).isEmpty()) {
128 return false;
129 } else {
130 return true;
131 }
132 }
133
134 /**
135 * Quickly check if an element is present
136 *
137 * <p>
138 * Just like {@link #isElementPresent(org.openqa.selenium.By)} but with a short 'implicit wait' time. Use this only
139 * if it is guaranteed that all elements are rendered.
140 * </p>
141 *
142 * @param by The locating mechanism of the element
143 * @return true if the element is present, false otherwise
144 */
145 public boolean isElementPresentQuick(By by) {
146 driver.manage().timeouts().implicitlyWait(SHORT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
147 boolean result = isElementPresent(by);
148 driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
149 return result;
150 }
151
152 /**
153 * Assert that clicking an element causes a popup window with a specific URL
154 *
155 * @param by The locating mechanism of the element to be clicked
156 * @param windowName The name of the popup window
157 * @param url The URL of the popup window
158 */
159 public void assertPopUpWindowUrl(By by, String windowName, String url) {
160 driver.findElement(by).click();
161 String parentWindowHandle = driver.getWindowHandle();
162 // wait page to be loaded
163 driver.switchTo().window(windowName).findElements(By.tagName("head"));
164 assertEquals(url, driver.getCurrentUrl());
165 driver.switchTo().window(parentWindowHandle);
166 }
167
168 public static String getBaseUrlString() {
169 String baseUrl = System.getProperty("remote.public.url");
170 if (baseUrl == null) {
171 baseUrl = "http://localhost:8080";
172 } else if (baseUrl.endsWith("/")) {
173 baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
174 } else if (!baseUrl.startsWith("http")) {
175 baseUrl = "http://" + baseUrl;
176 }
177 return baseUrl;
178 }
179
180 public WebDriver getWebDriver() {
181 String driverParam = System.getProperty("remote.public.driver");
182 if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) {
183 FirefoxProfile profile = new FirefoxProfile();
184 profile.setEnableNativeEvents(false);
185 return new FirefoxDriver(profile);
186 } else if ("chrome".equalsIgnoreCase(driverParam)) {
187 return new ChromeDriver();
188 }
189 return null;
190 }
191 }
192