View Javadoc
1   /**
2    * Copyright 2005-2014 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 org.kuali.rice.krad.demo.uif.library.fields;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.junit.Test;
20  import org.kuali.rice.krad.demo.uif.library.DemoLibraryBase;
21  import org.openqa.selenium.By;
22  import org.openqa.selenium.WebElement;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Node;
25  
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.xpath.XPath;
29  import javax.xml.xpath.XPathConstants;
30  import javax.xml.xpath.XPathFactory;
31  import java.util.List;
32  
33  import static org.junit.Assert.assertEquals;
34  
35  /**
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class DemoFieldsActionAft extends DemoLibraryBase {
39  
40      /**
41       * /kr-krad/kradsampleapp?viewId=Demo-ActionFieldView&methodToCall=start
42       */
43      public static final String BOOKMARK_URL = "/kr-krad/kradsampleapp?viewId=Demo-ActionFieldView&methodToCall=start";
44  
45      @Override
46      public String getBookmarkUrl() {
47          return BOOKMARK_URL;
48      }
49  
50      @Override
51      protected void navigate() throws Exception {
52          navigateToLibraryDemo("Fields", "Action Field");
53      }
54  
55      protected void testActionFieldDefault() throws Exception {
56          WebElement exampleDiv = navigateToExample("Demo-ActionField-Example1");
57          WebElement field = findElement(By.cssSelector(".uif-actionLink"), exampleDiv);
58  
59          String fieldId = field.getAttribute("id");
60  
61          assertIsVisible("#" + fieldId);
62          waitAndClickByLinkText(field.getText());
63  
64          assertTrue(driver.switchTo().alert().getText().contains("You clicked the link"));
65          alertAccept();
66      }
67  
68      protected void testActionFieldPresubmit() throws Exception {
69          WebElement exampleDiv = navigateToExample("Demo-ActionField-Example2");
70          List<WebElement> fields = exampleDiv.findElements(By.cssSelector("a.uif-actionLink"));
71  
72          assertEquals("Two action links do not exist on the page", 2, fields.size());
73  
74          assertElementPresentByLinkText("Pre submit returning true Link");
75          waitAndClickByLinkText("Pre submit returning true Link");
76          assertTrue(driver.switchTo().alert().getText().contains("Pre submit call was invoked, returning true"));
77          driver.switchTo().alert().accept();
78  
79          assertElementPresentByLinkText("Pre submit returning false Link");
80          waitAndClickByLinkText("Pre submit returning false Link");
81          assertTrue(driver.switchTo().alert().getText().contains("Pre submit call was invoked, returning false"));
82          driver.switchTo().alert().accept();
83      }
84  
85      protected void testActionFieldSuccessCallback() throws Exception {
86          WebElement exampleDiv = navigateToExample("Demo-ActionField-Example3");
87          WebElement field = findElement(By.cssSelector(".uif-actionLink"), exampleDiv);
88  
89          String fieldId = field.getAttribute("id");
90  
91          assertIsVisible("#" + fieldId);
92          waitAndClickByLinkText(field.getText());
93  
94          Thread.sleep(1500); // give alert time to be triggered
95          assertTrue(driver.switchTo().alert().getText().contains("Refresh called successfully"));
96          alertAccept();
97      }
98  
99      protected void testActionFieldValidation() throws Exception {
100         WebElement exampleDiv = navigateToExample("Demo-ActionField-Example4");
101         WebElement field = findElement(By.cssSelector(".uif-actionLink"), exampleDiv);
102 
103         String fieldId = field.getAttribute("id");
104 
105         assertIsVisible("#" + fieldId);
106         waitAndClickByLinkText(field.getText());
107 
108         assertElementPresent(".uif-errorMessageItem a");
109     }
110 
111     protected void testActionFieldImages() throws Exception {
112         WebElement exampleDiv = navigateToExample("Demo-ActionField-Example5");
113         List<WebElement> fields = exampleDiv.findElements(By.cssSelector("a.uif-actionLink"));
114 
115         assertEquals(2, fields.size());
116 
117         WebElement leftField = fields.get(0);
118         WebElement rightField = fields.get(1);
119 
120         String leftFieldId = leftField.getAttribute("id");
121         String rightFieldId = rightField.getAttribute("id");
122 
123         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
124         XPath xPathFactory = XPathFactory.newInstance().newXPath();
125         Document document = builder.parse(IOUtils.toInputStream(driver.getPageSource()));
126 
127         Node leftFieldImg = (Node) xPathFactory.evaluate("//a[@id='" + leftFieldId + "']/img", document,
128                 XPathConstants.NODE);
129         Node leftFieldImgNextSibling = leftFieldImg.getNextSibling();
130         if (!leftFieldImgNextSibling.getTextContent().contains("Action Link with left image")) {
131             fail("Image is not on the left of the link");
132         }
133 
134         Node rightFieldText = (Node) xPathFactory.evaluate(
135                 "//a[@id='" + rightFieldId + "']/text()[contains(., 'Action Link with right image')]", document,
136                 XPathConstants.NODE);
137         Node rightFieldTextNextSibling = rightFieldText.getNextSibling();
138         if (!rightFieldTextNextSibling.getNodeName().equals("img")) {
139             fail("Image is not on the right of the link");
140         }
141     }
142 
143     protected void testActionFieldButtons() throws Exception {
144         WebElement exampleDiv = navigateToExample("Demo-ActionField-Example6");
145         List<WebElement> fields = exampleDiv.findElements(By.cssSelector("button.btn-primary"));
146 
147         assertEquals(7, fields.size());
148 
149         String buttonFieldId = fields.get(0).getAttribute("id");
150         String imageBottomFieldId = fields.get(1).getAttribute("id");
151         String imageTopFieldId = fields.get(2).getAttribute("id");
152         String imageLeftFieldId = fields.get(3).getAttribute("id");
153         String imageRightFieldId = fields.get(4).getAttribute("id");
154 
155         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
156         XPath xPathFactory = XPathFactory.newInstance().newXPath();
157         Document document = builder.parse(IOUtils.toInputStream(driver.getPageSource()));
158 
159         assertIsVisible("#" + buttonFieldId);
160         waitAndClickButtonByText(fields.get(0).getText());
161         assertTrue(driver.switchTo().alert().getText().contains("You clicked a button"));
162         alertAccept();
163 
164         assertElementPresent("#" + imageBottomFieldId + " span.topBottomSpan img[src*='searchicon.png']");
165         Node topFieldText = (Node) xPathFactory.evaluate(
166                 "//button[@id='" + imageBottomFieldId + "']/text()[contains(., 'Image BOTTOM')]", document,
167                 XPathConstants.NODE);
168         Node topFieldTextNextSibling = topFieldText.getNextSibling();
169         if (!topFieldTextNextSibling.getNodeName().equals("span")) {
170             fail("Image is not on the bottom of the text");
171         }
172 
173         assertElementPresent("#" + imageTopFieldId + " span.topBottomSpan img[src*='searchicon.png']");
174         Node bottomFieldText = (Node) xPathFactory.evaluate(
175                 "//button[@id='" + imageTopFieldId + "']/text()[contains(., 'Image TOP')]", document,
176                 XPathConstants.NODE);
177         Node bottomFieldImgNextSibling = bottomFieldText.getPreviousSibling();
178         if (!bottomFieldImgNextSibling.getNodeName().contains("span")) {
179             fail("Image is not on the top of the text");
180         }
181 
182         Node leftFieldImg = (Node) xPathFactory.evaluate("//button[@id='" + imageLeftFieldId + "']/img", document,
183                 XPathConstants.NODE);
184         Node leftFieldImgNextSibling = leftFieldImg.getNextSibling();
185         if (!leftFieldImgNextSibling.getTextContent().contains("Image LEFT")) {
186             fail("Image is not on the left of the text");
187         }
188 
189         Node rightFieldText = (Node) xPathFactory.evaluate(
190                 "//button[@id='" + imageRightFieldId + "']/text()[contains(., 'Image RIGHT')]", document,
191                 XPathConstants.NODE);
192         Node rightFieldTextNextSibling = rightFieldText.getNextSibling();
193         if (!rightFieldTextNextSibling.getNodeName().equals("img")) {
194             fail("Image is not on the right of the text");
195         }
196 
197         driver.findElement(By.xpath(
198                 "//button[contains(text(),'Disabled Button') and @disabled]/preceding-sibling::button/img"));
199         driver.findElement(By.xpath("//button/img[contains(@alt,'Image Only button')]"));
200 
201         driver.findElement(By.xpath("//button[contains(text(),'Disabled Button') and @disabled]"));
202     }
203 
204     protected void testActionFieldExamples() throws Exception {
205         testActionFieldDefault();
206         testActionFieldSuccessCallback();
207         testActionFieldValidation();
208         testActionFieldImages();
209         testActionFieldButtons();
210         testActionFieldPresubmit();
211     }
212 
213     @Test
214     public void testActionFieldExamplesBookmark() throws Exception {
215         testActionFieldExamples();
216         passed();
217     }
218 
219     @Test
220     public void testActionFieldExamplesNav() throws Exception {
221         testActionFieldExamples();
222         passed();
223     }
224 }