001    /*
002     * Copyright 2006-2012 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    
017    package edu.samplu.krad.validationmessagesview;
018    
019    import com.thoughtworks.selenium.Selenium;
020    import junit.framework.Assert;
021    import org.openqa.selenium.firefox.FirefoxDriver;
022    import org.openqa.selenium.WebDriver;
023    import org.openqa.selenium.WebDriverBackedSelenium;
024    import org.junit.After;
025    import org.junit.Before;
026    import org.junit.Test;
027    
028    public class ClientErrorsIT {
029        private Selenium selenium;
030    
031        @Before
032        public void setUp() throws Exception {
033            WebDriver driver = new FirefoxDriver();
034            selenium = new WebDriverBackedSelenium(driver,
035                    "http://localhost:8080/kr-dev/kr-krad/uicomponents?viewId=Demo-ValidationLayout&methodToCall=start");
036    
037            // Login
038            selenium.open(
039                    "http://localhost:8080/kr-dev/kr-krad/uicomponents?viewId=Demo-ValidationLayout&methodToCall=start");
040            Assert.assertEquals("Login", selenium.getTitle());
041            selenium.type("__login_user", "admin");
042            selenium.click("//input[@value='Login']");
043            selenium.waitForPageToLoad("30000");
044        }
045    
046        @Test
047        public void testClientErrors() throws Exception {
048            selenium.fireEvent("name=field1", "focus");
049            selenium.type("name=field1", "");
050            selenium.fireEvent("name=field1", "blur");
051            Assert.assertEquals("true", selenium.getAttribute("name=field1@aria-invalid"));
052            Assert.assertTrue(selenium.getAttribute("name=field1@class").matches("^[\\s\\S]*error[\\s\\S]*$"));
053            Assert.assertTrue(selenium.isElementPresent("//textarea[@name='field1']/../img[@alt='Error']"));
054            selenium.fireEvent("name=field1", "focus");
055            for (int second = 0; ; second++) {
056                if (second >= 60) {
057                    Assert.fail("timeout");
058                }
059                try {
060                    if (selenium.isVisible("css=.jquerybubblepopup-innerHtml")) {
061                        break;
062                    }
063                } catch (Exception e) {
064                }
065                Thread.sleep(1000);
066            }
067    
068            Assert.assertTrue(selenium.isVisible(
069                    "css=.jquerybubblepopup-innerHtml > .uif-clientMessageItems  .uif-errorMessageItem-field"));
070            selenium.type("name=field1", "a");
071            selenium.keyDown("name=field1", "b");
072            selenium.keyUp("name=field1", "b");
073            for (int second = 0; ; second++) {
074                if (second >= 60) {
075                    Assert.fail("timeout");
076                }
077                try {
078                    if (!selenium.isVisible("css=.jquerybubblepopup-innerHtml")) {
079                        break;
080                    }
081                } catch (Exception e) {
082                }
083                Thread.sleep(1000);
084            }
085    
086            Assert.assertFalse(selenium.isVisible("css=.jquerybubblepopup-innerHtml > .uif-clientMessageItems"));
087            selenium.fireEvent("name=field1", "blur");
088            Assert.assertFalse(selenium.isElementPresent("name=field1@aria-invalid"));
089            Assert.assertTrue(selenium.getAttribute("name=field1@class").matches("^[\\s\\S]*valid[\\s\\S]*$"));
090            Assert.assertFalse(selenium.isElementPresent("//textarea[@name='field1']/../img[@alt='Error']"));
091            selenium.fireEvent("name=field2", "focus");
092            selenium.type("name=field2", "");
093            selenium.fireEvent("name=field2", "blur");
094            Assert.assertEquals("true", selenium.getAttribute("name=field2@aria-invalid"));
095            Assert.assertTrue(selenium.getAttribute("name=field2@class").matches("^[\\s\\S]*error[\\s\\S]*$"));
096            Assert.assertTrue(selenium.isElementPresent("//input[@name='field2']/../img[@alt='Error']"));
097            selenium.fireEvent("name=field2", "focus");
098            selenium.type("name=field2", "a");
099            selenium.fireEvent("name=field2", "blur");
100            Assert.assertFalse(selenium.isElementPresent("name=field2@aria-invalid"));
101            Assert.assertTrue(selenium.getAttribute("name=field2@class").matches("^[\\s\\S]*valid[\\s\\S]*$"));
102            Assert.assertFalse(selenium.isElementPresent("//textarea[@name='field2']/../img[@alt='Error']"));
103            selenium.fireEvent("name=field3", "focus");
104            selenium.select("name=field3", "");
105            selenium.fireEvent("name=field3", "blur");
106            Assert.assertEquals("true", selenium.getAttribute("name=field3@aria-invalid"));
107            Assert.assertTrue(selenium.getAttribute("name=field3@class").matches("^[\\s\\S]*error[\\s\\S]*$"));
108            Assert.assertTrue(selenium.isElementPresent("//select[@name='field3']/../img[@alt='Error']"));
109            selenium.fireEvent("name=field3", "focus");
110            selenium.select("name=field3", "Option 1");
111            selenium.fireEvent("name=field3", "blur");
112            Assert.assertFalse(selenium.isElementPresent("name=field3@aria-invalid"));
113            Assert.assertTrue(selenium.getAttribute("name=field3@class").matches("^[\\s\\S]*valid[\\s\\S]*$"));
114            Assert.assertFalse(selenium.isElementPresent("//select[@name='field3']/../img[@alt='Error']"));
115            selenium.fireEvent("name=field4", "focus");
116            selenium.removeAllSelections("name=field4");
117            selenium.fireEvent("name=field4", "blur");
118            Assert.assertEquals("true", selenium.getAttribute("name=field4@aria-invalid"));
119            Assert.assertTrue(selenium.getAttribute("name=field4@class").matches("^[\\s\\S]*error[\\s\\S]*$"));
120            Assert.assertTrue(selenium.isElementPresent("//select[@name='field4']/../img[@alt='Error']"));
121            selenium.fireEvent("name=field4", "focus");
122            selenium.select("name=field4", "Option 1");
123            selenium.fireEvent("name=field4", "blur");
124            Assert.assertFalse(selenium.isElementPresent("name=field4@aria-invalid"));
125            Assert.assertTrue(selenium.getAttribute("name=field4@class").matches("^[\\s\\S]*valid[\\s\\S]*$"));
126            Assert.assertFalse(selenium.isElementPresent("//select[@name='field4']/../img[@alt='Error']"));
127            selenium.fireEvent("name=field117 value=3", "focus");
128            selenium.uncheck("name=field117 value=3");
129            selenium.fireEvent("name=field117", "blur");
130            for (int second = 0; ; second++) {
131                if (second >= 60) {
132                    Assert.fail("timeout");
133                }
134                try {
135                    if (selenium.isElementPresent("//input[@name='field117']/../../../img[@alt='Error']")) {
136                        break;
137                    }
138                } catch (Exception e) {
139                }
140                Thread.sleep(1000);
141            }
142    
143            Assert.assertEquals("true", selenium.getAttribute("name=field117 value=1@aria-invalid"));
144            Assert.assertTrue(selenium.getAttribute("name=field117 value=1@class").matches("^[\\s\\S]*error[\\s\\S]*$"));
145            Assert.assertTrue(selenium.isElementPresent("//input[@name='field117']/../../../img[@alt='Error']"));
146            selenium.fireEvent("name=field117 value=3", "focus");
147            selenium.check("name=field117 value=3");
148            selenium.fireEvent("name=field117 value=3", "blur");
149            for (int second = 0; ; second++) {
150                if (second >= 60) {
151                    Assert.fail("timeout");
152                }
153                try {
154                    if (!selenium.isElementPresent("//input[@name='field117']/../../../img[@alt='Error']")) {
155                        break;
156                    }
157                } catch (Exception e) {
158                }
159                Thread.sleep(1000);
160            }
161    
162            Assert.assertFalse(selenium.isElementPresent("name=field117 value=1@aria-invalid"));
163            Assert.assertTrue(selenium.getAttribute("name=field117 value=3@class").matches("^[\\s\\S]*valid[\\s\\S]*$"));
164            Assert.assertFalse(selenium.isElementPresent("//input[@name='field117']/../../../img[@alt='Error']"));
165            selenium.fireEvent("name=bField1", "focus");
166            selenium.uncheck("name=bField1");
167            selenium.fireEvent("name=bField1", "blur");
168            Assert.assertEquals("true", selenium.getAttribute("name=bField1@aria-invalid"));
169            Assert.assertTrue(selenium.getAttribute("name=bField1@class").matches("^[\\s\\S]*error[\\s\\S]*$"));
170            Assert.assertTrue(selenium.isElementPresent(
171                    "//input[@name='bField1' and following-sibling::img[@alt='Error']]"));
172            selenium.fireEvent("name=bField1", "focus");
173            selenium.check("name=bField1");
174            selenium.fireEvent("name=bField1", "blur");
175            Assert.assertFalse(selenium.isElementPresent("name=bField1@aria-invalid"));
176            Assert.assertTrue(selenium.getAttribute("name=bField1@class").matches("^[\\s\\S]*valid[\\s\\S]*$"));
177            Assert.assertFalse(selenium.isElementPresent(
178                    "//input[@name='bField1' and following-sibling::img[@alt='Error']]"));
179            selenium.fireEvent("name=field114 value=3", "focus");
180            selenium.uncheck("name=field114 value=3");
181            selenium.uncheck("name=field114 value=4");
182            selenium.fireEvent("name=field114", "blur");
183            for (int second = 0; ; second++) {
184                if (second >= 60) {
185                    Assert.fail("timeout");
186                }
187                try {
188                    if (selenium.isElementPresent("//input[@name='field114']/../../../img[@alt='Error']")) {
189                        break;
190                    }
191                } catch (Exception e) {
192                }
193                Thread.sleep(1000);
194            }
195    
196            Assert.assertEquals("true", selenium.getAttribute("name=field114 value=1@aria-invalid"));
197            Assert.assertTrue(selenium.getAttribute("name=field114 value=1@class").matches("^[\\s\\S]*error[\\s\\S]*$"));
198            Assert.assertTrue(selenium.isElementPresent("//input[@name='field114']/../../../img[@alt='Error']"));
199            selenium.fireEvent("name=field114 value=3", "focus");
200            selenium.check("name=field114 value=3");
201            selenium.check("name=field114 value=4");
202            selenium.fireEvent("name=field114", "blur");
203            for (int second = 0; ; second++) {
204                if (second >= 60) {
205                    Assert.fail("timeout");
206                }
207                try {
208                    if (!selenium.isElementPresent("//input[@name='field114']/../../../img[@alt='Error']")) {
209                        break;
210                    }
211                } catch (Exception e) {
212                }
213                Thread.sleep(1000);
214            }
215    
216            Assert.assertFalse(selenium.isElementPresent("name=field114 value=1@aria-invalid"));
217            Assert.assertFalse(selenium.isElementPresent("//input[@name='field114']/../../../img[@alt='Error']"));
218        }
219    
220        @After
221        public void tearDown() throws Exception {
222            selenium.stop();
223        }
224    }