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.travel;
017
018 import org.apache.commons.logging.Log;
019 import org.apache.commons.logging.LogFactory;
020 import org.junit.Test;
021 import org.kuali.rice.krad.uif.UifConstants;
022 import org.kuali.rice.testtools.selenium.WebDriverLegacyITBase;
023 import org.kuali.rice.testtools.selenium.WebDriverUtils;
024
025 import java.util.HashMap;
026 import java.util.Map;
027
028 import static org.junit.Assert.*;
029
030 /**
031 * Tests that the data attributes are rendered as expected for all controls
032 *
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035 public class UifDataAttributesNavAft extends WebDriverLegacyITBase {
036
037 public static String BOOKMARK_URL = WebDriverUtils.getBaseUrlString()+ "/kr-krad/data-attributes-test-uif-controller?viewId=dataAttributesView_selenium&methodToCall=start";
038
039 @Override
040 protected String getBookmarkUrl() {
041 return BOOKMARK_URL;
042 }
043
044 private Log log = LogFactory.getLog(getClass());
045
046 /**
047 * verify that a tag has simple data attributes
048 *
049 * @param tag - html tag e.g. img or a
050 * @param tagId - derived from the bean id set in the view
051 * @param tagIdSuffix - where applicable, a suffix that is appended to the control by krad e.g. _control
052 */
053 private void verifySimpleAttributes(String tag, String tagId, String tagIdSuffix) throws Exception{
054 // test the attributes that are set via the data attributes list
055 tagId = tagId + tagIdSuffix;
056 String simpleAttributesXpath="//" + tag + "[@id='" + tagId + "' and @data-iconTemplateName='cool-icon-%s.png' and @data-transitions='3']";
057 assertTrue(tagId + " does not have simple data attributes (via list) present", isElementPresentByXpath(simpleAttributesXpath));
058 verifyStaticDataAttributes(tag, tagId);
059
060 }
061
062 /**
063 * test the attributes that are set via the data*Attribute properties
064 *
065 * @param tag - html tag e.g. img or a
066 * @param tagId - the html tag id - a combination of bean id and any suffix
067 */
068 private void verifyStaticDataAttributes(String tag, String tagId) {
069 final String simpleAttributesXpath;
070 simpleAttributesXpath="//" + tag + "[@id='" + tagId + "'"
071 + " and @data-dataroleattribute='role' and @data-datatypeattribute='type' and @data-datametaattribute='meta']";
072 assertTrue(tagId + " does not have simple data attributes (via data*Attribute) properties present",
073 isElementPresentByXpath(simpleAttributesXpath));
074 }
075
076 /**
077 * check that complex attributes exist in the script
078 *
079 * @param tagId - the expected tag id
080 * @param suffix - the expected suffix e.g. _button
081 */
082 private void verifyComplexAttributes(String tagId, String suffix)throws Exception {
083 tagId = tagId + suffix;
084 String complexAttributesXpath="//input[(@type='hidden') and (@data-role='dataScript') and (@data-for='"+ tagId + "')]";
085 assertTrue(tagId + ": complex data attributes script not found", isElementPresentByXpath(complexAttributesXpath));
086
087 // the message field does not support complex attributes
088 //if (!tagId.equalsIgnoreCase("messageField")) {
089 String scriptValue = waitAndGetAttributeByXpath(complexAttributesXpath, "value");
090 assertNotNull("script value is null",scriptValue);
091 boolean ok = scriptValue.contains(
092 "jQuery('#" + tagId + "').data('capitals', {kenya:'nairobi', uganda:'kampala', tanzania:'dar'});")
093 && scriptValue.contains("jQuery('#" + tagId + "').data('intervals', {short:2, medium:5, long:13});");
094 if (!ok) {
095 log.info("scriptValue for " + tagId + " is " + scriptValue);
096 }
097 // check for complex attributes
098 assertTrue(tagId + ": complex attributes script does not contain expected code", ok);
099 //}
100 }
101
102 /**
103 * check that all attributes exist in the script
104 *
105 * @param tagId - the expected tag id
106 * @param suffix - the expected suffix e.g. _control
107 * @return true if all attributes were found in script, false otherwise
108 */
109 private boolean verifyAllAttributesInScript(String tagId, String suffix)throws Exception {
110 checkForIncidentReport();
111 tagId = tagId + suffix;
112 String complexAttributesXpath="//input[@type='hidden' and @data-for='"+ tagId + "']";
113
114 // the message field does not support complex attributes
115 String scriptValue = waitAndGetAttributeByXpath(complexAttributesXpath, "value");
116 assertNotNull("script value is null",scriptValue);
117 // log.info("scriptValue for " + tagId + " is " + scriptValue);
118 return scriptValue.contains("jQuery('#" + tagId + "').data('transitions', 3);") &&
119 scriptValue.contains("jQuery('#" + tagId + "').data('iconTemplateName', 'cool-icon-%s.png');") &&
120 scriptValue.contains("jQuery('#" + tagId + "').data('capitals', {kenya:'nairobi', uganda:'kampala', tanzania:'dar'});") &&
121 scriptValue.contains("jQuery('#" + tagId + "').data('intervals', {short:2, medium:5, long:13});");
122 }
123
124
125 /**
126 * Tests that the data attributes are rendered as expected for all controls
127 */
128 @Test
129 public void testDataAttributesPresentInControlsNav() throws Exception{
130 assertEquals("Kuali Portal Index", getTitle());
131 open(getBookmarkUrl());
132 waitForPageToLoad(); // if this times out make a special one that 50000
133
134 // custom suffix to mark test bean ids
135 String testIdSuffix = "_attrs";
136 // input fields, whose controls are implemented as spring form tags, will have both simple and complex attributes set via a script
137 String[] inputControls = {"textInputField", "textAreaInputField", "dropDown", "datePicker", "fileUpload", "userControl",
138 "spinnerControl", "hiddenControl", "checkBox"};//, "radioButton",
139 for (int i=0; i<inputControls.length; i++) {
140 assertTrue(inputControls[i] + ": script does not contain expected code",
141 verifyAllAttributesInScript(inputControls[i], testIdSuffix + UifConstants.IdSuffixes.CONTROL));
142 String tag = "input";
143 if (inputControls[i].equalsIgnoreCase("textAreaInputField")) {
144 tag = "textarea";
145 } else if (inputControls[i].equalsIgnoreCase("dropDown")) {
146 tag = "select";
147 }
148 verifyStaticDataAttributes(tag, inputControls[i] + testIdSuffix + UifConstants.IdSuffixes.CONTROL);
149 }
150 // these controls allow for simple attributes on the tag and complex attributes via js
151 Map<String, String[]> otherControlsMap = new HashMap<String, String[]>();
152 // controls whose simple attributes are set in an img tag
153 String[] imgControls = {"imageField_image"};
154 // fields whose simple attributes are set in an anchor tag
155 String[] anchorFields = {"navigationLink", "actionLink-noImage", "actionLink-imageRight", "actionLink-imageLeft", "linkElement"};
156 // fields whose simple attributes are set in a span tag
157 String[] spanFields = {"messageField"};
158 // fields whose simple attributes are set in an input tag
159 String[] inputFields = {"imageAction"};
160 // fields whose simple attributes are set in button tag
161 String[] buttonElements = {"buttonTextOnly", "buttonImageBottom", "buttonImageLeft", "buttonImageTop", "buttonImageRight"};
162 // iframe field
163 String[] iframeField = {"iframe"};
164 String[] divField={"spaceField","linkField"};
165
166 otherControlsMap.put("img", imgControls);
167 otherControlsMap.put("a", anchorFields);
168 otherControlsMap.put("span", spanFields);
169 otherControlsMap.put("input", inputFields);
170 otherControlsMap.put("button", buttonElements);
171 otherControlsMap.put("iframe", iframeField);
172 otherControlsMap.put("div", divField);
173
174 // a map to hold the tags where the simple attributes are affixed
175 // if a tag is not here, a empty string will be used for the suffix
176 Map<String, String> simpleTagIdSuffix = new HashMap<String, String>();
177 simpleTagIdSuffix.put("span", "_span");
178
179
180 for (String tag: otherControlsMap.keySet()) {
181 String[] controlIds = otherControlsMap.get(tag);
182 for (int i=0; i<controlIds.length; i++) {
183 String tagId = controlIds[i];
184
185 // check for complex attributes
186 verifyComplexAttributes(tagId, testIdSuffix);
187
188 // determine whether we are using a tag id suffix for the simple attributes
189 String tagIdSuffix = testIdSuffix;
190 if (simpleTagIdSuffix.containsKey(tag)) {
191 tagIdSuffix = tagIdSuffix + simpleTagIdSuffix.get(tag);
192 }
193
194 // check for simple attributes
195 verifySimpleAttributes(tag, tagId, tagIdSuffix);
196 }
197
198 // test label field - which uses the tagId suffix for both the simple attributes and complex
199 String tagId = "textInputField";
200 String tagIdSuffix = testIdSuffix + "_label";
201 // check for complex attributes
202 verifyComplexAttributes(tagId, tagIdSuffix);
203 // check for simple attributes
204 verifySimpleAttributes("label", tagId, tagIdSuffix);
205
206 //test that the radio buttons have the 3 data attributes that can appear in the tag
207 tagId = "radioButton" + testIdSuffix + UifConstants.IdSuffixes.CONTROL;
208 String[] radioButtonIds = {tagId + "_0", tagId + "_1"};
209 for (String id: radioButtonIds) {
210 verifyStaticDataAttributes("input", id);
211 }
212 //test that all complex and simple attributes set via the list are in a script
213 verifyAllAttributesInScript(tagId, "");
214 }
215 passed();
216 }
217 }