View Javadoc

1   package com.atlassian.webdriver;
2   
3   import com.atlassian.pageobjects.browser.Browser;
4   import com.atlassian.webdriver.utils.Check;
5   import com.atlassian.webdriver.utils.element.ElementIsVisible;
6   import com.atlassian.webdriver.utils.element.ElementLocated;
7   import com.atlassian.webdriver.utils.element.ElementNotLocated;
8   import com.atlassian.webdriver.utils.element.ElementNotVisible;
9   import com.google.common.base.Function;
10  import org.apache.commons.io.FileUtils;
11  import org.apache.commons.io.IOUtils;
12  import org.openqa.selenium.By;
13  import org.openqa.selenium.HasCapabilities;
14  import org.openqa.selenium.HasInputDevices;
15  import org.openqa.selenium.JavascriptExecutor;
16  import org.openqa.selenium.Keyboard;
17  import org.openqa.selenium.Mouse;
18  import org.openqa.selenium.OutputType;
19  import org.openqa.selenium.SearchContext;
20  import org.openqa.selenium.TakesScreenshot;
21  import org.openqa.selenium.WebDriver;
22  import org.openqa.selenium.WebDriverException;
23  import org.openqa.selenium.WebElement;
24  import org.openqa.selenium.remote.RemoteWebDriver;
25  import org.openqa.selenium.support.ui.WebDriverWait;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.inject.Inject;
30  import java.io.File;
31  import java.io.FileWriter;
32  import java.io.IOException;
33  import java.util.List;
34  import java.util.Set;
35  
36  import static com.google.common.base.Preconditions.checkNotNull;
37  
38  /**
39   * Exposes a set of common functions to use.
40   */
41  public class DefaultAtlassianWebDriver implements AtlassianWebDriver
42  {
43  
44      private static final Logger log = LoggerFactory.getLogger(DefaultAtlassianWebDriver.class);
45  
46      private static final int WAIT_TIME = 60;
47  
48      private final WebDriver driver;
49      private final Browser browser;
50  
51      @Inject
52      public DefaultAtlassianWebDriver(WebDriver driver, Browser browser)
53      {
54          this.driver = checkNotNull(driver, "driver");
55          this.browser = checkNotNull(browser, "browser");
56      }
57  
58      @Override
59      public Browser getBrowser()
60      {
61          return browser;
62      }
63  
64      @Override
65      public WebDriver getWrappedDriver()
66      {
67          return driver;
68      }
69  
70      @Override
71      public WebDriver getDriver()
72      {
73          return driver;
74      }
75  
76      @Override
77      public void quit()
78      {
79          log.debug("Quitting {}", this);
80          if(driver instanceof RemoteWebDriver) {
81              RemoteWebDriver remoteDriver = (RemoteWebDriver) driver;
82              if(remoteDriver.getSessionId() == null) {
83                  // already quit - avoid exceptions of trying to do it twice.
84                  // It's important for quit IE before getting to the shutdown hook, as IE crashes the
85                  // JVM (in IEDriver.dll) if quit is left until the shutdown hook.
86                  log.debug("No remote session {}", this);
87                  return;
88              }
89          }
90          driver.quit();
91          log.debug("Quit complete {}", this);
92      }
93  
94      public void waitUntil(final Function<WebDriver, Boolean> isTrue)
95      {
96          new WebDriverWait(getDriver(), WAIT_TIME).until(isTrue);
97      }
98  
99      public void waitUntil(final Function<WebDriver, Boolean> isTrue, int timeoutInSeconds)
100     {
101         new WebDriverWait(getDriver(), timeoutInSeconds).until(isTrue);
102     }
103 
104     public void dumpSourceTo(File dumpFile) {
105         FileWriter fileWriter = null;
106         try
107         {
108             fileWriter = new FileWriter(dumpFile);
109             IOUtils.write(getDriver().getPageSource(), fileWriter);
110         }
111         catch (IOException e1)
112         {
113             log.info("Error dumping page source to file: " + dumpFile + " : " + e1.getMessage());
114         }
115         finally
116         {
117             IOUtils.closeQuietly(fileWriter);
118         }
119     }
120 
121     public void takeScreenshotTo(File destFile) {
122         if (getDriver() instanceof TakesScreenshot) {
123             TakesScreenshot shotter = (TakesScreenshot) getDriver();
124             log.info("Saving screenshot to: " + destFile.getAbsolutePath());
125             try
126             {
127                 File screenshot = shotter.getScreenshotAs(OutputType.FILE);
128                 FileUtils.copyFile(screenshot, destFile);
129             }
130             catch (IOException e)
131             {
132                 log.warn("Could not capture screenshot to: " + destFile, e);
133             }
134             catch (WebDriverException e)
135             {
136                 // This will occur in Chrome for platforms (e.g. linux) where screenshots aren't supported.
137                 // Webdriver already spams the logs, so only debug log it.
138                 log.debug("Details: ", e);
139             }
140         }
141         else
142         {
143             log.info("Driver is not capable of taking screenshots.");
144         }
145     }
146 
147     public void waitUntilElementIsVisibleAt(By elementLocator, SearchContext at)
148     {
149         waitUntil(new ElementIsVisible(elementLocator, at));
150     }
151 
152     public void waitUntilElementIsVisible(By elementLocator)
153     {
154         waitUntilElementIsVisibleAt(elementLocator, null);
155     }
156 
157     public void waitUntilElementIsNotVisibleAt(By elementLocator, SearchContext at)
158     {
159         waitUntil(new ElementNotVisible(elementLocator, at));
160     }
161 
162     public void waitUntilElementIsNotVisible(By elementLocator)
163     {
164         waitUntilElementIsNotVisibleAt(elementLocator, null);
165     }
166 
167     public void waitUntilElementIsLocatedAt(By elementLocator, SearchContext at)
168     {
169         waitUntil(new ElementLocated(elementLocator, at));
170     }
171 
172     public void waitUntilElementIsLocated(By elementLocator)
173     {
174         waitUntilElementIsLocatedAt(elementLocator, null);
175     }
176 
177     public void waitUntilElementIsNotLocatedAt(By elementLocator, SearchContext at)
178     {
179         waitUntil(new ElementNotLocated(elementLocator, at));
180     }
181 
182     public void waitUntilElementIsNotLocated(By elementLocator)
183     {
184         waitUntilElementIsNotLocatedAt(elementLocator, null);
185     }
186 
187     public boolean elementExists(By locator)
188     {
189         return Check.elementExists(locator, driver);
190     }
191 
192     public boolean elementExistsAt(By locator, SearchContext context)
193     {
194         return Check.elementExists(locator, context);
195     }
196 
197     public boolean elementIsVisible(By locator)
198     {
199         return Check.elementIsVisible(locator, driver);
200     }
201 
202     public boolean elementIsVisibleAt(By locator, SearchContext context)
203     {
204         return Check.elementIsVisible(locator, context);
205     }
206 
207     /**
208      * WebDriver implementation below
209      */
210     public void get(final String url)
211     {
212         driver.get(url);
213     }
214 
215     public String getCurrentUrl()
216     {
217         return driver.getCurrentUrl();
218     }
219 
220     public String getTitle()
221     {
222         return driver.getTitle();
223     }
224 
225     public List<WebElement> findElements(final By by)
226     {
227         return driver.findElements(by);
228     }
229 
230     public WebElement findElement(final By by)
231     {
232         return driver.findElement(by);
233     }
234 
235     public String getPageSource()
236     {
237         return driver.getPageSource();
238     }
239 
240     public void close()
241     {
242         driver.close();
243     }
244 
245     public Set<String> getWindowHandles()
246     {
247         return driver.getWindowHandles();
248     }
249 
250     public String getWindowHandle()
251     {
252         return driver.getWindowHandle();
253     }
254 
255     public TargetLocator switchTo()
256     {
257         return driver.switchTo();
258     }
259 
260     public Navigation navigate()
261     {
262         return driver.navigate();
263     }
264 
265     public Options manage()
266     {
267         return driver.manage();
268     }
269 
270     public Object executeScript(final String script)
271     {
272         return ((JavascriptExecutor)driver).executeScript(script);
273     }
274 
275     public Object executeScript(final String script, final Object... args)
276     {
277         return ((JavascriptExecutor)driver).executeScript(script, args);
278     }
279 
280     public Object executeAsyncScript(String s, Object... objects)
281     {
282         return ((JavascriptExecutor) driver).executeAsyncScript(s, objects);
283     }
284 
285     public boolean isJavascriptEnabled()
286     {
287         return ((HasCapabilities)driver).getCapabilities().isJavascriptEnabled();
288     }
289 
290     public Keyboard getKeyboard()
291     {
292         return ((HasInputDevices)driver).getKeyboard();
293     }
294 
295     public Mouse getMouse()
296     {
297         return ((HasInputDevices)driver).getMouse();
298     }
299 }