/
WaitConditionProxy.java
112 lines (104 loc) · 4.66 KB
/
WaitConditionProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package org.fluentlenium.core.conditions.wait;
import com.google.common.base.Supplier;
import org.fluentlenium.core.conditions.AtLeastOneElementConditions;
import org.fluentlenium.core.conditions.Conditions;
import org.fluentlenium.core.conditions.EachElementConditions;
import org.fluentlenium.core.conditions.FluentConditions;
import org.fluentlenium.core.conditions.FluentListConditions;
import org.fluentlenium.core.conditions.WebElementConditions;
import org.fluentlenium.core.conditions.message.MessageProxy;
import org.fluentlenium.core.domain.FluentWebElement;
import org.fluentlenium.core.wait.FluentWait;
import java.lang.reflect.Proxy;
import java.util.List;
/**
* Provides proxy implementations of conditions that performs wait from those conditions.
*/
public final class WaitConditionProxy {
private WaitConditionProxy() {
//Utility class
}
/**
* Build a wait proxy.
*
* @param wait Fluent wait
* @param context Message context
* @param elementsSupplier Supplier for elements to wait.
* @return a proxy generating message from annotations.
*/
public static FluentListConditions each(final FluentWait wait, final String context,
final Supplier<? extends List<? extends FluentWebElement>> elementsSupplier) {
return list(wait, context, new Supplier<FluentListConditions>() {
@Override
public FluentListConditions get() {
return new EachElementConditions(elementsSupplier.get());
}
});
}
/**
* Build a wait proxy.
*
* @param wait Fluent wait
* @param context Message context
* @param elementsSupplier Supplier for elements to wait.
* @return a proxy generating message from annotations.
*/
public static FluentListConditions one(final FluentWait wait, final String context,
final Supplier<? extends List<? extends FluentWebElement>> elementsSupplier) {
return list(wait, context, new Supplier<FluentListConditions>() {
@Override
public FluentListConditions get() {
return new AtLeastOneElementConditions(elementsSupplier.get());
}
});
}
/**
* Build a wait proxy.
*
* @param wait Fluent wait
* @param context Message context
* @param conditionsSupplier Supplier for elements to wait.
* @return a proxy generating message from annotations.
*/
public static FluentListConditions list(final FluentWait wait, final String context,
final Supplier<? extends FluentListConditions> conditionsSupplier) {
return (FluentListConditions) Proxy
.newProxyInstance(MessageProxy.class.getClassLoader(), new Class<?>[] {FluentListConditions.class},
new WaitConditionInvocationHandler(FluentListConditions.class, wait, context, conditionsSupplier));
}
/**
* Build a wait proxy.
*
* @param wait Fluent wait
* @param context Message context
* @param elementSupplier Supplier for element to wait.
* @return a proxy generating message from annotations.
*/
public static FluentConditions element(final FluentWait wait, final String context,
final Supplier<? extends FluentWebElement> elementSupplier) {
return (FluentConditions) Proxy
.newProxyInstance(MessageProxy.class.getClassLoader(), new Class<?>[] {FluentConditions.class},
new WaitConditionInvocationHandler(FluentConditions.class, wait, context,
new Supplier<FluentConditions>() {
@Override
public FluentConditions get() {
return new WebElementConditions(elementSupplier.get());
}
}));
}
/**
* Build a wait proxy.
*
* @param conditionClass condition class
* @param wait Fluent wait
* @param context Message context
* @param conditionsSupplier Supplier for elements to wait.
* @param <C> condition type
* @return a proxy generating message from annotations.
*/
public static <C extends Conditions<?>> C custom(final Class<C> conditionClass, final FluentWait wait, final String context,
final Supplier<C> conditionsSupplier) {
return (C) Proxy.newProxyInstance(MessageProxy.class.getClassLoader(), new Class<?>[] {conditionClass},
new WaitConditionInvocationHandler(conditionClass, wait, context, conditionsSupplier));
}
}