Skip to content

Commit

Permalink
Fix mouse move offsets in Java tests
Browse files Browse the repository at this point in the history
With PR #7473 clarified that Action.moveToElement uses offset relative
to center instead of top-left, updating Java tests to match the change.

Signed-off-by: Alexei Barantsev <barancev@gmail.com>
  • Loading branch information
JohnChen0 authored and barancev committed Sep 3, 2019
1 parent 0c72445 commit 288a9c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ public void testMovingMouseByRelativeOffset() {
@NotYetImplemented(HTMLUNIT)
@NotYetImplemented(SAFARI)
@NotYetImplemented(EDGE)
@NotYetImplemented(CHROME)
public void testMovingMouseToRelativeElementOffset() {
driver.get(pages.mouseTrackerPage);

WebElement trackerDiv = driver.findElement(By.id("mousetracker"));
new Actions(driver).moveToElement(trackerDiv, 95, 195).perform();
Dimension size = trackerDiv.getSize();
new Actions(driver).moveToElement(trackerDiv, 95 - size.getWidth() / 2, 195 - size.getHeight() / 2).perform();

WebElement reporter = driver.findElement(By.id("status"));

Expand All @@ -389,7 +389,6 @@ public void testMovingMouseToRelativeElementOffset() {
@NotYetImplemented(HTMLUNIT)
@NotYetImplemented(SAFARI)
@NotYetImplemented(EDGE)
@NotYetImplemented(CHROME)
public void testMovingMouseToRelativeZeroElementOffset() {
driver.get(pages.mouseTrackerPage);

Expand All @@ -398,7 +397,8 @@ public void testMovingMouseToRelativeZeroElementOffset() {

WebElement reporter = driver.findElement(By.id("status"));

wait.until(fuzzyMatchingOfCoordinates(reporter, 0, 0));
Dimension size = trackerDiv.getSize();
wait.until(fuzzyMatchingOfCoordinates(reporter, size.getWidth() / 2, size.getHeight() / 2));
}

@NeedsFreshDriver({IE, CHROME, MARIONETTE, CHROMIUMEDGE})
Expand All @@ -425,7 +425,6 @@ public void testMoveRelativeToBody() {
@NotYetImplemented(HTMLUNIT)
@NotYetImplemented(SAFARI)
@NotYetImplemented(EDGE)
@NotYetImplemented(CHROME)
public void testMoveMouseByOffsetOverAndOutOfAnElement() {
driver.get(pages.mouseOverPage);

Expand All @@ -436,15 +435,19 @@ public void testMoveMouseByOffsetOverAndOutOfAnElement() {
int shiftX = redboxPosition.getX() - greenboxPosition.getX();
int shiftY = redboxPosition.getY() - greenboxPosition.getY();

new Actions(driver).moveToElement(greenbox, 2, 2).perform();
Dimension greenBoxSize = greenbox.getSize();
int xOffset = 2 - greenBoxSize.getWidth() / 2;
int yOffset = 2 - greenBoxSize.getHeight() / 2;

new Actions(driver).moveToElement(greenbox, xOffset, yOffset).perform();

shortWait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));

new Actions(driver).moveToElement(greenbox, 2, 2)
new Actions(driver).moveToElement(greenbox, xOffset, yOffset)
.moveByOffset(shiftX, shiftY).perform();
shortWait.until(attributeToBe(redbox, "background-color", Colors.RED.getColorValue().asRgba()));

new Actions(driver).moveToElement(greenbox, 2, 2)
new Actions(driver).moveToElement(greenbox, xOffset, yOffset)
.moveByOffset(shiftX, shiftY)
.moveByOffset(-shiftX, -shiftY).perform();

Expand All @@ -456,15 +459,15 @@ public void testMoveMouseByOffsetOverAndOutOfAnElement() {
@NotYetImplemented(HTMLUNIT)
@NotYetImplemented(SAFARI)
@NotYetImplemented(EDGE)
@NotYetImplemented(CHROME)
public void testCanMoveOverAndOutOfAnElement() {
driver.get(pages.mouseOverPage);

WebElement greenbox = driver.findElement(By.id("greenbox"));
WebElement redbox = driver.findElement(By.id("redbox"));
Dimension size = redbox.getSize();
Dimension greenSize = greenbox.getSize();
Dimension redSize = redbox.getSize();

new Actions(driver).moveToElement(greenbox, 1, 1).perform();
new Actions(driver).moveToElement(greenbox, 1 - greenSize.getWidth() / 2, 1 - greenSize.getHeight() / 2).perform();

assertThat(Color.fromString(redbox.getCssValue("background-color")))
.isEqualTo(GREEN.getColorValue());
Expand All @@ -475,7 +478,7 @@ public void testCanMoveOverAndOutOfAnElement() {

// IE8 (and *only* IE8) requires a move of 2 pixels. All other browsers
// would be happy with 1.
new Actions(driver).moveToElement(redbox, size.getWidth() + 2, size.getHeight() + 2)
new Actions(driver).moveToElement(redbox, redSize.getWidth() / 2 + 2, redSize.getHeight() / 2 + 2)
.perform();

wait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
Expand Down Expand Up @@ -223,15 +224,15 @@ public void testCanClickOnLinksWithAnOffset() {
@NotYetImplemented(HTMLUNIT)
@NotYetImplemented(SAFARI)
@NotYetImplemented(EDGE)
@NotYetImplemented(CHROME)
public void testClickAfterMoveToAnElementWithAnOffsetShouldUseLastMousePosition() {
driver.get(pages.clickEventPage);

WebElement element = driver.findElement(By.id("eventish"));
Dimension size = element.getSize();
Point location = element.getLocation();

new Actions(driver)
.moveToElement(element, 20, 10)
.moveToElement(element, 20 - size.getWidth() / 2, 10 - size.getHeight() / 2)
.click()
.perform();

Expand Down

0 comments on commit 288a9c3

Please sign in to comment.