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
017package edu.samplu.admin.test;
018
019import edu.samplu.common.ITUtil;
020import edu.samplu.common.WebDriverITBase;
021import edu.samplu.common.WebDriverUtil;
022import org.openqa.selenium.By;
023import org.openqa.selenium.NoSuchElementException;
024import org.openqa.selenium.WebElement;
025
026import java.net.URLEncoder;
027import java.util.HashMap;
028import java.util.Map;
029import java.util.concurrent.TimeUnit;
030
031import static com.thoughtworks.selenium.SeleneseTestBase.assertTrue;
032import static com.thoughtworks.selenium.SeleneseTestBase.fail;
033import static com.thoughtworks.selenium.SeleneseTestCase.assertEquals;
034
035/**
036 * Tests docsearch url parameters
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039public class DocumentSearchURLParametersITBase extends WebDriverITBase {
040
041    @Override
042    public String getTestUrl() {
043        return ITUtil.PORTAL;
044    }
045
046    private static final String DOCUMENT_TYPE_NAME = "KualiNotification";
047    private static final String ADVANCED_SEARCH_ONLY_FIELD = "applicationDocumentId";
048
049    protected static final Map<String, String> CORE_FIELDS = new HashMap<String, String>();
050    static {
051        // basic
052        CORE_FIELDS.put("documentTypeName", DOCUMENT_TYPE_NAME);
053        CORE_FIELDS.put("documentId", "1234");
054        CORE_FIELDS.put("initiatorPrincipalName", "CURRENT_USER");
055        CORE_FIELDS.put("dateCreated", "11/11/11");
056
057    }
058    protected static final Map<String, String> BASIC_FIELDS = new HashMap<String, String>();
059    static {
060        BASIC_FIELDS.putAll(CORE_FIELDS);
061        // searchable attrs
062        BASIC_FIELDS.put("documentAttribute.notificationContentType", "testType");
063        BASIC_FIELDS.put("documentAttribute.notificationChannel", "testChannel");
064        BASIC_FIELDS.put("documentAttribute.notificationProducer", "testProducer");
065        BASIC_FIELDS.put("documentAttribute.notificationPriority", "testPriority");
066        BASIC_FIELDS.put("documentAttribute.notificationRecipients", "testRecipients");
067        BASIC_FIELDS.put("documentAttribute.notificationSenders", "testSenders");
068        BASIC_FIELDS.put("saveName", "testBasicSearchFields_saved_search");
069        BASIC_FIELDS.put("isAdvancedSearch", "NO");
070    }
071
072    protected static final Map<String, String> ADVANCED_FIELDS = new HashMap<String, String>();
073    static {
074        ADVANCED_FIELDS.put("approverPrincipalName", "testApproverName");
075        ADVANCED_FIELDS.put("viewerPrincipalName", "testViewerName");
076        ADVANCED_FIELDS.put("applicationDocumentId", "testApplicationDocumentId");
077        // ADVANCED_FIELDS.put("routeNodeName", "Adhoc Routing");
078        ADVANCED_FIELDS.put("dateApproved", "01/01/01");
079        ADVANCED_FIELDS.put("dateLastModified", "02/02/02");
080        ADVANCED_FIELDS.put("dateFinalized", "03/03/03");
081        ADVANCED_FIELDS.put("title", "test title");
082        ADVANCED_FIELDS.put("isAdvancedSearch", "YES");
083    }
084
085    String getDocSearchURL(Map<String, String> params) {
086        StringBuilder sb = new StringBuilder();
087        for (Map.Entry<String, String> entry: params.entrySet()) {
088            sb.append(URLEncoder.encode(entry.getKey()) + "=" + URLEncoder.encode(entry.getValue()) + "&");
089        }
090        return getDocSearchURL(sb.toString());
091        
092    }
093    protected String getDocSearchURL(String params) {
094        return ITUtil.getBaseUrlString() + "/kew/DocumentSearch.do?" + params;
095    }
096    
097    private WebElement findElementByTagAndName(String tag, String name) {
098        return driver.findElement(By.cssSelector(tag + "[name=" + name + "]"));
099    }
100    
101    private WebElement findInput(String name) {
102        return driver.findElement(By.xpath("//input[@name='" + name + "']"));
103    }
104    
105    protected WebElement findModeToggleButton() {
106        WebElement toggleAdvancedSearch = null;
107        try {
108            return driver.findElement(By.id("toggleAdvancedSearch"));
109        } catch (NoSuchElementException e) {
110            fail("toggleAdvancedSearch button not found");
111            return null;
112        }
113    }
114
115    protected void assertSearchDetailMode(WebElement e, boolean advanced) {
116        assertEquals((advanced ? "basic" : "detailed") + " search", e.getAttribute("title"));
117
118        try {
119            findInput(ADVANCED_SEARCH_ONLY_FIELD);
120            if (!advanced) fail("Advanced search field found in basic search");
121        } catch (NoSuchElementException nsee) {
122            if (advanced) fail("Advanced search field not found in advancedsearch");
123        }
124    }
125
126    protected void assertInputValues(Map<String, String> fields) {
127        boolean quickmode = false;
128        for (Map.Entry<String, String> entry: fields.entrySet()) {
129            String value = findInput(entry.getKey()).getAttribute("value");
130            assertEquals("Field '" + entry.getKey() + "' expected '" + entry.getValue() + "' got '" + value + "'", entry.getValue(), value);
131            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
132                driver.manage().timeouts().implicitlyWait(WebDriverUtil.SHORT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
133                quickmode = true;
134            }
135        }
136        if (quickmode) {
137            driver.manage().timeouts().implicitlyWait(WebDriverUtil.DEFAULT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
138        }
139    }
140
141    protected void assertInputPresence(Map<String, String> fields, boolean present) {
142        boolean quickmode = false;
143        for (String name: fields.keySet()) {
144            if (present) {
145                assertTrue("Expected field '" + name + "' to be present", driver.findElements(By.name(name)).size() != 0);
146            } else {
147                assertEquals("Expected field '" + name + "' not to be present", 0, driver.findElements(By.name(name)).size());
148            }
149            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
150                driver.manage().timeouts().implicitlyWait(WebDriverUtil.SHORT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
151                quickmode = true;
152            }
153        }
154        if (quickmode) {
155            driver.manage().timeouts().implicitlyWait(WebDriverUtil.DEFAULT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
156        }
157    }
158}