001    package edu.samplu.common;
002    
003    import org.junit.After;
004    import org.junit.Assert;
005    import org.junit.Before;
006    import org.junit.BeforeClass;
007    import org.junit.Rule;
008    import org.junit.rules.TestName;
009    import org.openqa.selenium.By;
010    import org.openqa.selenium.WebDriver;
011    import org.openqa.selenium.WebElement;
012    import org.openqa.selenium.chrome.ChromeDriverService;
013    import org.openqa.selenium.remote.RemoteWebDriver;
014    
015    import java.io.BufferedReader;
016    import java.io.InputStreamReader;
017    import java.net.HttpURLConnection;
018    import java.net.URL;
019    import java.util.List;
020    import java.util.concurrent.TimeUnit;
021    
022    import static com.thoughtworks.selenium.SeleneseTestBase.fail;
023    import static org.junit.Assert.assertEquals;
024    
025    /**
026     * Class to upgrade UpgradedSeleniumITBase tests to WebDriver.
027     * @deprecated Use WebDriverITBase for new tests.
028     * @author Kuali Rice Team (rice.collab@kuali.org)
029     */
030    public abstract class WebDriverLegacyITBase { //implements com.saucelabs.common.SauceOnDemandSessionIdProvider {
031    
032        public static final int DEFAULT_WAIT_SEC = 60;
033        public static final String REMOTE_PUBLIC_USERPOOL_PROPERTY = "remote.public.userpool";
034    
035        public abstract String getTestUrl();
036    
037        protected WebDriver driver;
038        protected String user = "admin";
039        protected boolean passed = false;
040        static ChromeDriverService chromeDriverService;
041    
042        public @Rule TestName testName= new TestName();
043    
044        String sessionId = null;
045    
046        public String getSessionId() {
047            return sessionId;
048        }
049    
050        @BeforeClass
051        public static void createAndStartService() throws Exception {
052            chromeDriverService = WebDriverUtil.createAndStartService();
053            if (chromeDriverService != null) chromeDriverService.start();
054        }
055    
056        /**
057         * Setup the WebDriver test, login and load the tested web page
058         *
059         * @throws Exception
060         */
061        @Before
062        public void setUp() throws Exception {
063            // {"test":"1","user":"1"}
064            try {
065                if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) {
066                    String userResponse = getHTML(ITUtil.prettyHttp(System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + this.toString().trim()));
067                    user = userResponse.substring(userResponse.lastIndexOf(":" ) + 2, userResponse.lastIndexOf("\""));
068                }
069                driver = WebDriverUtil.setUp(getUserName(), ITUtil.getBaseUrlString() + "/" + getTestUrl(),
070                        getClass().getSimpleName(), testName);
071                this.sessionId = ((RemoteWebDriver)driver).getSessionId().toString();
072            } catch (Exception e) {
073                fail("Exception in setUp " + e.getMessage());
074                e.printStackTrace();
075            }
076        }
077    
078        @After
079        public void tearDown() throws Exception {
080            try {
081    //            if (System.getProperty(SauceLabsWebDriverHelper.SAUCE_PROPERTY) != null) {
082    //                SauceLabsWebDriverHelper.tearDown(passed, sessionId, System.getProperty(SauceLabsWebDriverHelper.SAUCE_USER_PROPERTY), System.getProperty(SauceLabsWebDriverHelper.SAUCE_KEY_PROPERTY));
083    //            }
084                if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) {
085                    getHTML(ITUtil.prettyHttp(System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + this.toString() + "&user=" + user));
086                }
087            } catch (Exception e) {
088                System.out.println("Exception in tearDown " + e.getMessage());
089                e.printStackTrace();
090            } finally {
091                if (driver != null) {
092                    driver.close();
093                    driver.quit();
094                } else {
095                    System.out.println("WebDriver is null, has sauceleabs been uncommented in WebDriverUtil.java?");
096                }
097            }
098        }
099    
100       protected String getHTML(String urlToRead) {
101          URL url;
102          HttpURLConnection conn;
103          BufferedReader rd;
104          String line;
105          String result = "";
106          try {
107             url = new URL(urlToRead);
108             conn = (HttpURLConnection) url.openConnection();
109             conn.setRequestMethod("GET");
110             rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
111             while ((line = rd.readLine()) != null) {
112                result += line;
113             }
114             rd.close();
115          } catch (Exception e) {
116             e.printStackTrace();
117          }
118          return result;
119       }
120    
121        protected void passed() {
122            passed = true;
123        }
124    
125        protected void assertElementPresentByName(String name) {
126            driver.findElement(By.name(name));
127        }
128        
129        protected void assertElementPresentByName(String name,String message) {
130            try{
131                    driver.findElement(By.name(name));
132            }catch(Exception e){
133                    Assert.fail(name+ " not present "+ message);                
134            }
135        }
136    
137        protected void assertElementPresentByXpath(String locator) {
138            driver.findElement(By.xpath(locator));
139        }
140        
141        protected void assertElementPresentByXpath(String locator,String message) {
142            try{
143                    driver.findElement(By.xpath(locator));
144            }catch(Exception e){
145                    Assert.fail(locator+ " not present "+ message);                
146            }
147        }
148        
149        protected void assertElementPresent(String locator) {
150            driver.findElement(By.cssSelector(locator));
151        }
152        
153        protected void assertTextPresent(String text) {
154            assertTextPresent(text, "");
155        }
156    
157        protected void assertTextPresent(String text, String message) {
158            if (!driver.getPageSource().contains(text)) {
159                Assert.fail(text + " not present " + message);
160            }
161        }
162    
163        protected void blanketApproveTest() throws InterruptedException {
164            ITUtil.checkForIncidentReport(driver.getPageSource(), "methodToCall.blanketApprove", "");
165            waitAndClickByName("methodToCall.blanketApprove", "No blanket approve button does the user " + getUserName() + " have permission?");
166            Thread.sleep(2000);
167    
168            if (driver.findElements(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).size()>0) {
169                String errorText = driver.findElement(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).getText();
170                if (errorText != null && errorText.contains("error(s) found on page.")) {
171                    errorText = ITUtil.blanketApprovalCleanUpErrorText(errorText);
172                    if (driver.findElements(By.xpath(ITUtil.DIV_EXCOL_LOCATOR)).size()>0) { // not present if errors are at the bottom of the page (see left-errmsg below)
173                        errorText = ITUtil.blanketApprovalCleanUpErrorText(driver.findElement(By.xpath(ITUtil.DIV_EXCOL_LOCATOR)).getText()); // replacing errorText as DIV_EXCOL_LOCATOR includes the error count
174                    }
175    
176                    //                if (selenium.isElementPresent("//div[@class='left-errmsg']/div")) {
177                    //                    errorText = errorText + " " + selenium.getText("//div[@class='left-errmsg']/div/div[1]");
178                    //                }
179                    Assert.fail(errorText);
180                }
181            }
182            ITUtil.checkForIncidentReport(driver.getPageSource(), "//img[@alt='doc search']", "Blanket Approve failure");
183            waitAndClickByXpath("//img[@alt='doc search']");
184            assertEquals("Kuali Portal Index", driver.getTitle());
185            selectFrame("iframeportlet");
186            waitAndClickByXpath("//input[@name='methodToCall.search' and @value='search']");
187        }
188    
189        protected void checkForIncidentReport() {
190            checkForIncidentReport("", "");
191        }
192    
193        protected void checkForIncidentReport(String locator) {
194            checkForIncidentReport(locator, "");
195        }
196    
197        protected void checkForIncidentReport(String locator, String message) {
198            WebDriverUtil.checkForIncidentReport(driver, locator, message);
199        }
200    
201        protected String getAttribute(By by, String attribute) throws InterruptedException {
202            waitFor(by);
203            return driver.findElement(by).getAttribute(attribute);
204        }
205        
206        /**
207         * Get value of any attribute by using element name
208         *
209         *@param name name of an element
210         *@param attribute the name of an attribute whose value is to be retrieved
211        */
212        protected String getAttributeByName(String name,String attribute) throws InterruptedException {
213            return getAttribute(By.name(name),attribute);
214        }
215        
216        /**
217         * Get value of any attribute by using element xpath
218         *
219         *@param locator locating mechanism of an element
220         *@param attribute the name of an attribute whose value is to be retrieved
221        */
222        protected String getAttributeByXpath(String locator,String attribute) throws InterruptedException {
223            return getAttribute(By.xpath(locator),attribute);
224        }
225               
226        protected String getBaseUrlString() {
227            return ITUtil.getBaseUrlString();
228        }
229    
230        protected String getText(By by)  throws InterruptedException {
231            return driver.findElement(by).getText();
232        }
233    
234        protected String getTextByName(String name) throws InterruptedException {
235            return getText(By.name(name));
236        }
237        
238        protected String getText(String locator) throws InterruptedException {
239            return getText(By.cssSelector(locator));
240        }
241    
242        protected String getTextByXpath(String locator) throws InterruptedException {
243            return getText(By.xpath(locator));
244        }
245    
246        protected String getTitle() {
247            return driver.getTitle();
248        }
249    
250        /**
251         * Override in test to define a user other than admin
252         * @return
253         */
254        public String getUserName() {
255            return user;
256        }
257    
258        protected boolean isElementPresent(By by) {
259            return (driver.findElements(by)).size()>0;
260        }
261        
262        protected boolean isElementPresentByName(String name) {
263            return isElementPresent(By.name(name));
264        }
265        
266        protected boolean isElementPresentByXpath(String locator) {
267            return isElementPresent(By.xpath(locator));
268        }
269        
270        protected void open(String url) {
271            driver.get(url);
272        }
273    
274        protected void selectFrame(String locator) {
275            driver.switchTo().frame(locator);
276        }
277        
278        protected void selectTopFrame() {
279            driver.switchTo().defaultContent();
280        }
281        
282        protected void selectWindow(String locator) {
283            driver.switchTo().window(locator);
284        }
285    
286        protected String waitForDocId() throws InterruptedException {
287            waitForElementPresentByXpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]");
288            return driver.findElement(By.xpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]")).getText();
289        }
290        
291        protected void waitForElementPresent(String locator) throws InterruptedException {
292            waitFor(By.cssSelector(locator));
293        }
294    
295        protected void waitForElementPresentByXpath(String locator) throws InterruptedException {
296            waitFor(By.xpath(locator));
297        }
298        
299        protected void waitForElementPresentByName(String name) throws InterruptedException {
300            waitFor(By.name(name));
301        }
302    
303        protected void waitForTitleToEqualKualiPortalIndex() throws InterruptedException {
304            waitForTitleToEqualKualiPortalIndex("");
305        }
306    
307        protected void waitForTitleToEqualKualiPortalIndex(String message) throws InterruptedException {
308            boolean failed = false;
309            for (int second = 0;; second++) {
310                Thread.sleep(1000);
311                if (second >= 60) failed = true;
312                try { if (failed || ITUtil.KUALI_PORTAL_TITLE.equals(driver.getTitle())) break; } catch (Exception e) {}
313            }
314            WebDriverUtil.checkForIncidentReport(driver, message); // after timeout to be sure page is loaded
315            if (failed) fail("timeout of " + 60 + " seconds " + message);
316        }
317    
318        protected void waitAndClick(String locator) throws InterruptedException {
319            waitAndClick(locator, "");
320        }
321    
322        protected void waitForPageToLoad() {
323            // noop webdriver doesn't it need it, except when it does...
324        }
325    
326        protected void waitFor(By by) throws InterruptedException {
327            waitFor(by, "");
328        }
329    
330        protected void waitFor(By by, String message) throws InterruptedException {
331    //        for (int second = 0;; second++) {
332                Thread.sleep(1000);
333            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
334    
335    //            if (second >= DEFAULT_WAIT_SEC) fail(by.toString() + " " + message + " " + DEFAULT_WAIT_SEC + " sec timeout.");
336                try { driver.findElement(by);
337                    //break;
338                } catch (Exception e) {}
339            driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
340    //        }
341        }
342    
343        protected void waitAndClick(By by) throws InterruptedException {
344            waitAndClick(by, "");
345        }
346    
347        protected void waitAndClick(By by, String message) throws InterruptedException {
348            waitFor(by, message);
349            try {
350                (driver.findElement(by)).click();
351            } catch (Exception e) {
352                fail(e.getMessage() + " " + by.toString() + " " + message);
353                e.printStackTrace();
354            }
355        }
356    
357        protected void waitAndClick(String locator, String message) throws InterruptedException {
358            waitAndClick(By.cssSelector(locator), message);
359        }
360    
361        protected void waitAndClickByLinkText(String text) throws InterruptedException {
362            waitAndClick(By.linkText(text),"");
363        }
364    
365        protected void waitAndClickByLinkText(String text, String message) throws InterruptedException {
366            waitAndClick(By.linkText(text), message);
367        }
368    
369        protected void waitAndClickByName(String name) throws InterruptedException {
370            waitAndClick(By.name(name), "");
371        }
372    
373        protected void waitAndClickByXpath(String xpath) throws InterruptedException {
374            waitAndClick(By.xpath(xpath));
375        }
376    
377        protected void waitAndClickByName(String name, String message) throws InterruptedException {
378            waitAndClick(By.name(name), message);
379        }
380        
381        protected void waitAndClickByXpath(String xpath, String message) throws InterruptedException {
382            waitAndClick(By.xpath(xpath), message);
383        }
384    
385        protected void waitAndType(By by, String text) throws InterruptedException {
386            waitFor(by, "");
387            try {
388                (driver.findElement(by)).sendKeys(text);
389            } catch (Exception e) {
390                fail(e.getMessage() + " " + by.toString() + " " + text);
391                e.printStackTrace();
392            }
393        }
394        
395        protected void waitAndType(By by, String text, String message) throws InterruptedException {
396            waitFor(by, "");
397            try {
398                (driver.findElement(by)).sendKeys(text);
399            } catch (Exception e) {
400                fail(e.getMessage() + " " + by.toString() + " " + text + "  "+message);
401                e.printStackTrace();
402            }
403        }
404        
405        protected void waitAndTypeByXpath(String locator, String text) throws InterruptedException {
406            waitAndType(By.xpath(locator), text);
407        }
408        
409        protected void waitAndTypeByXpath(String locator, String text, String message) throws InterruptedException {
410            waitAndType(By.xpath(locator), text, message);
411        }
412        
413        protected void waitAndTypeByName(String name, String text) throws InterruptedException {
414            waitAndType(By.name(name), text);
415        }
416        
417        protected void selectByXpath(String locator, String select) throws InterruptedException {
418            select(By.xpath(locator), select);
419        }
420        
421        protected void selectByName(String name, String select) throws InterruptedException {
422            select(By.name(name), select);
423        }
424        
425        protected void select(By by, String select)  throws InterruptedException {
426            WebElement select1 = driver.findElement(by);
427            List<WebElement> options = select1.findElements(By.tagName("option"));
428            for(WebElement option : options){
429                if(option.getText().equals(select)){
430                    option.click();
431                    break;
432                }
433            }
434        }
435        
436        protected String[] getSelectOptions(By by) throws InterruptedException {
437            WebElement select1 = driver.findElement(by);
438            List<WebElement> options = select1.findElements(By.tagName("option"));
439            String[] optionValues = new String[options.size()];
440            int counter=0;
441            for(WebElement option : options){
442                optionValues[counter] = option.getAttribute("value");
443                counter++;
444            }
445            return optionValues;
446        }
447        
448        protected String[] getSelectOptionsByName(String name) throws InterruptedException {
449            return getSelectOptions(By.name(name));
450        }
451        
452        protected String[] getSelectOptionsByXpath(String locator) throws InterruptedException {
453            return getSelectOptions(By.xpath(locator));
454        }
455        
456        protected int getCssCount(String selector) {
457            return getCssCount(By.cssSelector(selector));
458        }
459        
460        protected int getCssCount(By by) {
461            return (driver.findElements(by)).size();
462        }
463        
464        protected void checkErrorMessageItem(String message)
465        {
466            final String error_locator = "//li[@class='uif-errorMessageItem']";
467            assertElementPresentByXpath(error_locator);
468            String errorText=null;
469            try {
470                errorText = getTextByXpath(error_locator);
471            } catch (InterruptedException e) {
472                e.printStackTrace();
473            }
474            if (errorText != null && errorText.contains("errors")) {
475                Assert.fail(errorText + message);
476            }
477                   
478        }
479        
480        protected boolean isVisibleByXpath(String locator) {
481            return isVisible(By.xpath(locator));
482        }
483        
484        
485        protected boolean isVisible(By by) {
486            return driver.findElement(by).isDisplayed();
487        }
488        
489        protected void waitNotVisibleByXpath(String locator) throws InterruptedException {
490            for (int second = 0;; second++) {
491                if (second >= 15) {
492                    Assert.fail("timeout");
493                }
494    
495                if (!isVisibleByXpath(locator)) {
496                    break;
497                }
498    
499                Thread.sleep(1000);
500            }
501        }
502    
503        protected void waitIsVisibleByXpath(String locator) throws InterruptedException {
504            for (int second = 0;; second++) {
505                if (second >= 15) {
506                    Assert.fail("timeout");
507                }
508                if (isVisibleByXpath(locator)) {
509                    break;
510                }
511                Thread.sleep(1000);
512            }
513        }
514        
515        protected void colapseExpandByXpath(String clickLocator, String visibleLocator) throws InterruptedException {
516            waitAndClickByXpath(clickLocator);
517            waitNotVisibleByXpath(visibleLocator);
518    
519            waitAndClickByXpath(clickLocator);
520            waitIsVisibleByXpath(visibleLocator);
521        }
522    
523        protected void expandColapseByXpath(String clickLocator, String visibleLocator) throws InterruptedException {
524            waitAndClickByXpath(clickLocator);
525            waitIsVisibleByXpath(visibleLocator);
526    
527            waitAndClickByXpath(clickLocator);
528            waitNotVisibleByXpath(visibleLocator);
529        }
530    }