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.sampleu.admin;
018    
019    import org.junit.Test;
020    import org.kuali.rice.krad.util.KRADConstants;
021    import org.kuali.rice.testtools.selenium.AutomatedFunctionalTestUtils;
022    import org.kuali.rice.testtools.selenium.WebDriverITBase;
023    import org.kuali.rice.testtools.selenium.WebDriverUtils;
024    import org.openqa.selenium.By;
025    import org.openqa.selenium.NoSuchElementException;
026    import org.openqa.selenium.WebElement;
027    
028    import java.net.URLEncoder;
029    import java.util.HashMap;
030    import java.util.Map;
031    import java.util.concurrent.TimeUnit;
032    
033    import static com.thoughtworks.selenium.SeleneseTestBase.assertFalse;
034    import static com.thoughtworks.selenium.SeleneseTestBase.assertTrue;
035    import static com.thoughtworks.selenium.SeleneseTestCase.assertEquals;
036    
037    /**
038     * Tests docsearch url parameters
039     * @author Kuali Rice Team (rice.collab@kuali.org)
040     */
041    public class DocumentSearchUrlParametersAft extends WebDriverITBase {
042    
043        @Override
044        public String getTestUrl() {
045            return AutomatedFunctionalTestUtils.PORTAL;
046        }
047    
048        private static final String DOCUMENT_TYPE_NAME = "KualiNotification";
049        private static final String ADVANCED_SEARCH_ONLY_FIELD = "applicationDocumentId";
050    
051        protected static final Map<String, String> CORE_FIELDS = new HashMap<String, String>();
052        static {
053            // basic
054            CORE_FIELDS.put("documentTypeName", DOCUMENT_TYPE_NAME);
055            CORE_FIELDS.put("documentId", "1234");
056            CORE_FIELDS.put("initiatorPrincipalName", "CURRENT_USER");
057            CORE_FIELDS.put("dateCreated", "11/11/11");
058    
059        }
060        protected static final Map<String, String> BASIC_FIELDS = new HashMap<String, String>();
061        static {
062            BASIC_FIELDS.putAll(CORE_FIELDS);
063            // searchable attrs
064            BASIC_FIELDS.put("documentAttribute.notificationContentType", "testType");
065            BASIC_FIELDS.put("documentAttribute.notificationChannel", "testChannel");
066            BASIC_FIELDS.put("documentAttribute.notificationProducer", "testProducer");
067            BASIC_FIELDS.put("documentAttribute.notificationPriority", "testPriority");
068            BASIC_FIELDS.put("documentAttribute.notificationRecipients", "testRecipients");
069            BASIC_FIELDS.put("documentAttribute.notificationSenders", "testSenders");
070            BASIC_FIELDS.put("saveName", "testBasicSearchFields_saved_search");
071            BASIC_FIELDS.put("isAdvancedSearch", "NO");
072        }
073    
074        protected static final Map<String, String> ADVANCED_FIELDS = new HashMap<String, String>();
075        static {
076            ADVANCED_FIELDS.put("approverPrincipalName", "testApproverName");
077            ADVANCED_FIELDS.put("viewerPrincipalName", "testViewerName");
078            ADVANCED_FIELDS.put("applicationDocumentId", "testApplicationDocumentId");
079            // ADVANCED_FIELDS.put("routeNodeName", "Adhoc Routing");
080            ADVANCED_FIELDS.put("dateApproved", "01/01/01");
081            ADVANCED_FIELDS.put("dateLastModified", "02/02/02");
082            ADVANCED_FIELDS.put("dateFinalized", "03/03/03");
083            ADVANCED_FIELDS.put("title", "test title");
084            ADVANCED_FIELDS.put("isAdvancedSearch", "YES");
085        }
086    
087        String getDocSearchURL(Map<String, String> params) {
088            StringBuilder sb = new StringBuilder();
089            for (Map.Entry<String, String> entry: params.entrySet()) {
090                sb.append(URLEncoder.encode(entry.getKey()) + "=" + URLEncoder.encode(entry.getValue()) + "&");
091            }
092            return getDocSearchURL(sb.toString());
093            
094        }
095        protected String getDocSearchURL(String params) {
096            return WebDriverUtils.getBaseUrlString() + "/kew/DocumentSearch.do?" + params;
097        }
098        
099        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    }