Skip to content

Commit

Permalink
Add constructor accepting FluentWebElement to ElementActions and add …
Browse files Browse the repository at this point in the history
…missing moveByOffset method (#697)
  • Loading branch information
slawekradzyminski committed Jan 31, 2019
1 parent 3ebd903 commit 55208ef
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
@@ -1,5 +1,6 @@
package org.fluentlenium.core.action;

import org.fluentlenium.core.domain.FluentWebElement;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Expand All @@ -24,12 +25,23 @@ public KeyboardElementActions(WebDriver driver, WebElement element) {
this.element = element;
}

/**
* Creates a new object to execute actions with the keyboard, using given selenium driver and element.
*
* @param driver selenium driver
* @param fluentWebElement FluentWebElement on which to execute actions
*/
public KeyboardElementActions(WebDriver driver, FluentWebElement fluentWebElement) {
this.driver = driver;
this.element = fluentWebElement.getElement();
}

/**
* Get selenium interactions actions.
*
* @return selenium actions
*/
protected org.openqa.selenium.interactions.Actions actions() {
private org.openqa.selenium.interactions.Actions actions() {
return new org.openqa.selenium.interactions.Actions(driver);
}

Expand Down
Expand Up @@ -108,4 +108,19 @@ public MouseActions contextClick() {
actions().contextClick().perform();
return this;
}

/**
* Moves the mouse from its current position (or 0,0) by the given offset. If the coordinates
* provided are outside the viewport (the mouse will end up outside the browser window) then
* the viewport is scrolled to match.
* @param xOffset horizontal offset. A negative value means moving the mouse left.
* @param yOffset vertical offset. A negative value means moving the mouse up.
* @return this object reference to chain calls
* @see org.openqa.selenium.interactions.Actions#moveByOffset(int, int)
*/
public MouseActions moveByOffset(int xOffset, int yOffset) {
actions().moveByOffset(xOffset, yOffset).perform();
return this;
}

}
@@ -1,5 +1,6 @@
package org.fluentlenium.core.action;

import org.fluentlenium.core.domain.FluentWebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Coordinates;
Expand All @@ -24,6 +25,17 @@ public MouseElementActions(WebDriver driver, WebElement element) {
this.element = element;
}

/**
* Creates a new mouse element actions.
*
* @param driver selenium driver
* @param fluentWebElement FluentWebElement
*/
public MouseElementActions(WebDriver driver, FluentWebElement fluentWebElement) {
this.driver = driver;
this.element = fluentWebElement.getElement();
}

private org.openqa.selenium.interactions.Actions actions() {
return new org.openqa.selenium.interactions.Actions(driver);
}
Expand Down
@@ -1,6 +1,7 @@
package org.fluentlenium.core.action;

import org.assertj.core.api.Assertions;
import org.fluentlenium.core.domain.FluentWebElement;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -34,6 +35,9 @@ public class KeyboardElementActionsTest {
@Mock
private LocatableElement element;

@Mock
private FluentWebElement fluentWebElement;

@Mock
private Coordinates coordinates;

Expand All @@ -51,14 +55,25 @@ public void after() {
}

@Test
public void testKeyDown() {
public void testKeyDownWebElement() {
KeyboardElementActions actions = new KeyboardElementActions(driver, element);
actions.keyDown(Keys.SHIFT);

verify(mouse).click(coordinates);
verify(keyboard).pressKey(Keys.SHIFT);
}

@Test
public void testKeyDownFluentWebElement() {
when(fluentWebElement.getElement()).thenReturn(element);

KeyboardElementActions actions = new KeyboardElementActions(driver, fluentWebElement);
actions.keyDown(Keys.SHIFT);

verify(mouse).click(coordinates);
verify(keyboard).pressKey(Keys.SHIFT);
}

@Test
public void testKeyUp() {
KeyboardElementActions actions = new KeyboardElementActions(driver, element);
Expand Down
Expand Up @@ -92,6 +92,13 @@ public void testBasic() {
Assertions.assertThat(actions.basic()).isSameAs(mouse);
}

@Test
public void moveByOffset() {
MouseActions actions = new MouseActions(driver);
actions.moveByOffset(1, 1);
verify(mouse).mouseMove(null, 1, 1);
}

private abstract static class InputDevicesDriver implements WebDriver, HasInputDevices { // NOPMD AbstractNaming
}
}
@@ -1,6 +1,7 @@
package org.fluentlenium.core.action;

import org.assertj.core.api.Assertions;
import org.fluentlenium.core.domain.FluentWebElement;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -35,6 +36,9 @@ public class MouseElementActionsTest {
@Mock
private LocatableElement element;

@Mock
private FluentWebElement fluentWebElement;

@Mock
private Coordinates coordinates;

Expand All @@ -61,14 +65,26 @@ public void testClickAndHold() {
}

@Test
public void testClick() {
public void testClickWebElement() {
MouseElementActions actions = new MouseElementActions(driver, element);
actions.click();

verify(mouse).mouseMove(coordinates);
verify(mouse).click(coordinates);
}

@Test
public void testClickFluentWebElement() {
when(fluentWebElement.getElement()).thenReturn(element);

MouseElementActions actions = new MouseElementActions(driver, fluentWebElement);
actions.click();

verify(mouse).mouseMove(coordinates);
verify(mouse).click(coordinates);
}


@Test
public void testContextClick() {
MouseElementActions actions = new MouseElementActions(driver, element);
Expand Down

0 comments on commit 55208ef

Please sign in to comment.