View Javadoc

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