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.Before;
021 import org.openqa.selenium.By;
022 import org.openqa.selenium.WebDriver;
023 import org.openqa.selenium.firefox.FirefoxDriver;
024 import org.openqa.selenium.firefox.FirefoxProfile;
025
026 import java.util.concurrent.TimeUnit;
027
028 import static org.junit.Assert.assertEquals;
029
030 /**
031 * Base class for Selenium Webdriver integration tests
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035 public abstract class WebDriverITBase {
036
037 int DEFAULT_IMPLICIT_WAIT_TIME = 30;
038 int SHORT_IMPLICIT_WAIT_TIME = 1;
039
040 public WebDriver driver;
041
042 /**
043 * Returns the URL to be used with this test
044 *
045 * @return URL of the test
046 */
047 public abstract String getTestUrl();
048
049 /**
050 * Setup the WebDriver test, login and load the tested web page
051 *
052 * @throws Exception
053 */
054 @Before
055 public void setUp() throws Exception {
056 FirefoxProfile profile = new FirefoxProfile();
057 profile.setEnableNativeEvents(false);
058 driver = new FirefoxDriver(profile);
059 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
060
061 // Login
062 driver.get(getTestUrl());
063 driver.findElement(By.name("__login_user")).clear();
064 driver.findElement(By.name("__login_user")).sendKeys("admin");
065 driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
066 }
067
068 /**
069 * Tear down the WebDriver test
070 *
071 * @throws Exception
072 */
073 @After
074 public void tearDown() throws Exception {
075 driver.quit();
076 }
077
078 /**
079 * Check if an element is present
080 *
081 * <p>
082 * This test takes a while due to the 'implicit wait' time.
083 * </p>
084 *
085 * @param by The locating mechanism of the element
086 * @return true if the element is present, false otherwise
087 */
088 public boolean isElementPresent(By by) {
089 if (driver.findElements(by).isEmpty()) {
090 return false;
091 } else {
092 return true;
093 }
094 }
095
096 /**
097 * Quickly check if an element is present
098 *
099 * <p>
100 * Just like {@link #isElementPresent(org.openqa.selenium.By)} but with a short 'implicit wait' time. Use this only
101 * if it is guaranteed that all elements are rendered.
102 * </p>
103 *
104 * @param by The locating mechanism of the element
105 * @return true if the element is present, false otherwise
106 */
107 public boolean isElementPresentQuick(By by) {
108 driver.manage().timeouts().implicitlyWait(SHORT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
109 boolean result = isElementPresent(by);
110 driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
111 return result;
112 }
113
114 /**
115 * Assert that clicking an element causes a popup window with a specific URL
116 *
117 * @param by The locating mechanism of the element to be clicked
118 * @param windowName The name of the popup window
119 * @param url The URL of the popup window
120 */
121 public void assertPopUpWindowUrl(By by, String windowName, String url) {
122 driver.findElement(by).click();
123 String parentWindowHandle = driver.getWindowHandle();
124 // wait page to be loaded
125 driver.switchTo().window(windowName).findElements(By.tagName("head"));
126 assertEquals(url, driver.getCurrentUrl());
127 driver.switchTo().window(parentWindowHandle);
128 }
129
130 }