1717
1818package org .openqa .selenium .interactions ;
1919
20- import static org .assertj .core .api .Assertions .assertThat ;
21- import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
22- import static org .openqa .selenium .WaitingConditions .elementTextToEqual ;
23- import static org .openqa .selenium .WaitingConditions .elementValueToEqual ;
24- import static org .openqa .selenium .support .Colors .GREEN ;
25- import static org .openqa .selenium .support .Colors .RED ;
26- import static org .openqa .selenium .support .ui .ExpectedConditions .attributeToBe ;
27- import static org .openqa .selenium .support .ui .ExpectedConditions .not ;
28- import static org .openqa .selenium .support .ui .ExpectedConditions .titleIs ;
29- import static org .openqa .selenium .testing .drivers .Browser .ALL ;
30- import static org .openqa .selenium .testing .drivers .Browser .CHROME ;
31- import static org .openqa .selenium .testing .drivers .Browser .EDGE ;
32- import static org .openqa .selenium .testing .drivers .Browser .LEGACY_FIREFOX_XPI ;
33- import static org .openqa .selenium .testing .drivers .Browser .HTMLUNIT ;
34- import static org .openqa .selenium .testing .drivers .Browser .IE ;
35- import static org .openqa .selenium .testing .drivers .Browser .FIREFOX ;
36- import static org .openqa .selenium .testing .drivers .Browser .SAFARI ;
37-
3820import org .junit .Test ;
3921import org .openqa .selenium .By ;
4022import org .openqa .selenium .Dimension ;
5234import org .openqa .selenium .testing .NotYetImplemented ;
5335import org .openqa .selenium .testing .SwitchToTopAfterTest ;
5436
37+ import static org .assertj .core .api .Assertions .assertThat ;
38+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
39+ import static org .openqa .selenium .WaitingConditions .elementTextToEqual ;
40+ import static org .openqa .selenium .WaitingConditions .elementValueToEqual ;
41+ import static org .openqa .selenium .support .Colors .GREEN ;
42+ import static org .openqa .selenium .support .Colors .RED ;
43+ import static org .openqa .selenium .support .ui .ExpectedConditions .attributeToBe ;
44+ import static org .openqa .selenium .support .ui .ExpectedConditions .not ;
45+ import static org .openqa .selenium .testing .drivers .Browser .CHROME ;
46+ import static org .openqa .selenium .testing .drivers .Browser .EDGE ;
47+ import static org .openqa .selenium .testing .drivers .Browser .FIREFOX ;
48+ import static org .openqa .selenium .testing .drivers .Browser .HTMLUNIT ;
49+ import static org .openqa .selenium .testing .drivers .Browser .IE ;
50+ import static org .openqa .selenium .testing .drivers .Browser .SAFARI ;
51+
5552/**
5653 * Tests operations that involve mouse and keyboard.
5754 */
58- public class BasicMouseInterfaceTest extends JUnit4TestBase {
55+ public class DefaultMouseTest extends JUnit4TestBase {
5956
6057 private Actions getBuilder (WebDriver driver ) {
6158 return new Actions (driver );
@@ -220,77 +217,11 @@ public void testCannotMoveToANullLocator() {
220217 }
221218
222219 @ Test
223- @ Ignore (CHROME )
224- @ Ignore (EDGE )
225- @ Ignore (IE )
226- @ Ignore (LEGACY_FIREFOX_XPI )
227- @ Ignore (FIREFOX )
228- @ NotYetImplemented (HTMLUNIT )
229- @ NotYetImplemented (SAFARI )
230- public void testMousePositionIsNotPreservedInActionsChain () {
231- driver .get (pages .javascriptPage );
232- WebElement toMoveTo = driver .findElement (By .id ("clickField" ));
233-
234- getBuilder (driver ).moveToElement (toMoveTo ).build ().perform ();
235-
236- // TODO(andreastt): Is this correct behaviour? Should the last known mouse position be
237- // disregarded if calling click() from a an Actions chain?
238- assertThatExceptionOfType (InvalidCoordinatesException .class )
239- .isThrownBy (() -> getBuilder (driver ).click ().build ().perform ());
240- }
241-
242- @ Test
243- @ Ignore (value = ALL , reason = "Behaviour not finalized yet regarding linked images." )
244- public void testMovingIntoAnImageEnclosedInALink () {
245- driver .get (pages .linkedImage );
246-
247- // Note: For some reason, the Accessibility API in Firefox will not be available before we
248- // click on something. As a work-around, click on a different element just to get going.
249- driver .findElement (By .id ("linkToAnchorOnThisPage" )).click ();
250-
251- WebElement linkElement = driver .findElement (By .id ("linkWithEnclosedImage" ));
252-
253- // Image is 644 x 41 - move towards the end.
254- // Note: The width of the link element itself is correct - 644 pixels. However,
255- // the height is 17 pixels and the rectangle containing it is *below* the image.
256- // For this reason, this action will fail.
257- new Actions (driver ).moveToElement (linkElement , 500 , 30 ).click ().perform ();
258-
259- wait .until (titleIs ("We Arrive Here" ));
260- }
261-
262- @ Test
263- @ Ignore (CHROME )
264- @ Ignore (EDGE )
265- @ Ignore (IE )
266- @ Ignore (LEGACY_FIREFOX_XPI )
267- @ Ignore (FIREFOX )
268220 @ Ignore (value = HTMLUNIT , reason ="test should enable JavaScript" )
269221 @ NotYetImplemented (SAFARI )
270- public void testMovingMouseBackAndForthPastViewPort () {
271- driver .get (pages .veryLargeCanvas );
272-
273- WebElement firstTarget = driver .findElement (By .id ("r1" ));
274- new Actions (driver ).moveToElement (firstTarget ).click ().perform ();
275-
276- WebElement resultArea = driver .findElement (By .id ("result" ));
277- String expectedEvents = "First" ;
278- wait .until (elementTextToEqual (resultArea , expectedEvents ));
279-
280- // Move to element with id 'r2', at (2500, 50) to (2580, 100)
281- new Actions (driver ).moveByOffset (2540 - 150 , 75 - 125 ).click ().perform ();
282- expectedEvents += " Second" ;
283- wait .until (elementTextToEqual (resultArea , expectedEvents ));
284-
285- // Move to element with id 'r3' at (60, 1500) to (140, 1550)
286- new Actions (driver ).moveByOffset (100 - 2540 , 1525 - 75 ).click ().perform ();
287- expectedEvents += " Third" ;
288- wait .until (elementTextToEqual (resultArea , expectedEvents ));
289-
290- // Move to element with id 'r4' at (220,180) to (320, 230)
291- new Actions (driver ).moveByOffset (270 - 100 , 205 - 1525 ).click ().perform ();
292- expectedEvents += " Fourth" ;
293- wait .until (elementTextToEqual (resultArea , expectedEvents ));
222+ public void testMovingPastViewPortThrowsException () {
223+ assertThatExceptionOfType (MoveTargetOutOfBoundsException .class )
224+ .isThrownBy (() -> getBuilder (driver ).moveByOffset (-1000 , -1000 ).perform ());
294225 }
295226
296227 @ SwitchToTopAfterTest
@@ -299,7 +230,7 @@ public void testShouldClickElementInIFrame() {
299230 driver .get (pages .clicksPage );
300231 driver .switchTo ().frame ("source" );
301232 WebElement element = driver .findElement (By .id ("otherframe" ));
302- new Actions (driver ).moveToElement (element ).click ().perform ();
233+ getBuilder (driver ).moveToElement (element ).click ().perform ();
303234 driver .switchTo ().defaultContent ()
304235 .switchTo ().frame ("target" );
305236 wait .until (elementTextToEqual (By .id ("span" ), "An inline element" ));
@@ -316,7 +247,7 @@ public void testShouldAllowUsersToHoverOverElements() {
316247 assertThat (item .getText ()).isEqualTo ("" );
317248
318249 ((JavascriptExecutor ) driver ).executeScript ("arguments[0].style.background = 'green'" , element );
319- new Actions (driver ).moveToElement (element ).build ().perform ();
250+ getBuilder (driver ).moveToElement (element ).build ().perform ();
320251
321252 wait .until (not (elementTextToEqual (item , "" )));
322253 assertThat (item .getText ()).isEqualTo ("Item 1" );
@@ -328,15 +259,15 @@ public void testHoverPersists() throws Exception {
328259 driver .get (pages .javascriptPage );
329260 // Move to a different element to make sure the mouse is not over the
330261 // element with id 'item1' (from a previous test).
331- new Actions (driver ).moveToElement (driver .findElement (By .id ("dynamo" ))).build ().perform ();
262+ getBuilder (driver ).moveToElement (driver .findElement (By .id ("dynamo" ))).build ().perform ();
332263
333264 WebElement element = driver .findElement (By .id ("menu1" ));
334265
335266 final WebElement item = driver .findElement (By .id ("item1" ));
336267 assertThat (item .getText ()).isEqualTo ("" );
337268
338269 ((JavascriptExecutor ) driver ).executeScript ("arguments[0].style.background = 'green'" , element );
339- new Actions (driver ).moveToElement (element ).build ().perform ();
270+ getBuilder (driver ).moveToElement (element ).build ().perform ();
340271
341272 // Intentionally wait to make sure hover persists.
342273 Thread .sleep (2000 );
@@ -352,13 +283,13 @@ public void testMovingMouseByRelativeOffset() {
352283 driver .get (pages .mouseTrackerPage );
353284
354285 WebElement trackerDiv = driver .findElement (By .id ("mousetracker" ));
355- new Actions (driver ).moveToElement (trackerDiv ).perform ();
286+ getBuilder (driver ).moveToElement (trackerDiv ).perform ();
356287
357288 WebElement reporter = driver .findElement (By .id ("status" ));
358289
359290 wait .until (fuzzyMatchingOfCoordinates (reporter , 50 , 200 ));
360291
361- new Actions (driver ).moveByOffset (10 , 20 ).build ().perform ();
292+ getBuilder (driver ).moveByOffset (10 , 20 ).build ().perform ();
362293
363294 wait .until (fuzzyMatchingOfCoordinates (reporter , 60 , 220 ));
364295 }
@@ -370,7 +301,7 @@ public void testMovingMouseToRelativeElementOffset() {
370301
371302 WebElement trackerDiv = driver .findElement (By .id ("mousetracker" ));
372303 Dimension size = trackerDiv .getSize ();
373- new Actions (driver ).moveToElement (trackerDiv , 95 - size .getWidth () / 2 , 195 - size .getHeight () / 2 ).perform ();
304+ getBuilder (driver ).moveToElement (trackerDiv , 95 - size .getWidth () / 2 , 195 - size .getHeight () / 2 ).perform ();
374305
375306 WebElement reporter = driver .findElement (By .id ("status" ));
376307
@@ -383,7 +314,7 @@ public void testMovingMouseToRelativeZeroElementOffset() {
383314 driver .get (pages .mouseTrackerPage );
384315
385316 WebElement trackerDiv = driver .findElement (By .id ("mousetracker" ));
386- new Actions (driver ).moveToElement (trackerDiv , 0 , 0 ).perform ();
317+ getBuilder (driver ).moveToElement (trackerDiv , 0 , 0 ).perform ();
387318
388319 WebElement reporter = driver .findElement (By .id ("status" ));
389320
@@ -399,13 +330,13 @@ public void testMoveRelativeToBody() {
399330 try {
400331 driver .get (pages .mouseTrackerPage );
401332
402- new Actions (driver ).moveByOffset (50 , 100 ).perform ();
333+ getBuilder (driver ).moveByOffset (50 , 100 ).perform ();
403334
404335 WebElement reporter = driver .findElement (By .id ("status" ));
405336
406337 wait .until (fuzzyMatchingOfCoordinates (reporter , 40 , 20 ));
407338 } finally {
408- new Actions (driver ).moveByOffset (-50 , -100 ).perform ();
339+ getBuilder (driver ).moveByOffset (-50 , -100 ).perform ();
409340 }
410341 }
411342
@@ -427,15 +358,15 @@ public void testMoveMouseByOffsetOverAndOutOfAnElement() {
427358 int xOffset = 2 - greenBoxSize .getWidth () / 2 ;
428359 int yOffset = 2 - greenBoxSize .getHeight () / 2 ;
429360
430- new Actions (driver ).moveToElement (greenbox , xOffset , yOffset ).perform ();
361+ getBuilder (driver ).moveToElement (greenbox , xOffset , yOffset ).perform ();
431362
432363 shortWait .until (attributeToBe (redbox , "background-color" , Colors .GREEN .getColorValue ().asRgba ()));
433364
434- new Actions (driver ).moveToElement (greenbox , xOffset , yOffset )
365+ getBuilder (driver ).moveToElement (greenbox , xOffset , yOffset )
435366 .moveByOffset (shiftX , shiftY ).perform ();
436367 shortWait .until (attributeToBe (redbox , "background-color" , Colors .RED .getColorValue ().asRgba ()));
437368
438- new Actions (driver ).moveToElement (greenbox , xOffset , yOffset )
369+ getBuilder (driver ).moveToElement (greenbox , xOffset , yOffset )
439370 .moveByOffset (shiftX , shiftY )
440371 .moveByOffset (-shiftX , -shiftY ).perform ();
441372
@@ -454,18 +385,16 @@ public void testCanMoveOverAndOutOfAnElement() {
454385 Dimension greenSize = greenbox .getSize ();
455386 Dimension redSize = redbox .getSize ();
456387
457- new Actions (driver ).moveToElement (greenbox , 1 - greenSize .getWidth () / 2 , 1 - greenSize .getHeight () / 2 ).perform ();
388+ getBuilder (driver ).moveToElement (greenbox , 1 - greenSize .getWidth () / 2 , 1 - greenSize .getHeight () / 2 ).perform ();
458389
459390 assertThat (Color .fromString (redbox .getCssValue ("background-color" )))
460391 .isEqualTo (GREEN .getColorValue ());
461392
462- new Actions (driver ).moveToElement (redbox ).perform ();
393+ getBuilder (driver ).moveToElement (redbox ).perform ();
463394 assertThat (Color .fromString (redbox .getCssValue ("background-color" )))
464395 .isEqualTo (RED .getColorValue ());
465396
466- // IE8 (and *only* IE8) requires a move of 2 pixels. All other browsers
467- // would be happy with 1.
468- new Actions (driver ).moveToElement (redbox , redSize .getWidth () / 2 + 2 , redSize .getHeight () / 2 + 2 )
397+ getBuilder (driver ).moveToElement (redbox , redSize .getWidth () / 1 + 1 , redSize .getHeight () / 1 + 1 )
469398 .perform ();
470399
471400 wait .until (attributeToBe (redbox , "background-color" , Colors .GREEN .getColorValue ().asRgba ()));
0 commit comments