001    /**
002     * Copyright 2005-2014 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    package edu.sampleu.admin;
017    
018    import org.junit.Test;
019    import org.kuali.rice.krad.util.KRADConstants;
020    import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
021    import org.kuali.rice.testtools.selenium.WebDriverITBase;
022    import org.kuali.rice.testtools.selenium.WebDriverUtils;
023    import org.openqa.selenium.By;
024    import org.openqa.selenium.NoSuchElementException;
025    import org.openqa.selenium.WebElement;
026    
027    import java.net.URLEncoder;
028    import java.util.HashMap;
029    import java.util.Map;
030    import java.util.concurrent.TimeUnit;
031    
032    import static com.thoughtworks.selenium.SeleneseTestBase.assertFalse;
033    import static com.thoughtworks.selenium.SeleneseTestBase.assertTrue;
034    import static com.thoughtworks.selenium.SeleneseTestCase.assertEquals;
035    
036    /**
037     * Tests docsearch url parameters
038     * @author Kuali Rice Team (rice.collab@kuali.org)
039     */
040    public class DocumentSearchUrlParametersAft extends WebDriverITBase {
041    
042        @Override
043        public String getTestUrl() {
044            return AutomatedFunctionalTestUtils.PORTAL;
045        }
046    
047        private static final String DOCUMENT_TYPE_NAME = "KualiNotification";
048        private static final String ADVANCED_SEARCH_ONLY_FIELD = "applicationDocumentId";
049    
050        protected static final Map<String, String> CORE_FIELDS = new HashMap<String, String>();
051        static {
052            // basic
053            CORE_FIELDS.put("documentTypeName", DOCUMENT_TYPE_NAME);
054            CORE_FIELDS.put("documentId", "1234");
055            CORE_FIELDS.put("initiatorPrincipalName", "CURRENT_USER");
056            CORE_FIELDS.put("dateCreated", "11/11/11");
057    
058        }
059        protected static final Map<String, String> BASIC_FIELDS = new HashMap<String, String>();
060        static {
061            BASIC_FIELDS.putAll(CORE_FIELDS);
062            // searchable attrs
063            BASIC_FIELDS.put("documentAttribute.notificationContentType", "testType");
064            BASIC_FIELDS.put("documentAttribute.notificationChannel", "testChannel");
065            BASIC_FIELDS.put("documentAttribute.notificationProducer", "testProducer");
066            BASIC_FIELDS.put("documentAttribute.notificationPriority", "testPriority");
067            BASIC_FIELDS.put("documentAttribute.notificationRecipients", "testRecipients");
068            BASIC_FIELDS.put("documentAttribute.notificationSenders", "testSenders");
069            BASIC_FIELDS.put("saveName", "testBasicSearchFields_saved_search");
070            BASIC_FIELDS.put("isAdvancedSearch", "NO");
071        }
072    
073        protected static final Map<String, String> ADVANCED_FIELDS = new HashMap<String, String>();
074        static {
075            ADVANCED_FIELDS.put("approverPrincipalName", "testApproverName");
076            ADVANCED_FIELDS.put("viewerPrincipalName", "testViewerName");
077            ADVANCED_FIELDS.put("applicationDocumentId", "testApplicationDocumentId");
078            // ADVANCED_FIELDS.put("routeNodeName", "Adhoc Routing");
079            ADVANCED_FIELDS.put("dateApproved", "01/01/01");
080            ADVANCED_FIELDS.put("dateLastModified", "02/02/02");
081            ADVANCED_FIELDS.put("dateFinalized", "03/03/03");
082            ADVANCED_FIELDS.put("title", "test title");
083            ADVANCED_FIELDS.put("isAdvancedSearch", "YES");
084        }
085    
086        String getDocSearchURL(Map<String, String> params) {
087            StringBuilder sb = new StringBuilder();
088            for (Map.Entry<String, String> entry: params.entrySet()) {
089                sb.append(URLEncoder.encode(entry.getKey()) + "=" + URLEncoder.encode(entry.getValue()) + "&");
090            }
091            return getDocSearchURL(sb.toString());
092            
093        }
094        protected String getDocSearchURL(String params) {
095            return WebDriverUtils.getBaseUrlString() + "/kew/DocumentSearch.do?" + params;
096        }
097        
098        private WebElement findElementByTagAndName(String tag, String name) {
099            return driver.findElement(By.cssSelector(tag + "[name=" + name + "]"));
100        }
101        
102        private WebElement findInput(String name) {
103            return driver.findElement(By.xpath("//input[@name='" + name + "']"));
104        }
105        
106        protected WebElement findModeToggleButton() {
107            WebElement toggleAdvancedSearch = null;
108            try {
109                return driver.findElement(By.id("toggleAdvancedSearch"));
110            } catch (NoSuchElementException e) {
111                fail("toggleAdvancedSearch button not found");
112                return null;
113            }
114        }
115    
116        protected void assertSearchDetailMode(WebElement e, boolean advanced) {
117            assertEquals((advanced ? "basic" : "detailed") + " search", e.getAttribute("title"));
118    
119            try {
120                findInput(ADVANCED_SEARCH_ONLY_FIELD);
121                if (!advanced) fail("Advanced search field found in basic search");
122            } catch (NoSuchElementException nsee) {
123                if (advanced) fail("Advanced search field not found in advancedsearch");
124            }
125        }
126    
127        protected void assertInputValues(Map<String, String> fields) {
128            boolean quickmode = false;
129            for (Map.Entry<String, String> entry: fields.entrySet()) {
130                String value = findInput(entry.getKey()).getAttribute("value");
131                assertEquals("Field '" + entry.getKey() + "' expected '" + entry.getValue() + "' got '" + value + "'", entry.getValue(), value);
132                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
133                    driver.manage().timeouts().implicitlyWait(WebDriverUtils.IMPLICIT_WAIT_TIME_LOOP_MS, TimeUnit.MILLISECONDS);
134                    quickmode = true;
135                }
136            }
137            if (quickmode) {
138                driver.manage().timeouts().implicitlyWait(WebDriverUtils.configuredImplicityWait(), TimeUnit.SECONDS);
139            }
140        }
141    
142        protected void assertInputPresence(Map<String, String> fields, boolean present) {
143            boolean quickmode = false;
144            for (String name: fields.keySet()) {
145                if (present) {
146                    assertTrue("Expected field '" + name + "' to be present", driver.findElements(By.name(name)).size() != 0);
147                } else {
148                    assertEquals("Expected field '" + name + "' not to be present", 0, driver.findElements(By.name(name)).size());
149                }
150                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
151                    driver.manage().timeouts().implicitlyWait(WebDriverUtils.IMPLICIT_WAIT_TIME_LOOP_MS, TimeUnit.MILLISECONDS);
152                    quickmode = true;
153                }
154            }
155            if (quickmode) {
156                driver.manage().timeouts().implicitlyWait(WebDriverUtils.configuredImplicityWait(), TimeUnit.SECONDS);
157            }
158        }
159    
160        @Test
161        public void testAdvancedSearchFields() throws InterruptedException{
162            // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
163            Map<String, String> values = new HashMap<String, String>(BASIC_FIELDS);
164            values.putAll(ADVANCED_FIELDS);
165            driver.get(getDocSearchURL(values));
166    
167            assertInputValues(values);
168    
169            driver.findElement(By.id("toggleAdvancedSearch")).click();
170    
171            assertInputValues(BASIC_FIELDS);
172        }
173    
174        @Test
175        public void testAdvancedSearchFieldsAndExecuteSearch() throws InterruptedException{
176            // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
177            Map<String, String> expected = new HashMap<String, String>(BASIC_FIELDS);
178            expected.putAll(ADVANCED_FIELDS);
179    
180            Map<String, String> values = new HashMap<String, String>(expected);
181            values.put("methodToCall", "search");
182            driver.get(getDocSearchURL(values));
183    
184            assertInputValues(expected);
185    
186            // verify that it attempted the search
187            assertTrue(driver.getPageSource().contains("No values match this search"));
188    
189            driver.findElement(By.id("toggleAdvancedSearch")).click();
190    
191            assertInputValues(BASIC_FIELDS);
192        }
193    
194        @Test
195        public void testAdvancedSearchFieldsAndExecuteSearchWithHiddenCriteria() throws InterruptedException {
196            // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
197            Map<String, String> expected = new HashMap<String, String>(BASIC_FIELDS);
198            expected.putAll(ADVANCED_FIELDS);
199    
200            Map<String, String> values = new HashMap<String, String>(expected);
201            values.put("methodToCall", "search");
202            values.put("searchCriteriaEnabled", "NO");
203            driver.get(getDocSearchURL(values));
204    
205            assertInputPresence(expected, false);
206    
207            // verify that it attempted the search
208            assertTrue(driver.getPageSource().contains("No values match this search"));
209    
210            // NOTE: toggling modes re-enables the search criteria
211        }
212    
213        @Test
214        public void testAdvancedSearchMode() {
215            driver.get(getDocSearchURL((KRADConstants.ADVANCED_SEARCH_FIELD + "=YES")));
216            WebElement toggle = findModeToggleButton();
217            assertSearchDetailMode(toggle, true);
218            toggle.click();
219            assertSearchDetailMode(findModeToggleButton(), false);
220        }
221    
222        @Test
223        public void testBasicSearchFields() throws InterruptedException{
224            // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
225            driver.get(getDocSearchURL(BASIC_FIELDS));
226    
227            assertInputValues(BASIC_FIELDS);
228    
229            driver.findElement(By.id("toggleAdvancedSearch")).click();
230    
231            Map<String, String> expected = new HashMap<String, String>(BASIC_FIELDS);
232            for (Map.Entry<String, String> entry: ADVANCED_FIELDS.entrySet()) {
233                if (!"isAdvancedSearch".equals(entry.getKey())) {
234                    expected.put(entry.getKey(), "");
235                } else {
236                    expected.put(entry.getKey(), entry.getValue());
237                }
238            }
239            assertInputValues(expected);
240        }
241    
242        @Test
243        public void testBasicSearchFieldsAndExecuteSearch() throws InterruptedException {
244            // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
245            Map<String, String> fields = new HashMap<String, String>();
246            fields.putAll(BASIC_FIELDS);
247            fields.put("methodToCall", "search");
248            driver.get(getDocSearchURL(fields));
249    
250            assertInputValues(BASIC_FIELDS);
251    
252            // verify that it attempted the search
253            assertTrue(driver.getPageSource().contains("No values match this search"));
254    
255            driver.findElement(By.id("toggleAdvancedSearch")).click();
256    
257            Map<String, String> expected = new HashMap<String, String>(BASIC_FIELDS);
258            for (Map.Entry<String, String> entry: ADVANCED_FIELDS.entrySet()) {
259                if (!"isAdvancedSearch".equals(entry.getKey())) {
260                    expected.put(entry.getKey(), "");
261                } else {
262                    expected.put(entry.getKey(), entry.getValue());
263                }
264            }
265            assertInputValues(expected);
266    
267            // I guess switching modes doesn't re-execute the search
268            // assertTrue(driver.getPageSource().contains("No values match this search"));
269        }
270    
271        @Test
272        public void testBasicSearchFieldsAndExecuteSearchWithHiddenCriteria() throws InterruptedException {
273            // criteria.initiator=delyea&criteria.docTypeFullName=" + documentTypeName +
274            Map<String, String> fields = new HashMap<String, String>();
275            fields.putAll(BASIC_FIELDS);
276            fields.put("methodToCall", "search");
277            fields.put("searchCriteriaEnabled", "NO");
278            driver.get(getDocSearchURL(fields));
279    
280            assertInputPresence(BASIC_FIELDS, false);
281    
282            // verify that it attempted the search
283            assertTrue(driver.getPageSource().contains("No values match this search"));
284    
285            // NOTE: toggling modes re-enables the search criteria
286        }
287    
288        @Test
289        public void testBasicSearchMode() throws InterruptedException{
290            driver.get(getDocSearchURL(""));
291            WebElement toggle = findModeToggleButton();
292            assertSearchDetailMode(toggle, false);
293            toggle.click();
294            assertSearchDetailMode(findModeToggleButton(), true);
295        }
296    
297        @Test
298        public void testCriteriaDisabled() throws InterruptedException{
299            driver.get(getDocSearchURL("searchCriteriaEnabled=NO"));
300            assertInputPresence(CORE_FIELDS, false);
301            driver.get(getDocSearchURL("searchCriteriaEnabled=true"));
302            assertInputPresence(CORE_FIELDS, true);
303        }
304    
305        @Test
306        public void testHeaderBarDisabled() throws InterruptedException{
307            driver.get(getDocSearchURL("headerBarEnabled=false"));
308            assertTrue(driver.findElements(By.id("headerarea-small")).isEmpty());
309            assertInputPresence(CORE_FIELDS, true);
310            driver.get(getDocSearchURL("headerBarEnabled=true"));
311            assertFalse(driver.findElements(By.id("headerarea-small")).isEmpty());
312            assertInputPresence(CORE_FIELDS, true);
313        }
314    
315        /**
316         * Supplying a saveName does not result in the saved search getting loaded.
317         * @throws InterruptedException
318         */
319        @Test
320        public void testSupplyingSavedSearchNameDoesNothing() throws InterruptedException {
321            // get the search saved
322            driver.get(getDocSearchURL(BASIC_FIELDS));
323    
324            driver.get(getDocSearchURL("saveName=testBasicSearchFields_saved_search"));
325    
326            Map<String, String> emptyForm = new HashMap<String, String>();
327            for (String key: CORE_FIELDS.keySet()) {
328                emptyForm.put(key, "");
329            }
330    
331            assertInputValues(emptyForm);
332        }
333    }