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.sampleu.admin;
18  
19  import org.junit.Test;
20  import org.kuali.rice.krad.util.KRADConstants;
21  import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
22  import org.kuali.rice.testtools.selenium.WebDriverITBase;
23  import org.kuali.rice.testtools.selenium.WebDriverUtils;
24  import org.openqa.selenium.By;
25  import org.openqa.selenium.NoSuchElementException;
26  import org.openqa.selenium.WebElement;
27  
28  import java.net.URLEncoder;
29  import java.util.HashMap;
30  import java.util.Map;
31  import java.util.concurrent.TimeUnit;
32  
33  import static com.thoughtworks.selenium.SeleneseTestBase.assertFalse;
34  import static com.thoughtworks.selenium.SeleneseTestBase.assertTrue;
35  import static com.thoughtworks.selenium.SeleneseTestCase.assertEquals;
36  
37  /**
38   * Tests docsearch url parameters
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class DocumentSearchUrlParametersAft extends WebDriverITBase {
42  
43      @Override
44      public String getTestUrl() {
45          return AutomatedFunctionalTestUtils.PORTAL;
46      }
47  
48      private static final String DOCUMENT_TYPE_NAME = "KualiNotification";
49      private static final String ADVANCED_SEARCH_ONLY_FIELD = "applicationDocumentId";
50  
51      protected static final Map<String, String> CORE_FIELDS = new HashMap<String, String>();
52      static {
53          // basic
54          CORE_FIELDS.put("documentTypeName", DOCUMENT_TYPE_NAME);
55          CORE_FIELDS.put("documentId", "1234");
56          CORE_FIELDS.put("initiatorPrincipalName", "CURRENT_USER");
57          CORE_FIELDS.put("dateCreated", "11/11/11");
58  
59      }
60      protected static final Map<String, String> BASIC_FIELDS = new HashMap<String, String>();
61      static {
62          BASIC_FIELDS.putAll(CORE_FIELDS);
63          // searchable attrs
64          BASIC_FIELDS.put("documentAttribute.notificationContentType", "testType");
65          BASIC_FIELDS.put("documentAttribute.notificationChannel", "testChannel");
66          BASIC_FIELDS.put("documentAttribute.notificationProducer", "testProducer");
67          BASIC_FIELDS.put("documentAttribute.notificationPriority", "testPriority");
68          BASIC_FIELDS.put("documentAttribute.notificationRecipients", "testRecipients");
69          BASIC_FIELDS.put("documentAttribute.notificationSenders", "testSenders");
70          BASIC_FIELDS.put("saveName", "testBasicSearchFields_saved_search");
71          BASIC_FIELDS.put("isAdvancedSearch", "NO");
72      }
73  
74      protected static final Map<String, String> ADVANCED_FIELDS = new HashMap<String, String>();
75      static {
76          ADVANCED_FIELDS.put("approverPrincipalName", "testApproverName");
77          ADVANCED_FIELDS.put("viewerPrincipalName", "testViewerName");
78          ADVANCED_FIELDS.put("applicationDocumentId", "testApplicationDocumentId");
79          // ADVANCED_FIELDS.put("routeNodeName", "Adhoc Routing");
80          ADVANCED_FIELDS.put("dateApproved", "01/01/01");
81          ADVANCED_FIELDS.put("dateLastModified", "02/02/02");
82          ADVANCED_FIELDS.put("dateFinalized", "03/03/03");
83          ADVANCED_FIELDS.put("title", "test title");
84          ADVANCED_FIELDS.put("isAdvancedSearch", "YES");
85      }
86  
87      String getDocSearchURL(Map<String, String> params) {
88          StringBuilder sb = new StringBuilder();
89          for (Map.Entry<String, String> entry: params.entrySet()) {
90              sb.append(URLEncoder.encode(entry.getKey()) + "=" + URLEncoder.encode(entry.getValue()) + "&");
91          }
92          return getDocSearchURL(sb.toString());
93          
94      }
95      protected String getDocSearchURL(String params) {
96          return WebDriverUtils.getBaseUrlString() + "/kew/DocumentSearch.do?" + params;
97      }
98      
99      private WebElement findElementByTagAndName(String tag, String name) {
100         return driver.findElement(By.cssSelector(tag + "[name=" + name + "]"));
101     }
102     
103     private WebElement findInput(String name) {
104         return driver.findElement(By.xpath("//input[@name='" + name + "']"));
105     }
106     
107     protected WebElement findModeToggleButton() {
108         WebElement toggleAdvancedSearch = null;
109         try {
110             return driver.findElement(By.id("toggleAdvancedSearch"));
111         } catch (NoSuchElementException e) {
112             fail("toggleAdvancedSearch button not found");
113             return null;
114         }
115     }
116 
117     protected void assertSearchDetailMode(WebElement e, boolean advanced) {
118         assertEquals((advanced ? "basic" : "detailed") + " search", e.getAttribute("title"));
119 
120         try {
121             findInput(ADVANCED_SEARCH_ONLY_FIELD);
122             if (!advanced) fail("Advanced search field found in basic search");
123         } catch (NoSuchElementException nsee) {
124             if (advanced) fail("Advanced search field not found in advancedsearch");
125         }
126     }
127 
128     protected void assertInputValues(Map<String, String> fields) {
129         boolean quickmode = false;
130         for (Map.Entry<String, String> entry: fields.entrySet()) {
131             String value = findInput(entry.getKey()).getAttribute("value");
132             assertEquals("Field '" + entry.getKey() + "' expected '" + entry.getValue() + "' got '" + value + "'", entry.getValue(), value);
133             if (!quickmode) { // do the first find slow to make sure the screen has finished loading, then do them fast, else some tests take minutes to run
134                 driver.manage().timeouts().implicitlyWait(WebDriverUtils.IMPLICIT_WAIT_TIME_LOOP_MS, TimeUnit.MILLISECONDS);
135                 quickmode = true;
136             }
137         }
138         if (quickmode) {
139             driver.manage().timeouts().implicitlyWait(WebDriverUtils.configuredImplicityWait(), TimeUnit.SECONDS);
140         }
141     }
142 
143     protected void assertInputPresence(Map<String, String> fields, boolean present) {
144         boolean quickmode = false;
145         for (String name: fields.keySet()) {
146             if (present) {
147                 assertTrue("Expected field '" + name + "' to be present", driver.findElements(By.name(name)).size() != 0);
148             } else {
149                 assertEquals("Expected field '" + name + "' not to be present", 0, driver.findElements(By.name(name)).size());
150             }
151             if (!quickmode) { // do the first find slow to make sure the screen has finished loading, then do them fast, else some tests take minutes to run
152                 driver.manage().timeouts().implicitlyWait(WebDriverUtils.IMPLICIT_WAIT_TIME_LOOP_MS, TimeUnit.MILLISECONDS);
153                 quickmode = true;
154             }
155         }
156         if (quickmode) {
157             driver.manage().timeouts().implicitlyWait(WebDriverUtils.configuredImplicityWait(), TimeUnit.SECONDS);
158         }
159     }
160 
161     @Test
162     public void testAdvancedSearchFields() throws InterruptedException{
163         // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
164         Map<String, String> values = new HashMap<String, String>(BASIC_FIELDS);
165         values.putAll(ADVANCED_FIELDS);
166         driver.get(getDocSearchURL(values));
167 
168         assertInputValues(values);
169 
170         driver.findElement(By.id("toggleAdvancedSearch")).click();
171 
172         assertInputValues(BASIC_FIELDS);
173     }
174 
175     @Test
176     public void testAdvancedSearchFieldsAndExecuteSearch() throws InterruptedException{
177         // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
178         Map<String, String> expected = new HashMap<String, String>(BASIC_FIELDS);
179         expected.putAll(ADVANCED_FIELDS);
180 
181         Map<String, String> values = new HashMap<String, String>(expected);
182         values.put("methodToCall", "search");
183         driver.get(getDocSearchURL(values));
184 
185         assertInputValues(expected);
186 
187         // verify that it attempted the search
188         assertTrue(driver.getPageSource().contains("No values match this search"));
189 
190         driver.findElement(By.id("toggleAdvancedSearch")).click();
191 
192         assertInputValues(BASIC_FIELDS);
193     }
194 
195     @Test
196     public void testAdvancedSearchFieldsAndExecuteSearchWithHiddenCriteria() throws InterruptedException {
197         // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
198         Map<String, String> expected = new HashMap<String, String>(BASIC_FIELDS);
199         expected.putAll(ADVANCED_FIELDS);
200 
201         Map<String, String> values = new HashMap<String, String>(expected);
202         values.put("methodToCall", "search");
203         values.put("searchCriteriaEnabled", "NO");
204         driver.get(getDocSearchURL(values));
205 
206         assertInputPresence(expected, false);
207 
208         // verify that it attempted the search
209         assertTrue(driver.getPageSource().contains("No values match this search"));
210 
211         // NOTE: toggling modes re-enables the search criteria
212     }
213 
214     @Test
215     public void testAdvancedSearchMode() {
216         driver.get(getDocSearchURL((KRADConstants.ADVANCED_SEARCH_FIELD + "=YES")));
217         WebElement toggle = findModeToggleButton();
218         assertSearchDetailMode(toggle, true);
219         toggle.click();
220         assertSearchDetailMode(findModeToggleButton(), false);
221     }
222 
223     @Test
224     public void testBasicSearchFields() throws InterruptedException{
225         // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
226         driver.get(getDocSearchURL(BASIC_FIELDS));
227 
228         assertInputValues(BASIC_FIELDS);
229 
230         driver.findElement(By.id("toggleAdvancedSearch")).click();
231 
232         Map<String, String> expected = new HashMap<String, String>(BASIC_FIELDS);
233         for (Map.Entry<String, String> entry: ADVANCED_FIELDS.entrySet()) {
234             if (!"isAdvancedSearch".equals(entry.getKey())) {
235                 expected.put(entry.getKey(), "");
236             } else {
237                 expected.put(entry.getKey(), entry.getValue());
238             }
239         }
240         assertInputValues(expected);
241     }
242 
243     @Test
244     public void testBasicSearchFieldsAndExecuteSearch() throws InterruptedException {
245         // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
246         Map<String, String> fields = new HashMap<String, String>();
247         fields.putAll(BASIC_FIELDS);
248         fields.put("methodToCall", "search");
249         driver.get(getDocSearchURL(fields));
250 
251         assertInputValues(BASIC_FIELDS);
252 
253         // verify that it attempted the search
254         assertTrue(driver.getPageSource().contains("No values match this search"));
255 
256         driver.findElement(By.id("toggleAdvancedSearch")).click();
257 
258         Map<String, String> expected = new HashMap<String, String>(BASIC_FIELDS);
259         for (Map.Entry<String, String> entry: ADVANCED_FIELDS.entrySet()) {
260             if (!"isAdvancedSearch".equals(entry.getKey())) {
261                 expected.put(entry.getKey(), "");
262             } else {
263                 expected.put(entry.getKey(), entry.getValue());
264             }
265         }
266         assertInputValues(expected);
267 
268         // I guess switching modes doesn't re-execute the search
269         // assertTrue(driver.getPageSource().contains("No values match this search"));
270     }
271 
272     @Test
273     public void testBasicSearchFieldsAndExecuteSearchWithHiddenCriteria() throws InterruptedException {
274         // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
275         Map<String, String> fields = new HashMap<String, String>();
276         fields.putAll(BASIC_FIELDS);
277         fields.put("methodToCall", "search");
278         fields.put("searchCriteriaEnabled", "NO");
279         driver.get(getDocSearchURL(fields));
280 
281         assertInputPresence(BASIC_FIELDS, false);
282 
283         // verify that it attempted the search
284         assertTrue(driver.getPageSource().contains("No values match this search"));
285 
286         // NOTE: toggling modes re-enables the search criteria
287     }
288 
289     @Test
290     public void testBasicSearchMode() throws InterruptedException{
291         driver.get(getDocSearchURL(""));
292         WebElement toggle = findModeToggleButton();
293         assertSearchDetailMode(toggle, false);
294         toggle.click();
295         assertSearchDetailMode(findModeToggleButton(), true);
296     }
297 
298     @Test
299     public void testCriteriaDisabled() throws InterruptedException{
300         driver.get(getDocSearchURL("searchCriteriaEnabled=NO"));
301         assertInputPresence(CORE_FIELDS, false);
302         driver.get(getDocSearchURL("searchCriteriaEnabled=true"));
303         assertInputPresence(CORE_FIELDS, true);
304     }
305 
306     @Test
307     public void testHeaderBarDisabled() throws InterruptedException{
308         driver.get(getDocSearchURL("headerBarEnabled=false"));
309         assertTrue(driver.findElements(By.id("headerarea-small")).isEmpty());
310         assertInputPresence(CORE_FIELDS, true);
311         driver.get(getDocSearchURL("headerBarEnabled=true"));
312         assertFalse(driver.findElements(By.id("headerarea-small")).isEmpty());
313         assertInputPresence(CORE_FIELDS, true);
314     }
315 
316     /**
317      * Supplying a saveName does not result in the saved search getting loaded.
318      * @throws InterruptedException
319      */
320     @Test
321     public void testSupplyingSavedSearchNameDoesNothing() throws InterruptedException {
322         // get the search saved
323         driver.get(getDocSearchURL(BASIC_FIELDS));
324 
325         driver.get(getDocSearchURL("saveName=testBasicSearchFields_saved_search"));
326 
327         Map<String, String> emptyForm = new HashMap<String, String>();
328         for (String key: CORE_FIELDS.keySet()) {
329             emptyForm.put(key, "");
330         }
331 
332         assertInputValues(emptyForm);
333     }
334 }