1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
37
38
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
52
53
54
55
56
57 private void verifySimpleAttributes(String tag, String tagId, String tagIdSuffix) {
58
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
68
69
70
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
82
83
84
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
92
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
102 assertTrue(tagId + ": complex attributes script does not contain expected code", ok);
103
104 }
105
106
107
108
109
110
111
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
119 String scriptValue = selenium.getAttribute(complexAttributesXpath + "@value");
120 assertNotNull("script value is null",scriptValue);
121
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
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
145 String testIdSuffix = "_attrs";
146
147 String[] inputControls = {"textInputField", "textAreaInputField", "dropDown", "datePicker", "fileUpload", "userControl",
148 "spinnerControl", "hiddenControl", "checkBox"};
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
161 Map<String, String[]> otherControlsMap = new HashMap<String, String[]>();
162
163 String[] imgControls = {"imageField_image"};
164
165 String[] anchorFields = {"navigationLink", "actionLink-noImage", "actionLink-imageRight", "actionLink-imageLeft",
166 "linkField", "linkElement"};
167
168 String[] spanFields = {"messageField", "spaceField"};
169
170 String[] inputFields = {"imageAction"};
171
172 String[] buttonElements = {"buttonTextOnly", "buttonImageBottom", "buttonImageLeft", "buttonImageTop", "buttonImageRight"};
173
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
184
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
195 verifyComplexAttributes(tagId, testIdSuffix);
196
197
198 String tagIdSuffix = testIdSuffix;
199 if (simpleTagIdSuffix.containsKey(tag)) {
200 tagIdSuffix = tagIdSuffix + simpleTagIdSuffix.get(tag);
201 }
202
203
204 verifySimpleAttributes(tag, tagId, tagIdSuffix);
205 }
206
207
208 String tagId = "textInputField";
209 String tagIdSuffix = testIdSuffix + "_label";
210
211 verifyComplexAttributes(tagId, tagIdSuffix);
212
213 verifySimpleAttributes("label", tagId, tagIdSuffix);
214
215
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
222 verifyAllAttributesInScript(tagId, "");
223 }
224 }
225
226 @After
227 public void tearDown() throws Exception {
228 selenium.stop();
229 }
230 }