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