001    package edu.samplu.krad.demo.uif.library;
002    
003    import org.apache.commons.io.IOUtils;
004    import org.junit.Test;
005    import org.openqa.selenium.By;
006    import org.openqa.selenium.WebElement;
007    import org.w3c.dom.Document;
008    import org.w3c.dom.Node;
009    
010    import javax.xml.parsers.DocumentBuilder;
011    import javax.xml.parsers.DocumentBuilderFactory;
012    import javax.xml.xpath.XPath;
013    import javax.xml.xpath.XPathConstants;
014    import javax.xml.xpath.XPathFactory;
015    import java.util.List;
016    
017    import static org.junit.Assert.assertEquals;
018    import static org.junit.Assert.assertTrue;
019    
020    /**
021     * @author Kuali Rice Team (rice.collab@kuali.org)
022     */
023    public class DemoLibraryFieldsActionSmokeTest extends DemoLibraryBase {
024    
025        /**
026         * /kr-krad/kradsampleapp?viewId=Demo-ActionField-View&methodToCall=start
027         */
028        public static final String BOOKMARK_URL = "/kr-krad/kradsampleapp?viewId=Demo-ActionField-View&methodToCall=start";
029    
030        @Override
031        public String getBookmarkUrl() {
032            return BOOKMARK_URL;
033        }
034    
035        @Override
036        protected void navigate() throws Exception {
037            navigateToLibraryDemo("Fields", "Action Field");
038        }
039    
040        protected void testActionFieldDefault() throws Exception {
041            WebElement exampleDiv = navigateToExample("Demo-ActionField-Example1");
042            WebElement field = findElement(By.cssSelector(".uif-actionLink"), exampleDiv);
043    
044            String fieldId = field.getAttribute("id");
045    
046            assertIsVisible("#" + fieldId);
047            waitAndClickByLinkText(field.getText());
048    
049            assertTrue(driver.switchTo().alert().getText().contains("You clicked the link"));
050            alertAccept();
051        }
052    
053        protected void testActionFieldPresubmit() throws Exception {
054            WebElement exampleDiv = navigateToExample("Demo-ActionField-Example2");
055            List<WebElement> fields = exampleDiv.findElements(By.cssSelector("a.uif-actionLink"));
056    
057            assertEquals("Two action links do not exist on the page", 2, fields.size());
058    
059            assertElementPresentByLinkText("Pre submit returning true Link");
060            waitAndClickByLinkText("Pre submit returning true Link");
061            assertTrue(driver.switchTo().alert().getText().contains("Pre submit call was invoked, returning true"));
062            driver.switchTo().alert().accept();
063    
064            assertElementPresentByLinkText("Pre submit returning false Link");
065            waitAndClickByLinkText("Pre submit returning false Link");
066            assertTrue(driver.switchTo().alert().getText().contains("Pre submit call was invoked, returning false"));
067            driver.switchTo().alert().accept();
068        }
069    
070        protected void testActionFieldSuccessCallback() throws Exception {
071            WebElement exampleDiv = navigateToExample("Demo-ActionField-Example3");
072            WebElement field = findElement(By.cssSelector(".uif-actionLink"), exampleDiv);
073    
074            String fieldId = field.getAttribute("id");
075    
076            assertIsVisible("#" + fieldId);
077            waitAndClickByLinkText(field.getText());
078    
079            assertTrue(driver.switchTo().alert().getText().contains("Refresh called successfully"));
080            alertAccept();
081        }
082    
083        protected void testActionFieldValidation() throws Exception {
084            WebElement exampleDiv = navigateToExample("Demo-ActionField-Example4");
085            WebElement field = findElement(By.cssSelector(".uif-actionLink"), exampleDiv);
086    
087            String fieldId = field.getAttribute("id");
088    
089            assertIsVisible("#" + fieldId);
090            waitAndClickByLinkText(field.getText());
091    
092            assertElementPresent(".uif-errorMessageItem a");
093        }
094    
095        protected void testActionFieldImages() throws Exception {
096            WebElement exampleDiv = navigateToExample("Demo-ActionField-Example5");
097            List<WebElement> fields = exampleDiv.findElements(By.cssSelector("a.uif-actionLink"));
098    
099            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    }