View Javadoc

1   /*
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package edu.samplu.travel.krad.test;
17  
18  import edu.samplu.common.ITUtil;
19  import edu.samplu.common.WebDriverLegacyITBase;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.junit.Assert;
24  import org.junit.Ignore;
25  import org.junit.Test;
26  import org.kuali.rice.krad.uif.UifConstants;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import static org.junit.Assert.*;
32  
33  /**
34   *  Tests that the data attributes are rendered as expected for all controls
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class UifDataAttributesNavIT extends WebDriverLegacyITBase {
39  
40      @Override
41      public void fail(String message) {
42          Assert.fail(message);
43      }
44  
45      @Override
46      public String getTestUrl() {
47          return ITUtil.PORTAL;
48      }
49  
50      private  Log log = LogFactory.getLog(getClass());
51  
52      /**
53       * verify that a tag has simple data attributes
54       *
55       * @param tag - html tag e.g. img or a
56       * @param tagId - derived from the bean id set in the view
57       * @param tagIdSuffix - where applicable, a suffix that is appended to the control by krad e.g. _control
58       */
59      private void verifySimpleAttributes(String tag, String tagId, String tagIdSuffix) throws Exception{
60          // test the attributes that are set via the data attributes list
61          tagId = tagId + tagIdSuffix;
62          String simpleAttributesXpath="//" + tag + "[@id='" + tagId + "' and @data-iconTemplateName='cool-icon-%s.png' and @data-transitions='3']";
63          assertTrue(tagId + " does not have simple data attributes (via list) present", isElementPresentByXpath(simpleAttributesXpath));
64          verifyStaticDataAttributes(tag, tagId);
65  
66      }
67  
68      /**
69       * test the attributes that are set via the data*Attribute properties
70       *
71       * @param tag - html tag e.g. img or a
72       * @param tagId - the html tag id - a combination of bean id and any suffix
73       */
74      private void verifyStaticDataAttributes(String tag, String tagId) {
75          final String simpleAttributesXpath;
76          simpleAttributesXpath="//" + tag + "[@id='" + tagId + "'"
77                  + " and @data-dataroleattribute='role' and @data-datatypeattribute='type' and @data-datametaattribute='meta']";
78          assertTrue(tagId + " does not have simple data attributes (via data*Attribute) properties present",
79                  isElementPresentByXpath(simpleAttributesXpath));
80      }
81  
82      /**
83       * check that complex attributes exist in the script
84       *
85       * @param tagId - the expected tag id
86       * @param suffix - the expected suffix e.g. _button
87       */
88      private void verifyComplexAttributes(String tagId, String suffix)throws Exception {
89          tagId = tagId + suffix;
90          String complexAttributesXpath="//input[(@type='hidden') and (@data-role='dataScript') and (@data-for='"+ tagId +  "')]";
91          assertTrue(tagId + ": complex data attributes script not found", isElementPresentByXpath(complexAttributesXpath));
92  
93          // the message field does not support complex attributes
94          //if (!tagId.equalsIgnoreCase("messageField")) {
95              String scriptValue = waitAndGetAttributeByXpath(complexAttributesXpath, "value");
96              assertNotNull("script value is null",scriptValue);
97          boolean ok = scriptValue.contains(
98                  "jQuery('#" + tagId + "').data('capitals', {kenya:'nairobi', uganda:'kampala', tanzania:'dar'});")
99                  && scriptValue.contains("jQuery('#" + tagId + "').data('intervals', {short:2, medium:5, long:13});");
100         if (!ok) {
101             log.info("scriptValue for " + tagId + " is " + scriptValue);
102         }
103         // check for complex attributes
104         assertTrue(tagId + ": complex attributes script does not contain expected code", ok);
105         //}
106     }
107 
108     /**
109      * check that all attributes exist in the script
110      *
111      * @param tagId - the expected tag id
112      * @param suffix - the expected suffix e.g. _control
113      * @return true if all attributes were found in script, false otherwise
114      */
115     private boolean verifyAllAttributesInScript(String tagId, String suffix)throws Exception {
116         checkForIncidentReport();
117         tagId = tagId + suffix;
118         String complexAttributesXpath="//input[@type='hidden' and @data-for='"+ tagId +  "']";
119 
120         // the message field does not support complex attributes
121         String scriptValue = waitAndGetAttributeByXpath(complexAttributesXpath, "value");
122         assertNotNull("script value is null",scriptValue);
123         // log.info("scriptValue for " + tagId + " is " + scriptValue);
124         return scriptValue.contains("jQuery('#" + tagId + "').data('transitions', 3);") &&
125                 scriptValue.contains("jQuery('#" + tagId + "').data('iconTemplateName', 'cool-icon-%s.png');") &&
126                 scriptValue.contains("jQuery('#" + tagId + "').data('capitals', {kenya:'nairobi', uganda:'kampala', tanzania:'dar'});") &&
127                 scriptValue.contains("jQuery('#" + tagId + "').data('intervals', {short:2, medium:5, long:13});");
128     }
129 
130 
131     /**
132      * Tests that the data attributes are rendered as expected for all controls
133      */
134     @Ignore // This test fails, but what is it testing doing?
135     @Test
136     public void testDataAttributesPresentInControls () throws Exception{
137         assertEquals("Kuali Portal Index", getTitle());
138         open(getBaseUrlString()+ "/kr-krad/data-attributes-test-uif-controller?viewId=dataAttributesView_selenium&methodToCall=start");
139         waitForPageToLoad(); // if this times out make a special one that 50000
140         
141         // custom suffix to mark  test bean ids
142         String testIdSuffix = "_attrs";
143         // input fields, whose controls are implemented as spring form tags, will have both simple and complex attributes set via a script
144         String[] inputControls = {"textInputField", "textAreaInputField", "dropDown", "datePicker", "fileUpload", "userControl",
145                 "spinnerControl", "hiddenControl", "checkBox"};//, "radioButton",
146         for (int i=0; i<inputControls.length; i++) {
147             assertTrue(inputControls[i] + ": script does not contain expected code",
148                     verifyAllAttributesInScript(inputControls[i], testIdSuffix + UifConstants.IdSuffixes.CONTROL));
149             String tag = "input";
150             if (inputControls[i].equalsIgnoreCase("textAreaInputField")) {
151                 tag = "textarea";
152             } else if (inputControls[i].equalsIgnoreCase("dropDown")) {
153                 tag = "select";
154             }
155             try {
156                 verifyStaticDataAttributes(tag, inputControls[i] + testIdSuffix + UifConstants.IdSuffixes.CONTROL);
157             } catch (AssertionError ae) {
158                 assertTrue("KULRICE-7752 : UifDataAttributesIT testDataAttributesPresentInControls textInputField_attrs_control: complex data attributes script not found", false);
159             }
160         }
161         // these controls allow for simple attributes on the tag and complex attributes via js
162         Map<String, String[]> otherControlsMap = new HashMap<String, String[]>();
163         // controls whose simple attributes are set in an img tag
164         String[] imgControls = {"imageField_image"};
165         // fields whose simple attributes are set in an anchor tag
166         String[] anchorFields = {"navigationLink", "actionLink-noImage", "actionLink-imageRight", "actionLink-imageLeft", "linkElement"};
167         // fields whose simple attributes are set in a span tag
168         String[] spanFields = {"messageField"};
169         // fields whose simple attributes are set in an input tag
170         String[] inputFields = {"imageAction"};
171         // fields whose simple attributes are set in button tag
172         String[] buttonElements = {"buttonTextOnly", "buttonImageBottom", "buttonImageLeft", "buttonImageTop", "buttonImageRight"};
173         // iframe field
174         String[] iframeField = {"iframe"};
175         String[] divField={"spaceField","linkField"};
176         
177         otherControlsMap.put("img", imgControls);
178         otherControlsMap.put("a", anchorFields);
179         otherControlsMap.put("span", spanFields);
180         otherControlsMap.put("input", inputFields);
181         otherControlsMap.put("button", buttonElements);
182         otherControlsMap.put("iframe", iframeField);
183         otherControlsMap.put("div", divField);
184         
185         // a map to hold the tags where the simple attributes are affixed
186         // if a tag is not here, a empty string will be used for the suffix
187         Map<String, String> simpleTagIdSuffix = new HashMap<String, String>();
188         simpleTagIdSuffix.put("span", "_span");
189         
190 
191         for (String tag: otherControlsMap.keySet()) {
192             String[] controlIds = otherControlsMap.get(tag);
193             for (int i=0; i<controlIds.length; i++) {
194                 String tagId = controlIds[i];
195 
196                 // check for complex attributes
197                 verifyComplexAttributes(tagId, testIdSuffix);
198 
199                 // determine whether we are using a tag id suffix for the simple attributes
200                 String tagIdSuffix = testIdSuffix;
201                 if (simpleTagIdSuffix.containsKey(tag)) {
202                     tagIdSuffix = tagIdSuffix + simpleTagIdSuffix.get(tag);
203                 }
204 
205                 // check for simple attributes
206                 verifySimpleAttributes(tag, tagId, tagIdSuffix);
207             }
208             
209             // test label field - which uses the tagId suffix for both the simple attributes and complex
210             String tagId = "textInputField";
211             String tagIdSuffix = testIdSuffix + "_label";
212             // check for complex attributes
213             verifyComplexAttributes(tagId, tagIdSuffix);
214             // check for simple attributes
215             verifySimpleAttributes("label", tagId, tagIdSuffix);
216 
217             //test that the radio buttons have the 3 data attributes that can appear in the tag
218             tagId = "radioButton" + testIdSuffix + UifConstants.IdSuffixes.CONTROL;
219             String[] radioButtonIds = {tagId + "_0", tagId + "_1"};
220             for (String id: radioButtonIds) {
221                 verifyStaticDataAttributes("input", id);
222             }
223             //test that all complex and simple attributes set via the list are in a script
224             verifyAllAttributesInScript(tagId, "");
225         }
226         passed();
227     }
228 }