View Javadoc

1   /*
2    * Copyright 2006-2012 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 com.thoughtworks.selenium.DefaultSelenium;
19  import com.thoughtworks.selenium.Selenium;
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.commons.logging.Log;
22  import org.junit.After;
23  import org.junit.Before;
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.assertEquals;
32  import static org.junit.Assert.assertNotNull;
33  import static org.junit.Assert.assertTrue;
34  
35  /**
36   *  Tests that the data attributes are rendered as expected for all controls
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class UifDataAttributesIT {
41      private Selenium selenium;
42      private  Log log = LogFactory.getLog(getClass());
43  
44      @Before
45      public void setUp() throws Exception {
46          selenium = new DefaultSelenium("localhost", 4444, "*firefox", System.getProperty("remote.public.url"));
47          selenium.start();
48      }
49  
50      /**
51       * verify that a tag has simple data attributes
52       *
53       * @param tag - html tag e.g. img or a
54       * @param tagId - derived from the bean id set in the view
55       * @param tagIdSuffix - where applicable, a suffix that is appended to the control by krad e.g. _control
56       */
57      private void verifySimpleAttributes(String tag, String tagId, String tagIdSuffix) {
58          // test the attributes that are set via the data attributes list
59          tagId = tagId + tagIdSuffix;
60          String simpleAttributesXpath="//" + tag + "[(@id='" + tagId + "') and (@data-iconTemplateName='cool-icon-%s.png') and (@data-transitions='3')]";
61          assertTrue(tagId + " does not have simple data attributes (via list) present", selenium.isElementPresent(simpleAttributesXpath));
62          verifyStaticDataAttributes(tag, tagId);
63  
64      }
65  
66      /**
67       * test the attributes that are set via the data*Attribute properties
68       *
69       * @param tag - html tag e.g. img or a
70       * @param tagId - the html tag id - a combination of bean id and any suffix
71       */
72      private void verifyStaticDataAttributes(String tag, String tagId) {
73          final String simpleAttributesXpath;
74          simpleAttributesXpath="//" + tag + "[(@id='" + tagId + "')"
75                  + " and (@data-role='role') and (@data-type='type') and (@data-meta='meta')]";
76          assertTrue(tagId + " does not have simple data attributes (via data*Attribute) properties present",
77                  selenium.isElementPresent(simpleAttributesXpath));
78      }
79  
80      /**
81       * check that complex attributes exist in the script
82       *
83       * @param tagId - the expected tag id
84       * @param suffix - the expected suffix e.g. _button
85       */
86      private void verifyComplexAttributes(String tagId, String suffix) {
87          tagId = tagId + suffix;
88          String complexAttributesXpath="//input[(@type='hidden') and (@data-role='dataScript') and (@data-for='"+ tagId +  "')]";
89          assertTrue(tagId + ": complex data attributes script not found", selenium.isElementPresent(complexAttributesXpath));
90  
91          // the message field does not support complex attributes
92          //if (!tagId.equalsIgnoreCase("messageField")) {
93              String scriptValue = selenium.getAttribute(complexAttributesXpath + "@value");
94              assertNotNull("script value is null",scriptValue);
95          boolean ok = scriptValue.contains(
96                  "jQuery('#" + tagId + "').data('capitals', {kenya:'nairobi', uganda:'kampala', tanzania:'dar'});")
97                  && scriptValue.contains("jQuery('#" + tagId + "').data('intervals', {short:2, medium:5, long:13});");
98          if (!ok) {
99              log.info("scriptValue for " + tagId + " is " + scriptValue);
100         }
101         // check for complex attributes
102         assertTrue(tagId + ": complex attributes script does not contain expected code", ok);
103         //}
104     }
105 
106     /**
107      * check that all attributes exist in the script
108      *
109      * @param tagId - the expected tag id
110      * @param suffix - the expected suffix e.g. _control
111      * @return true if all attributes were found in script, false otherwise
112      */
113     private boolean verifyAllAttributesInScript(String tagId, String suffix) {
114         tagId = tagId + suffix;
115         String complexAttributesXpath="//input[(@type='hidden') and (@data-for='"+ tagId +  "')]";
116         assertTrue(tagId + ": complex data attributes script not found", selenium.isElementPresent(complexAttributesXpath));
117 
118         // the message field does not support complex attributes
119         String scriptValue = selenium.getAttribute(complexAttributesXpath + "@value");
120         assertNotNull("script value is null",scriptValue);
121         // log.info("scriptValue for " + tagId + " is " + scriptValue);
122         return scriptValue.contains("jQuery('#" + tagId + "').data('transitions', 3);") &&
123                 scriptValue.contains("jQuery('#" + tagId + "').data('iconTemplateName', 'cool-icon-%s.png');") &&
124                 scriptValue.contains("jQuery('#" + tagId + "').data('capitals', {kenya:'nairobi', uganda:'kampala', tanzania:'dar'});") &&
125                 scriptValue.contains("jQuery('#" + tagId + "').data('intervals', {short:2, medium:5, long:13});");
126     }
127 
128 
129     /**
130      * Tests that the data attributes are rendered as expected for all controls
131      */
132     @Test
133     public void testDataAttributesPresentInControls () {
134         selenium.open(System.getProperty("remote.public.url"));
135         assertEquals("Login", selenium.getTitle());
136         selenium.type("__login_user", "admin");
137         selenium.click("//input[@value='Login']");
138         selenium.waitForPageToLoad("50000");
139         assertEquals("Kuali Portal Index", selenium.getTitle());
140         selenium.open(
141                 "/kr-dev/kr-krad/data-attributes-test-uif-controller?viewId=dataAttributesView_selenium&methodToCall=start");
142         selenium.waitForPageToLoad("50000");
143 
144         // custom suffix to mark  test bean ids
145         String testIdSuffix = "_attrs";
146         // input fields, whose controls are implemented as spring form tags, will have both simple and complex attributes set via a script
147         String[] inputControls = {"textInputField", "textAreaInputField", "dropDown", "datePicker", "fileUpload", "userControl",
148                 "spinnerControl", "hiddenControl", "checkBox"};//, "radioButton",
149         for (int i=0; i<inputControls.length; i++) {
150             assertTrue(inputControls[i] + ": script does not contain expected code",
151                     verifyAllAttributesInScript(inputControls[i], testIdSuffix + UifConstants.IdSuffixes.CONTROL));
152             String tag = "input";
153             if (inputControls[i].equalsIgnoreCase("textAreaInputField")) {
154                 tag = "textarea";
155             } else if (inputControls[i].equalsIgnoreCase("dropDown")) {
156                 tag = "select";
157             }
158             verifyStaticDataAttributes(tag, inputControls[i] + testIdSuffix + UifConstants.IdSuffixes.CONTROL);
159         }
160         // these controls allow for simple attributes on the tag and complex attributes via js
161         Map<String, String[]> otherControlsMap = new HashMap<String, String[]>();
162         // controls whose simple attributes are set in an img tag
163         String[] imgControls = {"imageField_image"};
164         // fields whose simple attributes are set in an anchor tag
165         String[] anchorFields = {"navigationLink", "actionLink-noImage", "actionLink-imageRight", "actionLink-imageLeft",
166                 "linkField", "linkElement"};
167         // fields whose simple attributes are set in a span tag
168         String[] spanFields = {"messageField", "spaceField"};
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         
176         otherControlsMap.put("img", imgControls);
177         otherControlsMap.put("a", anchorFields);
178         otherControlsMap.put("span", spanFields);
179         otherControlsMap.put("input", inputFields);
180         otherControlsMap.put("button", buttonElements);
181         otherControlsMap.put("iframe", iframeField);
182 
183         // a map to hold the tags where the simple attributes are affixed
184         // if a tag is not here, a empty string will be used for the suffix
185         Map<String, String> simpleTagIdSuffix = new HashMap<String, String>();
186         simpleTagIdSuffix.put("span", "_span");
187         
188 
189         for (String tag: otherControlsMap.keySet()) {
190             String[] controlIds = otherControlsMap.get(tag);
191             for (int i=0; i<controlIds.length; i++) {
192                 String tagId = controlIds[i];
193 
194                 // check for complex attributes
195                 verifyComplexAttributes(tagId, testIdSuffix);
196 
197                 // determine whether we are using a tag id suffix for the simple attributes
198                 String tagIdSuffix = testIdSuffix;
199                 if (simpleTagIdSuffix.containsKey(tag)) {
200                     tagIdSuffix = tagIdSuffix + simpleTagIdSuffix.get(tag);
201                 }
202 
203                 // check for simple attributes
204                 verifySimpleAttributes(tag, tagId, tagIdSuffix);
205             }
206             
207             // test label field - which uses the tagId suffix for both the simple attributes and complex
208             String tagId = "textInputField";
209             String tagIdSuffix = testIdSuffix + "_label";
210             // check for complex attributes
211             verifyComplexAttributes(tagId, tagIdSuffix);
212             // check for simple attributes
213             verifySimpleAttributes("label", tagId, tagIdSuffix);
214 
215             //test that the radio buttons have the 3 data attributes that can appear in the tag
216             tagId = "radioButton" + testIdSuffix + UifConstants.IdSuffixes.CONTROL;
217             String[] radioButtonIds = {tagId + "1", tagId + "2"};
218             for (String id: radioButtonIds) {
219                 verifyStaticDataAttributes("input", id);
220             }
221             //test that all complex and simple attributes set via the list are in a script
222             verifyAllAttributesInScript(tagId, "");
223         }
224     }
225 
226     @After
227     public void tearDown() throws Exception {
228         selenium.stop();
229     }
230 }