Skip to content

Commit fbfb491

Browse files
committed
[dotnet] only support moving to center of element
calculating the upper left corner of the element does not work when element is not completely scrolled into view so it is being removed
1 parent 24a5741 commit fbfb491

File tree

3 files changed

+21
-61
lines changed

3 files changed

+21
-61
lines changed

dotnet/src/webdriver/Interactions/Actions.cs

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,6 @@
2222

2323
namespace OpenQA.Selenium.Interactions
2424
{
25-
/// <summary>
26-
/// Provides values that indicate from where element offsets for MoveToElement
27-
/// are calculated.
28-
/// Note: TopLeft only does the expected thing when the element is completely
29-
/// inside the viewport.
30-
/// </summary>
31-
[Obsolete("Starting in Selenium 4.3 only Center behavior will be supported")]
32-
public enum MoveToElementOffsetOrigin
33-
{
34-
/// <summary>
35-
/// Offsets are calculated from the top-left corner of the element.
36-
/// </summary>
37-
TopLeft,
38-
39-
/// <summary>
40-
/// Offsets are calculated from the center of the element.
41-
/// </summary>
42-
Center
43-
}
44-
4525
/// <summary>
4626
/// Provides a mechanism for building advanced interactions with the browser.
4727
/// </summary>
@@ -305,9 +285,7 @@ public Actions MoveToElement(IWebElement toElement)
305285
throw new ArgumentException("MoveToElement cannot move to a null element with no offset.", nameof(toElement));
306286
}
307287

308-
ILocatable target = GetLocatableFromElement(toElement);
309-
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(toElement, 0, 0, DefaultMouseMoveDuration));
310-
return this;
288+
return this.MoveToElement(toElement, 0, 0);
311289
}
312290

313291
/// <summary>
@@ -320,35 +298,7 @@ public Actions MoveToElement(IWebElement toElement)
320298
/// <returns>A self-reference to this <see cref="Actions"/>.</returns>
321299
public Actions MoveToElement(IWebElement toElement, int offsetX, int offsetY)
322300
{
323-
return this.MoveToElement(toElement, offsetX, offsetY, MoveToElementOffsetOrigin.TopLeft);
324-
}
325-
326-
/// <summary>
327-
/// Moves the mouse to the specified offset of the top-left corner of the specified element.
328-
/// </summary>
329-
/// <param name="toElement">The element to which to move the mouse.</param>
330-
/// <param name="offsetX">The horizontal offset to which to move the mouse.</param>
331-
/// <param name="offsetY">The vertical offset to which to move the mouse.</param>
332-
/// <param name="offsetOrigin">The <see cref="MoveToElementOffsetOrigin"/> value from which to calculate the offset.</param>
333-
/// <returns>A self-reference to this <see cref="Actions"/>.</returns>
334-
[Obsolete("Starting in Selenium 4.3 only MoveToElementOffsetOrigin.Center will be supported")]
335-
public Actions MoveToElement(IWebElement toElement, int offsetX, int offsetY, MoveToElementOffsetOrigin offsetOrigin)
336-
{
337-
ILocatable target = GetLocatableFromElement(toElement);
338-
339-
if (offsetOrigin == MoveToElementOffsetOrigin.TopLeft)
340-
{
341-
Size elementSize = toElement.Size;
342-
343-
int modifiedOffsetX = offsetX - (elementSize.Width / 2);
344-
int modifiedOffsetY = offsetY - (elementSize.Height / 2);
345-
346-
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(toElement, modifiedOffsetX, modifiedOffsetY, DefaultMouseMoveDuration));
347-
}
348-
else
349-
{
350-
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(toElement, offsetX, offsetY, DefaultMouseMoveDuration));
351-
}
301+
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(toElement, offsetX, offsetY, DefaultMouseMoveDuration));
352302
return this;
353303
}
354304

dotnet/test/common/Interactions/BasicMouseInterfaceTest.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ public void MovingMouseToRelativeElementOffset()
298298
driver.Url = mouseTrackerPage;
299299

300300
IWebElement trackerDiv = driver.FindElement(By.Id("mousetracker"));
301-
new Actions(driver).MoveToElement(trackerDiv, 95, 195).Build().Perform();
301+
Size size = trackerDiv.Size;
302+
303+
new Actions(driver).MoveToElement(trackerDiv, 95 - size.Width / 2, 195 - size.Height / 2).Build().Perform();
302304

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

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

313315
IWebElement trackerDiv = driver.FindElement(By.Id("mousetracker"));
314-
new Actions(driver).MoveToElement(trackerDiv, 0, 0).Perform();
316+
Size size = trackerDiv.Size;
317+
318+
new Actions(driver).MoveToElement(trackerDiv, size.Width / 2, size.Height / 2).Perform();
315319

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

@@ -343,13 +347,17 @@ public void MoveMouseByOffsetOverAndOutOfAnElement()
343347
int shiftX = redboxPosition.X - greenboxPosition.X;
344348
int shiftY = redboxPosition.Y - greenboxPosition.Y;
345349

346-
new Actions(driver).MoveToElement(greenbox, 2, 2).Perform();
350+
Size greenBoxSize = greenbox.Size;
351+
int xOffset = 2 - greenBoxSize.Width / 2;
352+
int yOffset = 2 - greenBoxSize.Height / 2;
353+
354+
new Actions(driver).MoveToElement(greenbox, xOffset, yOffset).Perform();
347355
WaitFor(ElementColorToBe(redbox, Color.Green), "element color was not green");
348356

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

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

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

361369
IWebElement greenbox = driver.FindElement(By.Id("greenbox"));
362370
IWebElement redbox = driver.FindElement(By.Id("redbox"));
363-
Size size = redbox.Size;
371+
Size greenSize = greenbox.Size;
372+
Size redSize = redbox.Size;
364373

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

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

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

dotnet/test/common/Interactions/CombinedInputActionsTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ public void ClickAfterMoveToAnElementWithAnOffsetShouldUseLastMousePosition()
194194

195195
IWebElement element = driver.FindElement(By.Id("eventish"));
196196
Point location = element.Location;
197+
Size size = element.Size;
197198

198199
new Actions(driver)
199-
.MoveToElement(element, 20, 10)
200+
.MoveToElement(element, 20 - size.Width / 2, 10 - size.Height / 2)
200201
.Click()
201202
.Perform();
202203

0 commit comments

Comments
 (0)