Skip to content

Commit 2755531

Browse files
authored
Merge 8514aa9 into d717074
2 parents d717074 + 8514aa9 commit 2755531

5 files changed

Lines changed: 67 additions & 3 deletions

File tree

dotnet/src/webdriver/Interactions/Actions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ public Actions MoveByOffset(int offsetX, int offsetY)
314314
return this;
315315
}
316316

317+
/// <summary>
318+
/// Moves the mouse from the upper left corner of the current viewport by the provided offset.
319+
/// </summary>
320+
/// <param name="offsetX">The horizontal offset to which to move the mouse.</param>
321+
/// <param name="offsetY">The vertical offset to which to move the mouse.</param>
322+
/// <returns>A self-reference to this <see cref="Actions"/>.</returns>
323+
public Actions MoveToLocation(int offsetX, int offsetY)
324+
{
325+
this.actionBuilder.AddAction(this.defaultMouse.CreatePointerMove(CoordinateOrigin.Viewport, offsetX, offsetY, DefaultMouseMoveDuration));
326+
return this;
327+
}
328+
317329
/// <summary>
318330
/// Right-clicks the mouse on the specified element.
319331
/// </summary>

dotnet/test/common/DriverTestFixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public abstract class DriverTestFixture
7070
public string mapVisibilityPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("map_visibility.html");
7171
public string mouseTrackerPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mousePositionTracker.html");
7272
public string mouseOverPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mouseOver.html");
73+
public string mouseInteractionPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("mouse_interaction.html");
7374
public string readOnlyPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("readOnlyPage.html");
7475
public string clicksPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("clicks.html");
7576
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
@@ -134,6 +134,20 @@ public void ShouldAllowMoveAndClick()
134134
Assert.AreEqual("Clicked", toClick.GetAttribute("value"), "Value should change to Clicked.");
135135
}
136136

137+
[Test]
138+
public void ShouldMoveToLocation()
139+
{
140+
driver.Url = mouseInteractionPage;
141+
142+
Actions actionProvider = new Actions(driver);
143+
actionProvider.MoveToLocation(100, 200).Build().Perform();
144+
145+
IWebElement location = driver.FindElement(By.Id("absolute-location"));
146+
var coordinates = location.Text.Split(',');
147+
Assert.AreEqual("100", coordinates[0].Trim());
148+
Assert.AreEqual("200", coordinates[1].Trim());
149+
}
150+
137151
[Test]
138152
public void ShouldNotMoveToANullLocator()
139153
{
@@ -445,6 +459,11 @@ private Func<bool> TitleToBe(string desiredTitle)
445459
return () => driver.Title == desiredTitle;
446460
}
447461

462+
private Func<bool> ValueToBe(IWebElement element, string desiredValue)
463+
{
464+
return () => element.GetDomProperty("value") == desiredValue;
465+
}
466+
448467
private Func<bool> ElementTextToEqual(IWebElement element, string text)
449468
{
450469
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)