View Javadoc

1   /*
2    * Copyright 2006-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.samplu.common;
18  
19  import org.junit.After;
20  import org.junit.Before;
21  import org.openqa.selenium.By;
22  import org.openqa.selenium.WebDriver;
23  import org.openqa.selenium.firefox.FirefoxDriver;
24  import org.openqa.selenium.firefox.FirefoxProfile;
25  
26  import java.util.concurrent.TimeUnit;
27  
28  import static org.junit.Assert.assertEquals;
29  
30  /**
31   * Base class for Selenium Webdriver integration tests
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public abstract class WebDriverITBase {
36  
37      int DEFAULT_IMPLICIT_WAIT_TIME = 30;
38      int SHORT_IMPLICIT_WAIT_TIME = 1;
39  
40      public WebDriver driver;
41  
42      /**
43       * Returns the URL to be used with this test
44       *
45       * @return URL of the test
46       */
47      public abstract String getTestUrl();
48  
49      /**
50       * Setup the WebDriver test, login and load the tested web page
51       *
52       * @throws Exception
53       */
54      @Before
55      public void setUp() throws Exception {
56          FirefoxProfile profile = new FirefoxProfile();
57          profile.setEnableNativeEvents(false);
58          driver = new FirefoxDriver(profile);
59          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
60  
61          // Login
62          driver.get(getTestUrl());
63          driver.findElement(By.name("__login_user")).clear();
64          driver.findElement(By.name("__login_user")).sendKeys("admin");
65          driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
66      }
67  
68      /**
69       * Tear down the WebDriver test
70       *
71       * @throws Exception
72       */
73      @After
74      public void tearDown() throws Exception {
75          driver.quit();
76      }
77  
78      /**
79       * Check if an element is present
80       *
81       * <p>
82       * This test takes a while due to the 'implicit wait' time.
83       * </p>
84       *
85       * @param by The locating mechanism of the element
86       * @return true if the element is present, false otherwise
87       */
88      public boolean isElementPresent(By by) {
89          if (driver.findElements(by).isEmpty()) {
90              return false;
91          } else {
92              return true;
93          }
94      }
95  
96      /**
97       * Quickly check if an element is present
98       *
99       * <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 }