Skip to content
5 changes: 5 additions & 0 deletions src/main/java/aquality/selenium/elements/TextBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public void focus() {
ElementActionRetrier.doWithRetry(() -> getElement().sendKeys(""));
}

@Override
public void unfocus() {
ElementActionRetrier.doWithRetry(() -> getElement().sendKeys(Keys.TAB));
}

private void type(final String value, final boolean maskValueInLog) {
info(String.format(logTyping, maskValueInLog ? logMaskedValue : value));
getJsActions().highlightElement();
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/aquality/selenium/elements/actions/MouseActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import aquality.selenium.localization.LocalizationManager;
import aquality.selenium.logger.Logger;
import aquality.selenium.utils.ElementActionRetrier;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.util.function.BiConsumer;
import java.util.function.Function;

public class MouseActions {

Expand All @@ -33,15 +32,15 @@ public MouseActions(IElement element, String type) {
public void click() {
infoLoc("loc.clicking");
new JsActions(element, type).highlightElement();
ElementActionRetrier.doWithRetry(() -> performAction((actions, el) -> actions.click().build().perform()));
performAction(actions -> actions.click());
}

/**
* Click Right (calls context menu) on the element
*/
public void rightClick() {
infoLoc("loc.clicking.right");
ElementActionRetrier.doWithRetry(() -> performAction(((actions, el) -> actions.contextClick(el).build().perform())));
performAction(actions -> actions.contextClick(element.getElement()));
}

/**
Expand All @@ -50,7 +49,16 @@ public void rightClick() {
public void moveMouseToElement() {
infoLoc("loc.moving");
element.getElement().getCoordinates().inViewPort();
ElementActionRetrier.doWithRetry(() -> performAction((actions, el) -> actions.moveToElement(el).build().perform()));
performAction(actions -> actions.moveToElement(element.getElement()));
}

/**
* Move mouse from this element.
*/
public void moveMouseFromElement() {
infoLoc("loc.movingFrom");
performAction(actions -> actions.moveToElement(element.getElement(),
-element.getElement().getSize().width / 2, -element.getElement().getSize().height / 2));
}

/**
Expand All @@ -59,12 +67,13 @@ public void moveMouseToElement() {
public void doubleClick() {
infoLoc("loc.clicking.double");
ElementActionRetrier.doWithRetry(() -> (getBrowser().getDriver()).getMouse().mouseMove(element.getElement().getCoordinates()));
ElementActionRetrier.doWithRetry(() -> performAction(((actions, el) -> actions.doubleClick(el).build().perform())));
performAction(actions -> actions.doubleClick(element.getElement()));
}

private void performAction(BiConsumer<Actions, WebElement> consumer) {
Actions actions = new Actions(getBrowser().getDriver());
consumer.accept(actions.moveToElement(element.getElement()), element.getElement());
private void performAction(Function<Actions, Actions> function) {
Actions actions = new Actions(getBrowser().getDriver()).moveToElement(element.getElement());
ElementActionRetrier.doWithRetry(() ->
function.apply(actions).build().perform());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ public interface ITextBox extends IElement {
*/
String getValue();

/**
* Focuses on the element using send keys
*/
void focus();

/**
* Removes focus from the element using send keys
*/
void unfocus();
}
1 change: 1 addition & 0 deletions src/main/resources/localization/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"loc.label" : "Label",
"loc.link" : "Link",
"loc.moving" : "Moving mouse to element",
"loc.movingFrom" : "Moving mouse from element",
"loc.radio" : "RadioButton",
"loc.scrolling.center.js" : "Scrolling to the center via JavaScript",
"loc.scrolling.js" : "Scrolling via JavaScript",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/localization/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"loc.label" : "Надпись",
"loc.link" : "Ссылка",
"loc.moving" : "Наведение курсора на элемент",
"loc.movingFrom" : "Сдвиг курсора с элемента",
"loc.radio" : "Радиокнопка",
"loc.scrolling.center.js" : "Скроллинг в центр (при помощи JavaScript)",
"loc.scrolling.js" : "Скроллинг с помощью JavaScript",
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/tests/integration/ActionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public void testMoveMouseToElement() {
Assert.assertTrue(button.getText().contains("More"), "element is not focused after moveMouseToElement()");
}

@Test
public void testMoveMouseFromElement() {
ProductListForm productListForm = new ProductListForm();
productListForm.getLblLastProduct().getMouseActions().moveMouseToElement();
IButton button = productListForm.getBtnLastProductMore();
Assert.assertTrue(button.getText().contains("More"), "element is not focused after moveMouseToElement()");
productListForm.getLblLastProduct().getMouseActions().moveMouseFromElement();
Assert.assertFalse(button.state().isDisplayed(), "element is still focused after moveMouseFromElement()");
}

@Test
public void testGetElementText() {
ProductListForm productListForm = new ProductListForm();
Expand Down