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 */
016package edu.samplu.krad.demo.uif.library;
017
018import org.junit.Assert;
019import org.junit.Test;
020import org.kuali.rice.krad.uif.UifConstants;
021import org.openqa.selenium.By;
022import org.openqa.selenium.WebElement;
023
024/**
025 * @author Kuali Rice Team (rice.collab@kuali.org)
026 */
027public class DemoLibraryDataFieldSmokeTest extends DemoLibraryBase {
028
029    /**
030     * /kr-krad/kradsampleapp?viewId=Demo-DataField-View&methodToCall=start
031     */
032    public static final String BOOKMARK_URL = "/kr-krad/kradsampleapp?viewId=Demo-DataField-View&methodToCall=start";
033
034    @Override
035    public String getBookmarkUrl() {
036        return BOOKMARK_URL;
037    }
038
039    @Override
040    protected void navigate() throws Exception {
041        navigateToLibraryDemo("Fields", "Data Field");
042    }
043
044    protected void testDataFieldDefault() throws Exception {
045        WebElement exampleDiv = navigateToExample("Demo-DataField-Example1");
046        WebElement field = exampleDiv.findElement(By.cssSelector("div[data-label='DataField 1']"));
047
048        String fieldId = field.getAttribute("id");
049        String controlId = fieldId + UifConstants.IdSuffixes.CONTROL;
050
051        assertIsVisible("#" + fieldId);
052        assertIsVisible("label[for='" + controlId + "']");
053
054        WebElement label = field.findElement(By.cssSelector("label[for='" + controlId + "']"));
055        if(!label.getText().contains("DataField 1:")){
056            fail("Label text does not match");
057        }
058
059        assertIsVisible("#" + controlId);
060
061        assertTextPresent("1001", "#" + controlId, "DataField value not correct");
062    }
063
064    protected void testDataFieldLabelTop() throws Exception {
065        WebElement exampleDiv = navigateToExample("Demo-DataField-Example2");
066        WebElement field = exampleDiv.findElement(By.cssSelector("div[data-label='DataField 1']"));
067
068        String fieldId = field.getAttribute("id");
069        String controlId = fieldId + UifConstants.IdSuffixes.CONTROL;
070
071        assertIsVisible("#" + fieldId);
072        assertIsVisible("label[for='" + controlId + "']");
073
074        WebElement label = field.findElement(By.cssSelector("label[for='" + controlId + "']"));
075        if(!label.getText().contains("DataField 1:")){
076            fail("Label text does not match");
077        }
078
079        WebElement labelspan = field.findElement(By.cssSelector("span[data-label_for='" + fieldId + "']"));
080        // top and bottom add the uif-labelBlock class
081        if(!labelspan.getAttribute("class").contains("uif-labelBlock")){
082            fail("Label span does not contain the appropriate class expected");
083        }
084
085        assertIsVisible("#" + controlId);
086    }
087
088    protected void testDataFieldLabelRight() throws Exception {
089        WebElement exampleDiv = navigateToExample("Demo-DataField-Example3");
090        WebElement field = findElement(By.cssSelector("div[data-label='DataField 1']"), exampleDiv);
091
092        String fieldId = field.getAttribute("id");
093        String controlId = fieldId + UifConstants.IdSuffixes.CONTROL;
094
095        assertIsVisible("#" + fieldId);
096        assertIsVisible("label[for='" + controlId + "']");
097
098        WebElement label = findElement(By.cssSelector("[for='" + controlId + "']"), field);
099        if(!label.getText().contains("DataField 1")){
100            fail("Label text does not match");
101        }
102
103        assertIsVisible("#" + controlId);
104
105        // validate that the label comes after the value
106        findElement(By.cssSelector("span[id='" + controlId + "'] + span[data-label_for='" + fieldId + "']"), exampleDiv);
107    }
108
109    protected void testDataFieldDefaultValue() throws Exception {
110        String valueText = textValueUnderTest("Demo-DataField-Example4", "DataField 2");
111        Assert.assertEquals("2012", valueText);
112    }
113
114    protected void testDataFieldAppendProperty() throws Exception {
115        String valueText = textValueUnderTest("Demo-DataField-Example5", "DataField 1");
116        Assert.assertTrue(valueText.endsWith("ID Val"));
117    }
118
119    protected void testDataFieldReplaceProperty() throws Exception {
120        String valueText = textValueUnderTest("Demo-DataField-Example6", "DataField 1");
121        Assert.assertEquals("ID Val", valueText);
122    }
123
124    protected void testDataFieldReplacePropertyWithField() throws Exception {
125        String valueText = textValueUnderTest("Demo-DataField-Example7", "DataField 1");
126        Assert.assertEquals("My Book Title", valueText);
127    }
128
129    protected void testDataFieldAppendPropertyWithField() throws Exception {
130        String valueText = textValueUnderTest("Demo-DataField-Example8", "DataField 1");
131        Assert.assertEquals("1001 *-* My Book Title", valueText);
132    }
133
134    private String textValueUnderTest(String example, String testLabel) throws Exception {
135        WebElement exampleDiv = navigateToExample(example);
136        WebElement field = findElement(By.cssSelector("div[data-label='" + testLabel + "']"), exampleDiv);
137
138        String fieldId = field.getAttribute("id");
139        String controlId = fieldId + UifConstants.IdSuffixes.CONTROL;
140
141        assertIsVisible("#" + fieldId);
142        assertIsVisible("label[for='" + controlId + "']");
143
144        WebElement label = findElement(By.cssSelector("[for='" + controlId + "']"), field);
145        if(!label.getText().contains(testLabel)){
146            fail("Label text does not match");
147        }
148
149        assertIsVisible("#" + controlId);
150
151        return findElement(By.id(controlId), field).getText();
152    }
153
154    protected void testDataFieldExamples() throws Exception{
155        testDataFieldDefault();
156        testDataFieldLabelTop();
157        testDataFieldLabelRight();
158        testDataFieldDefaultValue();
159        testDataFieldAppendProperty();
160        testDataFieldReplaceProperty();
161        testDataFieldReplacePropertyWithField();
162        testDataFieldAppendPropertyWithField();
163    }
164
165    @Test
166    public void testDataFieldExamplesBookmark() throws Exception {
167        testDataFieldExamples();
168        passed();
169    }
170
171    @Test
172    public void testDataFieldExamplesNav() throws Exception {
173        testDataFieldExamples();
174        passed();
175    }
176
177    @Test
178    public void testDataFieldDefaultBookmark() throws Exception {
179        testDataFieldDefault();
180        passed();
181    }
182
183    @Test
184    public void testDataFieldDefaultNav() throws Exception {
185        testDataFieldDefault();
186        passed();
187    }
188
189    @Test
190    public void testDataFieldLabelTopBookmark() throws Exception {
191        testDataFieldLabelTop();
192        passed();
193    }
194
195    @Test
196    public void testDataFieldLabelTopNav() throws Exception {
197        testDataFieldLabelTop();
198        passed();
199    }
200
201    @Test
202    public void testDataFieldLabelRightBookmark() throws Exception {
203        testDataFieldLabelRight();
204        passed();
205    }
206
207    @Test
208    public void testDataFieldLabelRightNav() throws Exception {
209        testDataFieldLabelRight();
210        passed();
211    }
212
213    @Test
214    public void testDataFieldDefaultValueBookmark() throws Exception {
215        testDataFieldDefaultValue();
216        passed();
217    }
218
219    @Test
220    public void testDataFieldDefaultValueNav() throws Exception {
221        testDataFieldDefaultValue();
222        passed();
223    }
224
225    @Test
226    public void testDataFieldAppendPropertyBookmark() throws Exception {
227        testDataFieldAppendProperty();
228        passed();
229    }
230
231    @Test
232    public void testDataFieldAppendPropertyNav() throws Exception {
233        testDataFieldAppendProperty();
234        passed();
235    }
236
237    @Test
238    public void testDataFieldReplacePropertyBookmark() throws Exception {
239        testDataFieldReplaceProperty();
240        passed();
241    }
242
243    @Test
244    public void testDataFieldReplacePropertyNav() throws Exception {
245        testDataFieldReplaceProperty();
246        passed();
247    }
248
249    @Test
250    public void testDataFieldReplacePropertyWithFieldBookmark() throws Exception {
251        testDataFieldReplacePropertyWithField();
252        passed();
253    }
254
255    @Test
256    public void testDataFieldReplacePropertyWithFieldNav() throws Exception {
257        testDataFieldReplacePropertyWithField();
258        passed();
259    }
260
261    @Test
262    public void testDataFieldAppendPropertyWithFieldBookmark() throws Exception {
263        testDataFieldAppendPropertyWithField();
264        passed();
265    }
266
267    @Test
268    public void testDataFieldAppendPropertyWithFieldNav() throws Exception {
269        testDataFieldAppendPropertyWithField();
270        passed();
271    }
272}