View Javadoc

1   /*
2    * Copyright 2006-2012 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  
17  package edu.samplu.common;
18  
19  import org.junit.rules.TestName;
20  import org.openqa.selenium.WebDriver;
21  import org.openqa.selenium.chrome.ChromeDriverService;
22  
23  import java.io.File;
24  import java.util.concurrent.TimeUnit;
25  
26  /**
27   * @author Kuali Rice Team (rice.collab@kuali.org)
28   */
29  
30  public class WebDriverUtil {
31  
32      public static int DEFAULT_IMPLICIT_WAIT_TIME = 30;
33      public static int SHORT_IMPLICIT_WAIT_TIME = 1;
34  
35      /**
36       *
37       * @param username
38       * @param url
39       * @return
40       * @throws Exception
41       */
42      public static WebDriver setUp(String username, String url) throws Exception {
43          return setUp(username, url, null, null);
44      }
45  
46      /**
47       * Setup the WebDriver test, login and load the tested web page
48       *
49       * @throws Exception
50       */
51      public static WebDriver setUp(String username, String url, String className, TestName testName) throws Exception {
52          WebDriver driver = null;
53          if (System.getProperty("remote.driver.saucelabs") == null) {
54              driver = ITUtil.getWebDriver();
55  //        } else {
56  //            SauceLabsWebDriverHelper saucelabs = new SauceLabsWebDriverHelper();
57  //            saucelabs.setUp(className, testName);
58  //            driver = saucelabs.getDriver();
59          }
60          driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
61          driver.get(url);
62          ITUtil.login(driver, username);
63          driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICIT_WAIT_TIME, TimeUnit.SECONDS);
64          return driver;
65      }
66  
67      public static void checkForIncidentReport(WebDriver driver, String locator) {
68          checkForIncidentReport(driver, locator, "");
69      }
70  
71      public static void checkForIncidentReport(WebDriver driver, String locator, String message) {
72          ITUtil.checkForIncidentReport(driver.getPageSource(), locator, message);
73      }
74  
75      public static ChromeDriverService createAndStartService() {
76          String driverParam = System.getProperty("remote.public.driver");
77          if (driverParam != null && "chrome".equals(driverParam.toLowerCase())) {
78              if (System.getProperty("webdriver.chrome.driver") == null) {
79                  if (System.getProperty("remote.public.chrome") != null) {
80                      System.setProperty("webdriver.chrome.driver", System.getProperty("remote.public.chrome"));
81                  }
82              }
83              ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
84                      .usingChromeDriverExecutable(new File(System.getProperty("remote.public.chrome")))
85                      .usingAnyFreePort()
86                      .build();
87              return chromeDriverService;
88          }
89          return null;
90      }
91  }