001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.samplu.common;
017    
018    import org.junit.After;
019    import org.junit.Assert;
020    import org.junit.Before;
021    import org.junit.BeforeClass;
022    import org.junit.Rule;
023    import org.junit.rules.TestName;
024    import org.openqa.selenium.By;
025    import org.openqa.selenium.JavascriptExecutor;
026    import org.openqa.selenium.NoSuchFrameException;
027    import org.openqa.selenium.WebDriver;
028    import org.openqa.selenium.WebElement;
029    import org.openqa.selenium.chrome.ChromeDriverService;
030    import org.openqa.selenium.interactions.Actions;
031    import org.openqa.selenium.remote.RemoteWebDriver;
032    
033    import java.io.BufferedReader;
034    import java.io.InputStreamReader;
035    import java.net.HttpURLConnection;
036    import java.net.URL;
037    import java.util.List;
038    import java.util.Set;
039    import java.util.concurrent.TimeUnit;
040    
041    import static com.thoughtworks.selenium.SeleneseTestBase.fail;
042    import static org.junit.Assert.assertEquals;
043    
044    /**
045     * Class to upgrade UpgradedSeleniumITBase tests to WebDriver.
046     * @deprecated Use WebDriverITBase for new tests.
047     * @author Kuali Rice Team (rice.collab@kuali.org)
048     */
049    public abstract class WebDriverLegacyITBase { //implements com.saucelabs.common.SauceOnDemandSessionIdProvider {
050    
051        public static final int DEFAULT_WAIT_SEC = 60;
052        public static final String REMOTE_PUBLIC_USERPOOL_PROPERTY = "remote.public.userpool";
053        public static final String REMOTE_PUBLIC_USER_PROPERTY = "remote.public.user";
054    
055        public abstract String getTestUrl();
056    
057        protected WebDriver driver;
058        protected String user = "admin";
059        protected boolean passed = false;
060        static ChromeDriverService chromeDriverService;
061    
062        public @Rule TestName testName= new TestName();
063    
064        String sessionId = null;
065    
066        public String getSessionId() {
067            return sessionId;
068        }
069    
070        @BeforeClass
071        public static void createAndStartService() throws Exception {
072            chromeDriverService = WebDriverUtil.createAndStartService();
073            if (chromeDriverService != null) chromeDriverService.start();
074        }
075    
076        /**
077         * Setup the WebDriver test, login and load the tested web page
078         *
079         * @throws Exception
080         */
081        @Before
082        public void setUp() throws Exception {
083            // {"test":"1","user":"1"}
084            try {
085                if (System.getProperty(REMOTE_PUBLIC_USER_PROPERTY) != null) {
086                    user = System.getProperty(REMOTE_PUBLIC_USER_PROPERTY);
087                } else if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) { // deprecated
088                    String userResponse = getHTML(ITUtil.prettyHttp(System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + this.toString().trim()));
089                    user = userResponse.substring(userResponse.lastIndexOf(":" ) + 2, userResponse.lastIndexOf("\""));
090                }
091                driver = WebDriverUtil.setUp(getUserName(), ITUtil.getBaseUrlString() + getTestUrl(), getClass().getSimpleName(), testName);
092                this.sessionId = ((RemoteWebDriver)driver).getSessionId().toString();
093            } catch (Exception e) {
094                fail("Exception in setUp " + e.getMessage());
095                e.printStackTrace();
096            }
097            ITUtil.login(driver, user);
098        }
099    
100        @After
101        public void tearDown() throws Exception {
102            try {
103    //            if (System.getProperty(SauceLabsWebDriverHelper.SAUCE_PROPERTY) != null) {
104    //                SauceLabsWebDriverHelper.tearDown(passed, sessionId, System.getProperty(SauceLabsWebDriverHelper.SAUCE_USER_PROPERTY), System.getProperty(SauceLabsWebDriverHelper.SAUCE_KEY_PROPERTY));
105    //            }
106                if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) {
107                    getHTML(ITUtil.prettyHttp(System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + this.toString() + "&user=" + user));
108                }
109            } catch (Exception e) {
110                System.out.println("Exception in tearDown " + e.getMessage());
111                e.printStackTrace();
112            } finally {
113                if (driver != null) {
114                    if (ITUtil.dontTearDownPropertyNotSet()) {
115                        driver.close();
116                        driver.quit();
117                    }
118                } else {
119                    System.out.println("WebDriver is null, if using saucelabs, has sauceleabs been uncommented in WebDriverUtil.java?  If using a remote hub did you include the port?");
120                }
121            }
122        }
123    
124       protected String getHTML(String urlToRead) {
125          URL url;
126          HttpURLConnection conn;
127          BufferedReader rd;
128          String line;
129          String result = "";
130          try {
131             url = new URL(urlToRead);
132             conn = (HttpURLConnection) url.openConnection();
133             conn.setRequestMethod("GET");
134             rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
135             while ((line = rd.readLine()) != null) {
136                result += line;
137             }
138             rd.close();
139          } catch (Exception e) {
140             e.printStackTrace();
141          }
142          return result;
143       }
144    
145        protected void passed() {
146            passed = true;
147        }
148    
149        protected void assertElementPresentByName(String name) {
150            driver.findElement(By.name(name));
151        }
152        
153        protected void assertElementPresentByName(String name,String message) {
154            try{
155                    driver.findElement(By.name(name));
156            }catch(Exception e){
157                    Assert.fail(name+ " not present "+ message);
158            }
159        }
160    
161        protected void assertElementPresentByXpath(String locator) {
162            driver.findElement(By.xpath(locator));
163        }
164        
165        protected void assertElementPresentByXpath(String locator,String message) {
166            try{
167                    driver.findElement(By.xpath(locator));
168            }catch(Exception e){
169                    Assert.fail(locator+ " not present "+ message);                
170            }
171        }
172        
173        protected void assertElementPresent(String locator) {
174            driver.findElement(By.cssSelector(locator));
175        }
176        
177        protected void assertTextPresent(String text) {
178            assertTextPresent(text, "");
179        }
180    
181        protected void assertTextPresent(String text, String message) {
182            if (!driver.getPageSource().contains(text)) {
183                Assert.fail(text + " not present " + message);
184            }
185        }
186    
187        protected void blanketApproveTest() throws InterruptedException {
188            ITUtil.checkForIncidentReport(driver.getPageSource(), "methodToCall.blanketApprove", "");
189            waitAndClickByName("methodToCall.blanketApprove", "No blanket approve button does the user " + getUserName() + " have permission?");
190            Thread.sleep(2000);
191    
192            if (driver.findElements(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).size()>0) {
193                String errorText = driver.findElement(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).getText();
194                if (errorText != null && errorText.contains("error(s) found on page.")) {
195                    errorText = ITUtil.blanketApprovalCleanUpErrorText(errorText);
196                    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)
197                        errorText = ITUtil.blanketApprovalCleanUpErrorText(driver.findElement(By.xpath(ITUtil.DIV_EXCOL_LOCATOR)).getText()); // replacing errorText as DIV_EXCOL_LOCATOR includes the error count
198                    }
199    
200                    //                if (selenium.isElementPresent("//div[@class='left-errmsg']/div")) {
201                    //                    errorText = errorText + " " + selenium.getText("//div[@class='left-errmsg']/div/div[1]");
202                    //                }
203                    Assert.fail(errorText);
204                }
205            }
206            ITUtil.checkForIncidentReport(driver.getPageSource(), "//img[@alt='doc search']", "Blanket Approve failure");
207            waitAndClickByXpath("//img[@alt='doc search']");
208            assertEquals("Kuali Portal Index", driver.getTitle());
209            selectFrame("iframeportlet");
210            waitAndClickByXpath("//input[@name='methodToCall.search' and @value='search']");
211        }
212    
213        protected void checkForIncidentReport() {
214            checkForIncidentReport("", "");
215        }
216    
217        protected void checkForIncidentReport(String locator) {
218            checkForIncidentReport(locator, "");
219        }
220    
221        protected void checkForIncidentReport(String locator, String message) {
222            WebDriverUtil.checkForIncidentReport(driver, locator, message);
223        }
224    
225        protected void clearText(By by)  throws InterruptedException {
226            driver.findElement(by).clear();
227        }
228        
229        protected void clearText(String selector) throws InterruptedException {
230            clearText(By.cssSelector(selector));
231        }
232    
233        protected void clearTextByName(String name) throws InterruptedException {
234            clearText(By.name(name));
235        }
236    
237        protected void clearTextByXpath(String locator) throws InterruptedException {
238            clearText(By.xpath(locator));
239        }
240        
241        protected String getAttribute(By by, String attribute) throws InterruptedException {
242            waitFor(by);
243            return driver.findElement(by).getAttribute(attribute);
244        }
245        
246        /**
247         * Get value of any attribute by using element name
248         *
249         *@param name name of an element
250         *@param attribute the name of an attribute whose value is to be retrieved
251        */
252        protected String getAttributeByName(String name,String attribute) throws InterruptedException {
253            return getAttribute(By.name(name),attribute);
254        }
255        
256        /**
257         * Get value of any attribute by using element xpath
258         *
259         *@param locator locating mechanism of an element
260         *@param attribute the name of an attribute whose value is to be retrieved
261        */
262        protected String getAttributeByXpath(String locator,String attribute) throws InterruptedException {
263            return getAttribute(By.xpath(locator),attribute);
264        }
265               
266        protected String getBaseUrlString() {
267            return ITUtil.getBaseUrlString();
268        }
269    
270        protected String getText(By by)  throws InterruptedException {
271            return driver.findElement(by).getText();
272        }
273    
274        protected String getTextByName(String name) throws InterruptedException {
275            return getText(By.name(name));
276        }
277        
278        protected String getText(String locator) throws InterruptedException {
279            return getText(By.cssSelector(locator));
280        }
281    
282        protected String getTextByXpath(String locator) throws InterruptedException {
283            return getText(By.xpath(locator));
284        }
285    
286        protected String getTitle() {
287            return driver.getTitle();
288        }
289    
290        /**
291         * Override in test to define a user other than admin
292         * @return
293         */
294        public String getUserName() {
295            return user;
296        }
297    
298        /**
299         * Handles simple nested frame content; validates that a frame and nested frame exists before switching to it
300         */
301        protected void gotoNestedFrame() {
302            driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
303            driver.switchTo().defaultContent();
304            if(driver.findElements(By.xpath("//iframe")).size() > 0) {
305                WebElement containerFrame = driver.findElement(By.xpath("//iframe"));
306                driver.switchTo().frame(containerFrame);
307            }
308            if(driver.findElements(By.xpath("//iframe")).size() > 0) {
309                WebElement contentFrame = driver.findElement(By.xpath("//iframe"));
310                driver.switchTo().frame(contentFrame);
311            }
312            driver.manage().timeouts().implicitlyWait(DEFAULT_WAIT_SEC, TimeUnit.SECONDS);
313        }
314    
315        protected boolean isElementPresent(By by) {
316            return (driver.findElements(by)).size()>0;
317        }
318        
319        protected boolean isElementPresent(String locator) {
320            return (driver.findElements(By.cssSelector(locator))).size()>0;
321        }
322        
323        protected boolean isElementPresentByName(String name) {
324            return isElementPresent(By.name(name));
325        }
326        
327        protected boolean isElementPresentByXpath(String locator) {
328            return isElementPresent(By.xpath(locator));
329        }
330        
331        protected void open(String url) {
332            driver.get(url);
333        }
334    
335        protected void selectFrame(String locator) {
336            if ("iframeportlet".equals(locator)) {
337                gotoNestedFrame();
338            } else {
339                try {
340                    driver.switchTo().frame(locator);
341                } catch (NoSuchFrameException nsfe) {
342                    // don't fail
343                }
344            }
345        }
346        
347        protected void selectTopFrame() {
348            driver.switchTo().defaultContent();
349        }
350        
351        protected void selectWindow(String locator) {
352            driver.switchTo().window(locator);
353        }
354        
355        protected void close() {
356            driver.close();
357        }
358    
359        protected void testCancelConfirmation() throws InterruptedException {
360            waitAndCancelConfirmation();
361            passed();
362        }
363    
364        protected void testCreateNewSearchReturnValueCancelConfirmation() throws InterruptedException, Exception {
365            selectFrame("iframeportlet");
366            waitAndCreateNew();
367            waitAndSearch();
368            waitAndReturnValue();
369            waitAndCancelConfirmation();
370            passed();
371        }
372    
373        protected void testSearchEditCancel() throws InterruptedException {
374            selectFrame("iframeportlet");
375            waitAndSearch();
376            waitAndEdit();
377            testCancelConfirmation();
378        }
379    
380        protected void testVerifyAddDeleteFiscalOfficerLegacy() throws Exception {
381            selectFrame("iframeportlet");
382            waitAndTypeByName("document.documentHeader.documentDescription", ITUtil.DTS_TWO);
383            waitAndTypeByName("newCollectionLines['document.newMaintainableObject.dataObject.fiscalOfficer.accounts'].number", "1234567890");
384            waitAndTypeByName("newCollectionLines['document.newMaintainableObject.dataObject.fiscalOfficer.accounts'].foId", "2");
385    
386            waitAndClickByXpath("//button[@data-loadingmessage='Adding Line...']");
387    
388            assertElementPresentByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number", "https://jira.kuali.org/browse/KULRICE-8564");
389    
390            assertEquals("1234567890", getAttributeByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number","value"));
391            assertEquals("2", getAttributeByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].foId","value"));
392    
393            waitAndClickByXpath("//button[@data-loadingmessage='Deleting Line...']");
394    
395            assertElementPresentByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number");
396            passed();
397        }
398    
399        protected void waitAndCancelConfirmation() throws InterruptedException {
400            waitAndClickByName("methodToCall.cancel");
401            waitAndClickByName("methodToCall.processAnswer.button0");
402        }
403    
404        protected void waitAndCreateNew() throws InterruptedException {
405            waitAndClickByXpath("//img[@alt='create new']");
406    //        waitAndClickByXpath("//a[@title='Create a new record']");
407        }
408    
409        protected void waitAndEdit() throws InterruptedException {
410            waitAndClickByLinkText("edit");
411        }
412    
413        protected void waitAndReturnValue() throws InterruptedException {
414            waitAndClickByLinkText("return value");
415        }
416    
417        protected void waitAndSearch() throws InterruptedException {
418            waitAndClickByXpath("//input[@value='search']");
419    //        waitAndClickByXpath("//input[@name='methodToCall.search']");
420    //        waitAndClick("input[alt='search']");
421    //        waitAndClickByXpath("//input[@name='methodToCall.search' and @value='search']");
422        }
423    
424        protected String waitForDocId() throws InterruptedException {
425            waitForElementPresentByXpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]");
426            return driver.findElement(By.xpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]")).getText();
427        }
428        
429        protected void waitForElementPresent(String locator) throws InterruptedException {
430            waitFor(By.cssSelector(locator));
431        }
432    
433        protected void waitForElementPresentByXpath(String locator) throws InterruptedException {
434            waitFor(By.xpath(locator));
435        }
436        
437        protected void waitForElementPresentByName(String name) throws InterruptedException {
438            waitFor(By.name(name));
439        }
440    
441        protected void waitForTitleToEqualKualiPortalIndex() throws InterruptedException {
442            waitForTitleToEqualKualiPortalIndex("");
443        }
444    
445        protected void waitForTitleToEqualKualiPortalIndex(String message) throws InterruptedException {
446                Thread.sleep(2000);
447    // This started failing in CI....
448    //        boolean failed = false;
449    //        for (int second = 0;; second++) {
450    //            Thread.sleep(1000);
451    //            if (second >= 60) failed = true;
452    //            try { if (failed || ITUtil.KUALI_PORTAL_TITLE.equals(driver.getTitle())) break; } catch (Exception e) {}
453    //        }
454    //        WebDriverUtil.checkForIncidentReport(driver, message); // after timeout to be sure page is loaded
455    //        if (failed) fail("timeout of " + 60 + " seconds " + message);
456        }
457    
458        protected void waitAndClick(String locator) throws InterruptedException {
459            waitAndClick(locator, "");
460        }
461    
462        protected void waitForPageToLoad() {
463            // noop webdriver doesn't it need it, except when it does...
464        }
465    
466        protected void waitFor(By by) throws InterruptedException {
467            waitFor(by, "");
468        }
469    
470        protected void waitFor(By by, String message) throws InterruptedException {
471    //        for (int second = 0;; second++) {
472                Thread.sleep(1000);
473            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
474    
475    //            if (second >= DEFAULT_WAIT_SEC) fail(by.toString() + " " + message + " " + DEFAULT_WAIT_SEC + " sec timeout.");
476                try { driver.findElement(by);
477                    //break;
478                } catch (Exception e) {}
479            driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
480    //        }
481        }
482    
483        protected void waitAndClick(By by) throws InterruptedException {
484            waitAndClick(by, "");
485        }
486    
487        protected void waitAndClick(By by, String message) throws InterruptedException {
488            waitFor(by, message);
489            try {
490                (driver.findElement(by)).click();
491            } catch (Exception e) {
492                fail(e.getMessage() + " " + by.toString() + " " + message + " " + driver.getCurrentUrl() );
493                e.printStackTrace();
494            }
495        }
496    
497        protected void waitAndClick(String locator, String message) throws InterruptedException {
498            waitAndClick(By.cssSelector(locator), message);
499        }
500    
501        protected void waitAndClickByLinkText(String text) throws InterruptedException {
502            waitAndClick(By.linkText(text),"");
503        }
504    
505        protected void waitAndClickByLinkText(String text, String message) throws InterruptedException {
506            waitAndClick(By.linkText(text), message);
507        }
508    
509        protected void waitAndClickByName(String name) throws InterruptedException {
510            waitAndClick(By.name(name), "");
511        }
512    
513        protected void waitAndClickByXpath(String xpath) throws InterruptedException {
514            waitAndClick(By.xpath(xpath));
515        }    
516    
517        protected void waitAndClickByName(String name, String message) throws InterruptedException {
518            waitAndClick(By.name(name), message);
519        }
520        
521        protected void waitAndClickByXpath(String xpath, String message) throws InterruptedException {
522            waitAndClick(By.xpath(xpath), message);
523        }
524    
525        protected void waitAndType(By by, String text) throws InterruptedException {
526            waitFor(by, "");
527            try {
528                (driver.findElement(by)).sendKeys(text);
529            } catch (Exception e) {
530                fail(e.getMessage() + " " + by.toString() + " unable to type text '" + text + "' current url " + driver.getCurrentUrl()
531                        + "\n" + ITUtil.deLinespace(driver.getPageSource())
532                );
533                e.printStackTrace();
534            }
535        }
536        
537        protected void waitAndType(By by, String text, String message) throws InterruptedException {
538            waitFor(by, "");
539            try {
540                (driver.findElement(by)).sendKeys(text);
541            } catch (Exception e) {
542                fail(e.getMessage() + " " + by.toString() + "  unable to type text '" + text + "'  " + message + " current url " + driver.getCurrentUrl()
543                        + "\n" + ITUtil.deLinespace(driver.getPageSource())
544                );
545                e.printStackTrace();
546            }
547        }
548        
549        protected void waitAndType(String selector, String text) throws InterruptedException {
550            waitAndType(By.cssSelector(selector), text);
551        }
552        
553        protected void waitAndTypeByXpath(String locator, String text) throws InterruptedException {
554            waitAndType(By.xpath(locator), text);
555        }
556        
557        protected void waitAndTypeByXpath(String locator, String text, String message) throws InterruptedException {
558            waitAndType(By.xpath(locator), text, message);
559        }
560        
561        protected void waitAndTypeByName(String name, String text) throws InterruptedException {
562            waitAndType(By.name(name), text);
563        }
564        
565        protected void selectByXpath(String locator, String selectText) throws InterruptedException {
566            select(By.xpath(locator), selectText);
567        }
568        
569        protected void selectByName(String name, String selectText) throws InterruptedException {
570            select(By.name(name), selectText);
571        }
572        
573        protected void select(By by, String selectText)  throws InterruptedException {
574            WebElement select1 = driver.findElement(by);
575            List<WebElement> options = select1.findElements(By.tagName("option"));
576            for(WebElement option : options){
577                if(option.getText().equals(selectText)){
578                    option.click();
579                    break;
580                }
581            }
582        }
583    
584        protected void selectOptionByName(String name, String optionValue) throws InterruptedException {
585            selectOption(By.name(name), optionValue);
586        }
587    
588        protected void selectOptionByXpath(String locator, String optionValue) throws InterruptedException {
589            selectOption(By.name(locator), optionValue);
590        }
591    
592        protected void selectOption(By by, String optionValue)  throws InterruptedException {
593            WebElement select1 = driver.findElement(by);
594            List<WebElement> options = select1.findElements(By.tagName("option"));
595            for(WebElement option : options){
596                if(option.getAttribute("value").equals(optionValue)){
597                    option.click();
598                    break;
599                }
600            }
601        }
602    
603        protected String[] getSelectOptions(By by) throws InterruptedException {
604            WebElement select1 = driver.findElement(by);
605            List<WebElement> options = select1.findElements(By.tagName("option"));
606            String[] optionValues = new String[options.size()];
607            int counter=0;
608            for(WebElement option : options){
609                optionValues[counter] = option.getAttribute("value");
610                counter++;
611            }
612            return optionValues;
613        }
614        
615        protected String[] getSelectOptionsByName(String name) throws InterruptedException {
616            return getSelectOptions(By.name(name));
617        }
618        
619        protected String[] getSelectOptionsByXpath(String locator) throws InterruptedException {
620            return getSelectOptions(By.xpath(locator));
621        }
622        
623        protected int getCssCount(String selector) {
624            return getCssCount(By.cssSelector(selector));
625        }
626        
627        protected int getCssCount(By by) {
628            return (driver.findElements(by)).size();
629        }
630        
631        protected void checkErrorMessageItem(String message)
632        {
633            final String error_locator = "//li[@class='uif-errorMessageItem']";
634            assertElementPresentByXpath(error_locator);
635            String errorText=null;
636            try {
637                errorText = getTextByXpath(error_locator);
638            } catch (InterruptedException e) {
639                e.printStackTrace();
640            }
641            if (errorText != null && errorText.contains("errors")) {
642                Assert.fail(errorText + message);
643            }
644                   
645        }
646    
647        protected boolean isVisible(String locator) {
648            return driver.findElement(By.cssSelector(locator)).isDisplayed();
649        }
650    
651        protected boolean isVisible(By by) {
652            return driver.findElement(by).isDisplayed();
653        }
654    
655        protected boolean isVisibleByXpath(String locator) {
656            return isVisible(By.xpath(locator));
657        }
658        
659        protected void waitNotVisible(By by) throws InterruptedException {
660            for (int second = 0;; second++) {
661                if (second >= 60) {
662                    Assert.fail("timeout");
663                }
664    
665                if (!isVisible(by)) {
666                    break;
667                }
668    
669                Thread.sleep(1000);
670            }
671        }   
672    
673        protected void waitNotVisibleByXpath(String locator) throws InterruptedException {
674            waitNotVisible(By.xpath(locator));
675        }   
676        
677        protected void waitIsVisible(By by) throws InterruptedException {
678            for (int second = 0;; second++) {
679                if (second >= 60) {
680                    Assert.fail("timeout");
681                }
682                if (isVisible(by)) {
683                    break;
684                }
685                Thread.sleep(1000);
686            }
687        }
688        
689        protected void waitForElementVisible(String elementLocator, String message) throws InterruptedException {
690            boolean failed = false;
691            for (int second = 0;; second++) {
692                if (second >= 60) failed = true;                 
693                try { if (failed || (driver.findElements(By.cssSelector(elementLocator))).size()>0) break; } catch (Exception e) {}
694                Thread.sleep(1000);
695            }
696            checkForIncidentReport(elementLocator); // after timeout to be sure page is loaded
697            if (failed) fail("timeout of 60 seconds waiting for " + elementLocator + " " + message + " " + driver.getCurrentUrl());
698        }
699        
700        protected void waitIsVisible(String locator) throws InterruptedException {
701           waitIsVisible(By.cssSelector(locator));
702        }
703        
704        protected void waitIsVisibleByXpath(String locator) throws InterruptedException {
705            waitIsVisible(By.xpath(locator));
706        }
707        
708        protected void colapseExpandByXpath(String clickLocator, String visibleLocator) throws InterruptedException {
709            waitAndClickByXpath(clickLocator);
710            waitNotVisibleByXpath(visibleLocator);
711    
712            waitAndClickByXpath(clickLocator);
713            waitIsVisibleByXpath(visibleLocator);
714        }
715    
716        protected void expandColapseByXpath(String clickLocator, String visibleLocator) throws InterruptedException {
717            waitAndClickByXpath(clickLocator);
718            waitIsVisibleByXpath(visibleLocator);
719    
720            waitAndClickByXpath(clickLocator);
721            waitNotVisibleByXpath(visibleLocator);
722        }
723        
724        public void switchToWindow(String title) {
725            Set<String> windows = driver.getWindowHandles();
726    
727            for (String window : windows) {
728                driver.switchTo().window(window);
729                if (driver.getTitle().contains(title)) {
730                    return;
731                }
732            }
733        }
734        
735        public String [] getAllWindowTitles() {        
736            return (String[]) driver.getWindowHandles().toArray();
737        }
738        
739        
740        protected void check(By by)  throws InterruptedException {
741            WebElement element =driver.findElement(by);
742            if(!element.isSelected()){
743                element.click();
744            }
745        }
746        
747        protected void checkByName(String name) throws InterruptedException {
748            check(By.name(name));
749        }
750        
751        protected void checkByXpath(String locator) throws InterruptedException {
752            check(By.xpath(locator));
753        }
754    
755        protected void uncheck(By by)  throws InterruptedException {
756            WebElement element =driver.findElement(by);
757            if(element.isSelected()){
758                element.click();
759            }
760        }
761        
762        protected void uncheckByName(String name) throws InterruptedException {
763            uncheck(By.name(name));
764        }
765        
766        protected void uncheckByXpath(String locator) throws InterruptedException {
767            uncheck(By.xpath(locator));
768        }
769        
770        protected void fireEvent(String name, String event) {
771            ((JavascriptExecutor)driver).executeScript(
772                    "var elements=document.getElementsByName(\""+name+"\");"+
773                    "for (var i = 0; i < elements.length; i++){"+
774                            "elements[i]."+event+"();}"
775                            );
776        }
777        
778        protected void fireEvent(String name, String value, String event) {
779            ((JavascriptExecutor)driver).executeScript(
780                    "var elements=document.getElementsByName(\""+name+"\");"+
781                    "for (var i = 0; i < elements.length; i++){"+
782                            "if(elements[i].value=='"+value+"')"+
783                            "elements[i]."+event+"();}"
784                            );
785        }
786        
787        public void fireMouseOverEventByName(String name) {    
788            this.fireMouseOverEvent(By.name(name));
789        }
790        
791        public void fireMouseOverEventByXpath(String locator) {    
792            this.fireMouseOverEvent(By.xpath(locator));
793        }
794        
795        public void fireMouseOverEvent(By by) {    
796            Actions builder = new Actions(driver);
797            Actions hover = builder.moveToElement(driver.findElement(by));
798            hover.perform();
799       
800        }
801    }