1 package com.atlassian.pageobjects.elements.query.webdriver;
2
3 import com.atlassian.pageobjects.elements.WebDriverLocatable;
4 import com.atlassian.webdriver.AtlassianWebDriver;
5 import com.google.common.base.Function;
6 import com.google.common.base.Supplier;
7 import org.openqa.selenium.NoSuchElementException;
8 import org.openqa.selenium.StaleElementReferenceException;
9 import org.openqa.selenium.WebElement;
10
11 import static com.atlassian.pageobjects.elements.util.StringConcat.asString;
12 import static com.google.common.base.Preconditions.checkNotNull;
13
14
15
16
17
18
19
20
21
22
23
24 public class WebDriverLocatableBasedTimedQuery<T> extends GenericWebDriverTimedQuery<T>
25 {
26 private final WebDriverLocatable locatable;
27
28 public WebDriverLocatableBasedTimedQuery(WebDriverLocatable locatable, AtlassianWebDriver driver,
29 Function<WebElement, T> valueProvider, long timeout)
30 {
31 super(new LocatableBasedSupplier<T>(driver, locatable, valueProvider), timeout);
32 this.locatable = locatable;
33 }
34
35 public WebDriverLocatableBasedTimedQuery(WebDriverLocatable locatable, AtlassianWebDriver driver,
36 Function<WebElement, T> valueProvider, long timeout, long interval)
37 {
38 super(new LocatableBasedSupplier<T>(driver, locatable, valueProvider), timeout, interval);
39 this.locatable = locatable;
40 }
41
42 public WebDriverLocatableBasedTimedQuery(WebDriverLocatable locatable, AtlassianWebDriver driver,
43 Function<WebElement, T> valueProvider, long timeout, long interval,
44 T invalidValue)
45 {
46 super(new LocatableBasedSupplier<T>(driver, locatable, valueProvider, invalidValue), timeout, interval);
47 this.locatable = locatable;
48 }
49
50 public WebDriverLocatableBasedTimedQuery(WebDriverLocatableBasedTimedQuery<T> origin, long timeout)
51 {
52 super(origin.webElementSupplier(), timeout, origin.interval);
53 this.locatable = origin.locatable;
54 }
55
56 LocatableBasedSupplier<T> webElementSupplier()
57 {
58 return (LocatableBasedSupplier<T>) valueSupplier;
59 }
60
61 Function<WebElement,T> valueProvider()
62 {
63 return webElementSupplier().valueProvider;
64 }
65
66 @Override
67 public String toString()
68 {
69 return asString(super.toString(), "[locatable=", locatable, ",valueProvider=", valueProvider(), "]");
70 }
71
72 private static class LocatableBasedSupplier<S> implements Supplier<S>
73 {
74 private final AtlassianWebDriver webDriver;
75 private final WebDriverLocatable locatable;
76 private final Function<WebElement, S> valueProvider;
77 private final S invalidValue;
78
79 public LocatableBasedSupplier(AtlassianWebDriver webDriver ,WebDriverLocatable locatable,
80 Function<WebElement, S> valueProvider, S invalid)
81 {
82 this.valueProvider = valueProvider;
83 this.webDriver = webDriver;
84 this.locatable = checkNotNull(locatable);
85 this.invalidValue = invalid;
86 }
87
88 public LocatableBasedSupplier(AtlassianWebDriver webDriver, WebDriverLocatable locatable, Function<WebElement, S> valueProvider)
89 {
90 this(webDriver, locatable, valueProvider, null);
91 }
92
93 public S get()
94 {
95 try
96 {
97 return valueProvider.apply((WebElement) locatable.waitUntilLocated(webDriver, 0));
98 }
99 catch(StaleElementReferenceException e)
100 {
101
102
103 throw new InvalidValue(invalidValue);
104 }
105 catch (NoSuchElementException e1)
106 {
107 throw new InvalidValue(invalidValue);
108 }
109 }
110 }
111 }