Skip to content

Commit 8472410

Browse files
[java] [dotnet] Add move to location method to Actions (#11509)
* [java] Add move to location method to Actions Related to #10724 * [dotnet] Add move to location method to Actions class * [java] only support one implementation of moveToLocation pointer action Co-authored-by: titusfortner <[email protected]>
1 parent fd36c53 commit 8472410

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

dotnet/src/webdriver/Interactions/Actions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,18 @@ public Actions MoveByOffset(int offsetX, int offsetY)
437437
return this;
438438
}
439439

440+
/// <summary>
441+
/// Moves the mouse from the upper left corner of the current viewport by the provided offset.
442+
/// </summary>
443+
/// <param name="offsetX">The horizontal offset to which to move the mouse.</param>
444+
/// <param name="offsetY">The vertical offset to which to move the mouse.</param>
445+
/// <returns>A self-reference to this <see cref="Actions"/>.</returns>
446+
public Actions MoveToLocation(int offsetX, int offsetY)
447+
{
448+
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(CoordinateOrigin.Viewport, offsetX, offsetY, DefaultMouseMoveDuration));
449+
return this;
450+
}
451+
440452
/// <summary>
441453
/// Right-clicks the mouse on the specified element.
442454
/// </summary>

dotnet/test/common/DriverTestFixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public abstract class DriverTestFixture
7272
public string mapVisibilityPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("map_visibility.html");
7373
public string mouseTrackerPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mousePositionTracker.html");
7474
public string mouseOverPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mouseOver.html");
75+
public string mouseInteractionPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mouse_interaction.html");
7576
public string readOnlyPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("readOnlyPage.html");
7677
public string clicksPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("clicks.html");
7778
public string booleanAttributes = EnvironmentManager.Instance.UrlBuilder.WhereIs("booleanAttributes.html");

dotnet/test/common/Interactions/BasicMouseInterfaceTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ public void ShouldAllowMoveAndClick()
145145
Assert.AreEqual("Clicked", toClick.GetAttribute("value"), "Value should change to Clicked.");
146146
}
147147

148+
[Test]
149+
public void ShouldMoveToLocation()
150+
{
151+
driver.Url = mouseInteractionPage;
152+
153+
Actions actionProvider = new Actions(driver);
154+
actionProvider.MoveToLocation(100, 200).Build().Perform();
155+
156+
IWebElement location = driver.FindElement(By.Id("absolute-location"));
157+
var coordinates = location.Text.Split(',');
158+
Assert.AreEqual("100", coordinates[0].Trim());
159+
Assert.AreEqual("200", coordinates[1].Trim());
160+
}
161+
148162
[Test]
149163
public void ShouldNotMoveToANullLocator()
150164
{
@@ -456,6 +470,11 @@ private Func<bool> TitleToBe(string desiredTitle)
456470
return () => driver.Title == desiredTitle;
457471
}
458472

473+
private Func<bool> ValueToBe(IWebElement element, string desiredValue)
474+
{
475+
return () => element.GetDomProperty("value") == desiredValue;
476+
}
477+
459478
private Func<bool> ElementTextToEqual(IWebElement element, string text)
460479
{
461480
return () => element.Text == text;

java/src/org/openqa/selenium/interactions/Actions.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Set;
3131
import java.util.function.IntConsumer;
3232
import org.openqa.selenium.Keys;
33+
import org.openqa.selenium.Point;
3334
import org.openqa.selenium.WebDriver;
3435
import org.openqa.selenium.WebElement;
3536
import org.openqa.selenium.interactions.PointerInput.Origin;
@@ -352,9 +353,9 @@ private Actions moveInTicks(WebElement target, int xOffset, int yOffset) {
352353
}
353354

354355
/**
355-
* Moves the mouse from its current position (or 0,0) by the given offset. If the coordinates
356-
* provided are outside the viewport (the mouse will end up outside the browser window) then the
357-
* viewport is scrolled to match.
356+
* Moves the mouse from its current position (or 0,0) by the given offset. If the final
357+
* coordinates of the move are outside the viewport (the mouse will end up outside the browser window),
358+
* an exception is raised.
358359
*
359360
* @param xOffset horizontal offset. A negative value means moving the mouse left.
360361
* @param yOffset vertical offset. A negative value means moving the mouse up.
@@ -368,6 +369,24 @@ public Actions moveByOffset(int xOffset, int yOffset) {
368369
.createPointerMove(Duration.ofMillis(200), Origin.pointer(), xOffset, yOffset));
369370
}
370371

372+
/**
373+
* Moves the mouse to provided coordinates on screen regardless of starting position of
374+
* the mouse. If the coordinates
375+
* provided are outside the viewport (the mouse will end up outside the browser window),
376+
* an exception is raised.
377+
*
378+
* @param xCoordinate positive pixel value along horizontal axis in viewport. Numbers increase going right.
379+
* @param yCoordinate positive pixel value along vertical axis in viewport. Numbers increase going down.
380+
* @return A self reference.
381+
* @throws MoveTargetOutOfBoundsException if the provided offset is outside the document's
382+
* boundaries.
383+
*/
384+
public Actions moveToLocation(int xCoordinate, int yCoordinate) {
385+
return tick(
386+
getActivePointer()
387+
.createPointerMove(Duration.ofMillis(250), Origin.viewport(), xCoordinate, yCoordinate));
388+
}
389+
371390
/**
372391
* Performs a context-click at middle of the given element. First performs a mouseMove to the
373392
* location of the element.

java/test/org/openqa/selenium/interactions/DefaultMouseTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,19 @@ void testContextClick() {
190190
assertThat(toContextClick.getAttribute("value")).isEqualTo("ContextClicked");
191191
}
192192

193+
@Test
194+
void testMoveToLocation() {
195+
driver.get(pages.mouseInteractionPage);
196+
197+
Action moveAndClick = getBuilder(driver).moveToLocation(70, 60).click().build();
198+
199+
moveAndClick.perform();
200+
201+
WebElement element = driver.findElement(By.id("greeting"));
202+
203+
assertThat(element.getText()).isEqualTo("Success!");
204+
}
205+
193206
@Test
194207
void testMoveAndClick() {
195208
driver.get(pages.javascriptPage);

0 commit comments

Comments
 (0)