View Javadoc
1   /**
2    * Copyright 2005-2016 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.LibraryBase;
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 LibraryFieldsActionAft extends LibraryBase {
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          waitForProgressLoading();
79  
80          assertElementPresentByLinkText("Pre submit returning false Link");
81          waitAndClickByLinkText("Pre submit returning false Link");
82          assertTrue(driver.switchTo().alert().getText().contains("Pre submit call was invoked, returning false"));
83          driver.switchTo().alert().accept();
84      }
85  
86      protected void testActionFieldSuccessCallback() throws Exception {
87          WebElement exampleDiv = navigateToExample("Demo-ActionField-Example3");
88          WebElement field = findElement(By.cssSelector(".uif-actionLink"), exampleDiv);
89  
90          String fieldId = field.getAttribute("id");
91  
92          assertIsVisible("#" + fieldId);
93          waitAndClickByLinkText(field.getText());
94  
95          Thread.sleep(1500); // give alert time to be triggered
96          assertTrue(driver.switchTo().alert().getText().contains("Refresh called successfully"));
97          alertAccept();
98      }
99  
100     protected void testActionFieldValidation() throws Exception {
101         WebElement exampleDiv = navigateToExample("Demo-ActionField-Example4");
102         WebElement field = findElement(By.cssSelector(".uif-actionLink"), exampleDiv);
103 
104         String fieldId = field.getAttribute("id");
105 
106         assertIsVisible("#" + fieldId);
107         waitAndClickByLinkText(field.getText());
108 
109         assertElementPresent(".uif-errorMessageItem a");
110     }
111 
112     protected void testActionFieldImages() throws Exception {
113         WebElement exampleDiv = navigateToExample("Demo-ActionField-Example5");
114         List<WebElement> fields = exampleDiv.findElements(By.cssSelector("a.uif-actionLink"));
115 
116         assertEquals(2, fields.size());
117 
118         WebElement leftField = fields.get(0);
119         WebElement rightField = fields.get(1);
120 
121         String leftFieldId = leftField.getAttribute("id");
122         String rightFieldId = rightField.getAttribute("id");
123 
124         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
125         XPath xPathFactory = XPathFactory.newInstance().newXPath();
126         Document document = builder.parse(IOUtils.toInputStream(driver.getPageSource()));
127 
128         Node leftFieldImg = (Node) xPathFactory.evaluate("//a[@id='" + leftFieldId + "']/img", document,
129                 XPathConstants.NODE);
130         Node leftFieldImgNextSibling = leftFieldImg.getNextSibling();
131         if (!leftFieldImgNextSibling.getTextContent().contains("Action Link with left image")) {
132             fail("Image is not on the left of the link");
133         }
134 
135         Node rightFieldText = (Node) xPathFactory.evaluate(
136                 "//a[@id='" + rightFieldId + "']/text()[contains(., 'Action Link with right image')]", document,
137                 XPathConstants.NODE);
138         Node rightFieldTextNextSibling = rightFieldText.getNextSibling();
139         if (!rightFieldTextNextSibling.getNodeName().equals("img")) {
140             fail("Image is not on the right of the link");
141         }
142     }
143 
144     protected void testActionFieldButtons() throws Exception {
145         WebElement exampleDiv = navigateToExample("Demo-ActionField-Example6");
146         List<WebElement> fields = exampleDiv.findElements(By.cssSelector("button.btn-primary"));
147 
148         assertEquals(7, fields.size());
149 
150         String buttonFieldId = fields.get(0).getAttribute("id");
151         String imageBottomFieldId = fields.get(1).getAttribute("id");
152         String imageTopFieldId = fields.get(2).getAttribute("id");
153         String imageLeftFieldId = fields.get(3).getAttribute("id");
154         String imageRightFieldId = fields.get(4).getAttribute("id");
155 
156         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
157         XPath xPathFactory = XPathFactory.newInstance().newXPath();
158         Document document = builder.parse(IOUtils.toInputStream(driver.getPageSource()));
159 
160         assertIsVisible("#" + buttonFieldId);
161         waitAndClickButtonByText(fields.get(0).getText());
162         assertTrue(driver.switchTo().alert().getText().contains("You clicked a button"));
163         alertAccept();
164 
165         assertElementPresent("#" + imageBottomFieldId + " span.topBottomSpan img[src*='searchicon.png']");
166         Node topFieldText = (Node) xPathFactory.evaluate(
167                 "//button[@id='" + imageBottomFieldId + "']/text()[contains(., 'Image BOTTOM')]", document,
168                 XPathConstants.NODE);
169         Node topFieldTextNextSibling = topFieldText.getNextSibling();
170         if (!topFieldTextNextSibling.getNodeName().equals("span")) {
171             fail("Image is not on the bottom of the text");
172         }
173 
174         assertElementPresent("#" + imageTopFieldId + " span.topBottomSpan img[src*='searchicon.png']");
175         Node bottomFieldText = (Node) xPathFactory.evaluate(
176                 "//button[@id='" + imageTopFieldId + "']/text()[contains(., 'Image TOP')]", document,
177                 XPathConstants.NODE);
178         Node bottomFieldImgNextSibling = bottomFieldText.getPreviousSibling();
179         if (!bottomFieldImgNextSibling.getNodeName().contains("span")) {
180             fail("Image is not on the top of the text");
181         }
182 
183         Node leftFieldImg = (Node) xPathFactory.evaluate("//button[@id='" + imageLeftFieldId + "']/img", document,
184                 XPathConstants.NODE);
185         Node leftFieldImgNextSibling = leftFieldImg.getNextSibling();
186         if (!leftFieldImgNextSibling.getTextContent().contains("Image LEFT")) {
187             fail("Image is not on the left of the text");
188         }
189 
190         Node rightFieldText = (Node) xPathFactory.evaluate(
191                 "//button[@id='" + imageRightFieldId + "']/text()[contains(., 'Image RIGHT')]", document,
192                 XPathConstants.NODE);
193         Node rightFieldTextNextSibling = rightFieldText.getNextSibling();
194         if (!rightFieldTextNextSibling.getNodeName().equals("img")) {
195             fail("Image is not on the right of the text");
196         }
197 
198         driver.findElement(By.xpath(
199                 "//button[contains(text(),'Disabled Button') and @disabled]/preceding-sibling::button/img"));
200         driver.findElement(By.xpath("//button/img[contains(@alt,'Image Only button')]"));
201 
202         driver.findElement(By.xpath("//button[contains(text(),'Disabled Button') and @disabled]"));
203     }
204 
205     protected void testActionFieldExamples() throws Exception {
206         testActionFieldDefault();
207         testActionFieldSuccessCallback();
208         testActionFieldValidation();
209         testActionFieldImages();
210         testActionFieldButtons();
211         testActionFieldPresubmit();
212     }
213 
214     @Test
215     public void testActionFieldExamplesBookmark() throws Exception {
216         testActionFieldExamples();
217         passed();
218     }
219 
220     @Test
221     public void testActionFieldExamplesNav() throws Exception {
222         testActionFieldExamples();
223         passed();
224     }
225 }