Skip to content

Commit

Permalink
[dotnet] only support moving to center of element
Browse files Browse the repository at this point in the history
calculating the upper left corner of the element does not work when element is not completely scrolled into view so it is being removed
  • Loading branch information
titusfortner committed Jun 23, 2022
1 parent 24a5741 commit fbfb491
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 61 deletions.
54 changes: 2 additions & 52 deletions dotnet/src/webdriver/Interactions/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@

namespace OpenQA.Selenium.Interactions
{
/// <summary>
/// Provides values that indicate from where element offsets for MoveToElement
/// are calculated.
/// Note: TopLeft only does the expected thing when the element is completely
/// inside the viewport.
/// </summary>
[Obsolete("Starting in Selenium 4.3 only Center behavior will be supported")]
public enum MoveToElementOffsetOrigin
{
/// <summary>
/// Offsets are calculated from the top-left corner of the element.
/// </summary>
TopLeft,

/// <summary>
/// Offsets are calculated from the center of the element.
/// </summary>
Center
}

/// <summary>
/// Provides a mechanism for building advanced interactions with the browser.
/// </summary>
Expand Down Expand Up @@ -305,9 +285,7 @@ public Actions MoveToElement(IWebElement toElement)
throw new ArgumentException("MoveToElement cannot move to a null element with no offset.", nameof(toElement));
}

ILocatable target = GetLocatableFromElement(toElement);
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(toElement, 0, 0, DefaultMouseMoveDuration));
return this;
return this.MoveToElement(toElement, 0, 0);
}

/// <summary>
Expand All @@ -320,35 +298,7 @@ public Actions MoveToElement(IWebElement toElement)
/// <returns>A self-reference to this <see cref="Actions"/>.</returns>
public Actions MoveToElement(IWebElement toElement, int offsetX, int offsetY)
{
return this.MoveToElement(toElement, offsetX, offsetY, MoveToElementOffsetOrigin.TopLeft);
}

/// <summary>
/// Moves the mouse to the specified offset of the top-left corner of the specified element.
/// </summary>
/// <param name="toElement">The element to which to move the mouse.</param>
/// <param name="offsetX">The horizontal offset to which to move the mouse.</param>
/// <param name="offsetY">The vertical offset to which to move the mouse.</param>
/// <param name="offsetOrigin">The <see cref="MoveToElementOffsetOrigin"/> value from which to calculate the offset.</param>
/// <returns>A self-reference to this <see cref="Actions"/>.</returns>
[Obsolete("Starting in Selenium 4.3 only MoveToElementOffsetOrigin.Center will be supported")]
public Actions MoveToElement(IWebElement toElement, int offsetX, int offsetY, MoveToElementOffsetOrigin offsetOrigin)
{
ILocatable target = GetLocatableFromElement(toElement);

if (offsetOrigin == MoveToElementOffsetOrigin.TopLeft)
{
Size elementSize = toElement.Size;

int modifiedOffsetX = offsetX - (elementSize.Width / 2);
int modifiedOffsetY = offsetY - (elementSize.Height / 2);

this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(toElement, modifiedOffsetX, modifiedOffsetY, DefaultMouseMoveDuration));
}
else
{
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(toElement, offsetX, offsetY, DefaultMouseMoveDuration));
}
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(toElement, offsetX, offsetY, DefaultMouseMoveDuration));
return this;
}

Expand Down
25 changes: 17 additions & 8 deletions dotnet/test/common/Interactions/BasicMouseInterfaceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ public void MovingMouseToRelativeElementOffset()
driver.Url = mouseTrackerPage;

IWebElement trackerDiv = driver.FindElement(By.Id("mousetracker"));
new Actions(driver).MoveToElement(trackerDiv, 95, 195).Build().Perform();
Size size = trackerDiv.Size;

new Actions(driver).MoveToElement(trackerDiv, 95 - size.Width / 2, 195 - size.Height / 2).Build().Perform();

IWebElement reporter = driver.FindElement(By.Id("status"));

Expand All @@ -311,7 +313,9 @@ public void MovingMouseToRelativeZeroElementOffset()
driver.Url = mouseTrackerPage;

IWebElement trackerDiv = driver.FindElement(By.Id("mousetracker"));
new Actions(driver).MoveToElement(trackerDiv, 0, 0).Perform();
Size size = trackerDiv.Size;

new Actions(driver).MoveToElement(trackerDiv, size.Width / 2, size.Height / 2).Perform();

IWebElement reporter = driver.FindElement(By.Id("status"));

Expand Down Expand Up @@ -343,13 +347,17 @@ public void MoveMouseByOffsetOverAndOutOfAnElement()
int shiftX = redboxPosition.X - greenboxPosition.X;
int shiftY = redboxPosition.Y - greenboxPosition.Y;

new Actions(driver).MoveToElement(greenbox, 2, 2).Perform();
Size greenBoxSize = greenbox.Size;
int xOffset = 2 - greenBoxSize.Width / 2;
int yOffset = 2 - greenBoxSize.Height / 2;

new Actions(driver).MoveToElement(greenbox, xOffset, yOffset).Perform();
WaitFor(ElementColorToBe(redbox, Color.Green), "element color was not green");

new Actions(driver).MoveToElement(greenbox, 2, 2).MoveByOffset(shiftX, shiftY).Perform();
new Actions(driver).MoveToElement(greenbox, xOffset, yOffset).MoveByOffset(shiftX, shiftY).Perform();
WaitFor(ElementColorToBe(redbox, Color.Red), "element color was not red");

new Actions(driver).MoveToElement(greenbox, 2, 2).MoveByOffset(shiftX, shiftY).MoveByOffset(-shiftX, -shiftY).Perform();
new Actions(driver).MoveToElement(greenbox, xOffset, yOffset).MoveByOffset(shiftX, shiftY).MoveByOffset(-shiftX, -shiftY).Perform();
WaitFor(ElementColorToBe(redbox, Color.Green), "element color was not red");
}

Expand All @@ -360,15 +368,16 @@ public void CanMouseOverAndOutOfAnElement()

IWebElement greenbox = driver.FindElement(By.Id("greenbox"));
IWebElement redbox = driver.FindElement(By.Id("redbox"));
Size size = redbox.Size;
Size greenSize = greenbox.Size;
Size redSize = redbox.Size;

new Actions(driver).MoveToElement(greenbox, 1, 1).Perform();
new Actions(driver).MoveToElement(greenbox, 1 - greenSize.Width / 2, 1 - greenSize.Height / 2).Perform();
Assert.That(redbox.GetCssValue("background-color"), Is.EqualTo("rgba(0, 128, 0, 1)").Or.EqualTo("rgb(0, 128, 0)"));

new Actions(driver).MoveToElement(redbox).Perform();
Assert.That(redbox.GetCssValue("background-color"), Is.EqualTo("rgba(255, 0, 0, 1)").Or.EqualTo("rgb(255, 0, 0)"));

new Actions(driver).MoveToElement(redbox, size.Width + 2, size.Height + 2).Perform();
new Actions(driver).MoveToElement(redbox, redSize.Width / 2 + 2, redSize.Height / 2 + 2).Perform();
Assert.That(redbox.GetCssValue("background-color"), Is.EqualTo("rgba(0, 128, 0, 1)").Or.EqualTo("rgb(0, 128, 0)"));
}

Expand Down
3 changes: 2 additions & 1 deletion dotnet/test/common/Interactions/CombinedInputActionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ public void ClickAfterMoveToAnElementWithAnOffsetShouldUseLastMousePosition()

IWebElement element = driver.FindElement(By.Id("eventish"));
Point location = element.Location;
Size size = element.Size;

new Actions(driver)
.MoveToElement(element, 20, 10)
.MoveToElement(element, 20 - size.Width / 2, 10 - size.Height / 2)
.Click()
.Perform();

Expand Down

0 comments on commit fbfb491

Please sign in to comment.