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      public static final String REMOTE_DRIVER_SAUCELABS_PROPERTY = "remote.driver.saucelabs";
35  
36      /**
37       *
38       * @param username
39       * @param url
40       * @return
41       * @throws Exception
42       */
43      public static WebDriver setUp(String username, String url) throws Exception {
44          return setUp(username, url, null, null);
45      }
46  
47      /**
48       * Setup the WebDriver test, login and load the tested web page
49       *
50       * @throws Exception
51       */
52      public static WebDriver setUp(String username, String url, String className, TestName testName) throws Exception {
53          WebDriver driver = null;
54          if (System.getProperty(REMOTE_DRIVER_SAUCELABS_PROPERTY) == null) {
55              driver = ITUtil.getWebDriver();
56  //        } else {
57  //            SauceLabsWebDriverHelper saucelabs = new SauceLabsWebDriverHelper();
58  //            saucelabs.setUp(className, testName);
59  //            driver = saucelabs.getDriver();
60          }
61          driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
62          driver.get(url);
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(ITUtil.HUB_DRIVER_PROPERTY);
77          // TODO can the saucelabs driver stuff be leveraged here?
78          if (driverParam != null && "chrome".equals(driverParam.toLowerCase())) {
79              if (System.getProperty("webdriver.chrome.driver") == null) {
80                  if (System.getProperty("remote.public.chrome") != null) {
81                      System.setProperty("webdriver.chrome.driver", System.getProperty("remote.public.chrome"));
82                  }
83              }
84              ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
85                      .usingChromeDriverExecutable(new File(System.getProperty("remote.public.chrome")))
86                      .usingAnyFreePort()
87                      .build();
88              return chromeDriverService;
89          }
90          return null;
91      }
92  }