1
1
package info .novatec .testit .webtester .mouse ;
2
2
3
- import static info .novatec .testit .webtester .conditions .Conditions .visible ;
4
-
5
- import java .util .Arrays ;
6
3
import java .util .Collection ;
7
- import java .util .function .BiFunction ;
4
+ import java .util .function .Supplier ;
8
5
9
6
import org .openqa .selenium .TimeoutException ;
10
7
import org .openqa .selenium .WebDriver ;
11
8
import org .openqa .selenium .WebElement ;
12
9
import org .openqa .selenium .interactions .Actions ;
13
10
14
- import com . google . common . annotations . Beta ;
11
+ import lombok . Setter ;
15
12
16
13
import info .novatec .testit .webtester .events .pagefragments .ClickedEvent ;
17
14
import info .novatec .testit .webtester .events .pagefragments .ContextClickedEvent ;
18
15
import info .novatec .testit .webtester .events .pagefragments .DoubleClickedEvent ;
19
- import info .novatec .testit .webtester .events .pagefragments .DraggedAndDroppedEvent ;
20
- import info .novatec .testit .webtester .internal .ActionTemplate ;
21
16
import info .novatec .testit .webtester .pagefragments .PageFragment ;
22
- import info .novatec .testit .webtester .waiting .Wait ;
23
17
24
18
25
19
/**
26
20
* This class is used to perform a variety of mouse related actions.
27
21
*
28
- * @see MouseOnAction
29
- * @see MouseDragAction
30
- * @see MouseActionSequence
22
+ * @see OnPageFragment
23
+ * @see Sequence
31
24
* @since 2.0
32
25
*/
33
26
public final class Mouse {
34
27
35
- /* click with mouse */
28
+ /** The default {@link MouseDriver} supplier. Generates a new {@link DefaultMouseDriver} for each call. */
29
+ public static final Supplier <MouseDriver > DEFAULT_MOUSE_DRIVER = DefaultMouseDriver ::new ;
30
+
31
+ /**
32
+ * A supplier used to get a {@link MouseDriver} instance to use when executing any operations.
33
+ * The supplier can be changed externally to customize the behavior.
34
+ * Since this is a static field you should keep in mind that this will have an JVM global effect!
35
+ * <p>
36
+ * The default supplier is {@link #DEFAULT_MOUSE_DRIVER}.
37
+ */
38
+ @ Setter
39
+ private static Supplier <MouseDriver > mouseDriver = DEFAULT_MOUSE_DRIVER ;
36
40
37
41
/**
38
42
* Executes a single-click on the given {@link PageFragment page fragment}.
@@ -47,11 +51,7 @@ public final class Mouse {
47
51
* @since 2.0
48
52
*/
49
53
public static void click (PageFragment fragment ) {
50
- ActionTemplate .pageFragment (fragment ).execute (Mouse ::doClick ).fireEvent (ClickedEvent ::new ).markAsUsed ();
51
- }
52
-
53
- private static void doClick (PageFragment fragment ) {
54
- perform (fragment , Actions ::click );
54
+ mouseDriver .get ().click (fragment );
55
55
}
56
56
57
57
/**
@@ -67,11 +67,7 @@ private static void doClick(PageFragment fragment) {
67
67
* @since 2.0
68
68
*/
69
69
public static void doubleClick (PageFragment fragment ) {
70
- ActionTemplate .pageFragment (fragment ).execute (Mouse ::doDoubleClick ).fireEvent (DoubleClickedEvent ::new ).markAsUsed ();
71
- }
72
-
73
- private static void doDoubleClick (PageFragment fragment ) {
74
- perform (fragment , Actions ::doubleClick );
70
+ mouseDriver .get ().doubleClick (fragment );
75
71
}
76
72
77
73
/**
@@ -87,18 +83,9 @@ private static void doDoubleClick(PageFragment fragment) {
87
83
* @since 2.0
88
84
*/
89
85
public static void contextClick (PageFragment fragment ) {
90
- ActionTemplate .pageFragment (fragment )
91
- .execute (Mouse ::doContextClick )
92
- .fireEvent (ContextClickedEvent ::new )
93
- .markAsUsed ();
94
- }
95
-
96
- private static void doContextClick (PageFragment fragment ) {
97
- perform (fragment , Actions ::contextClick );
86
+ mouseDriver .get ().contextClick (fragment );
98
87
}
99
88
100
- /* move mouse */
101
-
102
89
/**
103
90
* Moves the mouse to each of the given {@link PageFragment page fragments} in the order they are given.
104
91
* <p>
@@ -116,8 +103,7 @@ private static void doContextClick(PageFragment fragment) {
116
103
* @since 2.0
117
104
*/
118
105
public static void moveToEach (PageFragment fragment , PageFragment ... fragments ) throws TimeoutException {
119
- moveTo (fragment );
120
- moveToEach (Arrays .asList (fragments ));
106
+ mouseDriver .get ().moveToEach (fragment , fragments );
121
107
}
122
108
123
109
/**
@@ -136,7 +122,7 @@ public static void moveToEach(PageFragment fragment, PageFragment... fragments)
136
122
* @since 2.0
137
123
*/
138
124
public static void moveToEach (Collection <PageFragment > fragments ) throws TimeoutException {
139
- fragments . forEach ( Mouse :: moveTo );
125
+ mouseDriver . get (). moveToEach ( fragments );
140
126
}
141
127
142
128
/**
@@ -156,83 +142,28 @@ public static void moveToEach(Collection<PageFragment> fragments) throws Timeout
156
142
* @since 2.0
157
143
*/
158
144
public static void moveTo (PageFragment fragment ) throws TimeoutException {
159
- ActionTemplate .pageFragment (fragment ).execute (Mouse ::doMoveTo );
160
- }
161
-
162
- private static void doMoveTo (PageFragment fragment ) {
163
- Wait .until (fragment ).is (visible ());
164
- perform (fragment , Actions ::moveToElement );
165
- }
166
-
167
- /* drag and drop */
168
-
169
- /**
170
- * Drags the given source {@link PageFragment page fragment} onto the given target {@link PageFragment page fragment}.
171
- * Fires a {@link DraggedAndDroppedEvent}.
172
- * <p>
173
- * The actual behavior might vary between different {@link WebDriver} implementations. Some implementations might move
174
- * the actual mouse cursor, some might simulate the behavior.
175
- * <p>
176
- * This might not work with all drivers!
177
- *
178
- * @param sourceFragment the fragment being dragged
179
- * @param targetFragment the fragment the source is dragged onto
180
- * @see PageFragment
181
- * @see Actions#dragAndDrop(WebElement, WebElement)
182
- * @since 2.0
183
- */
184
- @ Beta
185
- public static void dragAndDrop (PageFragment sourceFragment , PageFragment targetFragment ) {
186
- ActionTemplate .pageFragments (sourceFragment , targetFragment )
187
- .execute (Mouse ::doDragAndDrop )
188
- .fireEvent (DraggedAndDroppedEvent ::new )
189
- .markAsUsed ();
190
- }
191
-
192
- private static void doDragAndDrop (PageFragment sourceFragment , PageFragment targetFragment ) {
193
- sequenceFor (sourceFragment ).dragAndDrop (sourceFragment .webElement (), targetFragment .webElement ()).perform ();
194
- }
195
-
196
- private static void perform (PageFragment fragment , BiFunction <Actions , WebElement , Actions > biConsumer ) {
197
- biConsumer .apply (sequenceFor (fragment ), fragment .webElement ()).perform ();
198
- }
199
-
200
- private static Actions sequenceFor (PageFragment fragment ) {
201
- return new Actions (fragment .getBrowser ().webDriver ());
202
- }
203
-
204
- /* fluent API factories */
205
-
206
- /**
207
- * Creates a new {@link MouseOnAction} for the given {@link PageFragment}.
208
- *
209
- * @param fragment the page fragment to use
210
- * @return the created action
211
- * @since 2.0
212
- */
213
- public static MouseOnAction on (PageFragment fragment ) {
214
- return new MouseOnAction (fragment );
145
+ mouseDriver .get ().moveTo (fragment );
215
146
}
216
147
217
148
/**
218
- * Creates a new {@link MouseDragAction } for the given {@link PageFragment}.
149
+ * Creates a new {@link OnPageFragment } for the given {@link PageFragment}.
219
150
*
220
151
* @param fragment the page fragment to use
221
- * @return the created action
152
+ * @return the new instance
222
153
* @since 2.0
223
154
*/
224
- public static MouseDragAction drag (PageFragment fragment ) {
225
- return new MouseDragAction ( fragment );
155
+ public static OnPageFragment on (PageFragment fragment ) {
156
+ return new OnPageFragment ( mouseDriver . get (), fragment );
226
157
}
227
158
228
159
/**
229
- * Creates a new {@link MouseActionSequence }.
160
+ * Creates a new {@link Sequence }.
230
161
*
231
- * @return the created action
162
+ * @return the new instance
232
163
* @since 2.0
233
164
*/
234
- public static MouseActionSequence sequence () {
235
- return new MouseActionSequence ( );
165
+ public static Sequence sequence () {
166
+ return new Sequence ( mouseDriver . get () );
236
167
}
237
168
238
169
private Mouse () {
0 commit comments