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