|
1 | 1 | package aquality.selenium.elements.actions;
|
2 | 2 |
|
3 | 3 | import aquality.selenium.browser.AqualityServices;
|
| 4 | +import aquality.selenium.core.localization.ILocalizedLogger; |
4 | 5 | import aquality.selenium.core.utilities.IElementActionRetrier;
|
5 | 6 | import aquality.selenium.elements.interfaces.IElement;
|
| 7 | +import org.openqa.selenium.WebElement; |
6 | 8 | import org.openqa.selenium.interactions.Actions;
|
| 9 | +import org.openqa.selenium.interactions.WheelInput.ScrollOrigin; |
7 | 10 |
|
8 |
| -import java.util.function.UnaryOperator; |
| 11 | +import java.util.function.BiFunction; |
9 | 12 |
|
10 | 13 | import static aquality.selenium.browser.AqualityServices.getBrowser;
|
11 | 14 |
|
12 | 15 | public class MouseActions {
|
13 | 16 | private final IElement element;
|
14 | 17 | private final String type;
|
15 | 18 | private final String name;
|
| 19 | + private final ILocalizedLogger logger; |
| 20 | + private final IElementActionRetrier elementActionRetrier; |
16 | 21 |
|
17 | 22 | public MouseActions(IElement element, String type) {
|
18 | 23 | this.element = element;
|
19 | 24 | this.type = type;
|
20 | 25 | this.name = element.getName();
|
| 26 | + this.logger = AqualityServices.getLocalizedLogger(); |
| 27 | + this.elementActionRetrier = AqualityServices.get(IElementActionRetrier.class); |
21 | 28 | }
|
22 | 29 |
|
23 | 30 | /**
|
24 | 31 | * Click via Action.
|
25 | 32 | */
|
26 | 33 | public void click() {
|
27 |
| - infoLoc("loc.clicking"); |
| 34 | + logElementAction("loc.clicking"); |
28 | 35 | new JsActions(element, type).highlightElement();
|
29 |
| - performAction(Actions::click); |
| 36 | + performActionAfterMove((elem, actions) -> actions.click()); |
30 | 37 | }
|
31 | 38 |
|
32 | 39 | /**
|
33 | 40 | * Click Right (calls context menu) on the element
|
34 | 41 | */
|
35 | 42 | public void rightClick() {
|
36 |
| - infoLoc("loc.clicking.right"); |
37 |
| - performAction(actions -> actions.contextClick(element.getElement())); |
| 43 | + logElementAction("loc.clicking.right"); |
| 44 | + performActionAfterMove((elem, actions) -> actions.contextClick(elem)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Scrolling to element |
| 49 | + */ |
| 50 | + public void scrollToElement() { |
| 51 | + logElementAction("loc.scrolling"); |
| 52 | + performAction((elem, actions) -> actions.scrollToElement(elem)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Scrolling by coordinates |
| 57 | + * |
| 58 | + * @param x horizontal coordinate |
| 59 | + * @param y vertical coordinate |
| 60 | + */ |
| 61 | + public void scrollFromOrigin(int x, int y) { |
| 62 | + scrollFromOrigin(x, y, 0, 0); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Scrolling by coordinates |
| 67 | + * |
| 68 | + * @param x horizontal coordinate |
| 69 | + * @param y vertical coordinate |
| 70 | + * @param xOffset horizontal offset |
| 71 | + * @param yOffset vertical offset |
| 72 | + */ |
| 73 | + public void scrollFromOrigin(int x, int y, int xOffset, int yOffset) |
| 74 | + { |
| 75 | + logElementAction("loc.scrolling.by", x, y); |
| 76 | + elementActionRetrier.doWithRetry(() -> { |
| 77 | + ScrollOrigin scrollOrigin = ScrollOrigin.fromElement(element.getElement(), xOffset, yOffset); |
| 78 | + getBrowser().scrollFromOrigin(scrollOrigin, x, y); |
| 79 | + }); |
38 | 80 | }
|
39 | 81 |
|
40 | 82 | /**
|
41 | 83 | * Move mouse to this element.
|
42 | 84 | */
|
43 | 85 | public void moveMouseToElement() {
|
44 |
| - infoLoc("loc.moving"); |
45 |
| - performAction(actions -> actions); |
| 86 | + logElementAction("loc.moving"); |
| 87 | + performActionAfterMove((elem, actions) -> actions); |
46 | 88 | }
|
47 | 89 |
|
48 | 90 | /**
|
49 | 91 | * Move mouse from this element.
|
50 | 92 | */
|
51 | 93 | public void moveMouseFromElement() {
|
52 |
| - infoLoc("loc.movingFrom"); |
53 |
| - AqualityServices.get(IElementActionRetrier.class).doWithRetry(() -> |
54 |
| - new Actions(getBrowser().getDriver()) |
55 |
| - .moveToElement(element.getElement(), -element.getElement().getSize().width, -element.getElement().getSize().height) |
56 |
| - .build().perform()); |
| 94 | + logElementAction("loc.movingFrom"); |
| 95 | + performAction(((elem, actions) -> actions.moveToElement(elem, elem.getSize().width, elem.getSize().height))); |
57 | 96 | }
|
58 | 97 |
|
59 | 98 | /**
|
60 | 99 | * Performs double-click on the element.
|
61 | 100 | */
|
62 | 101 | public void doubleClick() {
|
63 |
| - infoLoc("loc.clicking.double"); |
64 |
| - performAction(actions -> actions.doubleClick(element.getElement())); |
| 102 | + logElementAction("loc.clicking.double"); |
| 103 | + performActionAfterMove((elem, actions) -> actions.doubleClick(elem)); |
| 104 | + } |
| 105 | + |
| 106 | + private void performActionAfterMove(BiFunction<WebElement, Actions, Actions> function) { |
| 107 | + performAction((elem, actions) -> function.apply(elem, actions.moveToElement(elem))); |
65 | 108 | }
|
66 | 109 |
|
67 |
| - private void performAction(UnaryOperator<Actions> function) { |
68 |
| - Actions actions = new Actions(getBrowser().getDriver()).moveToElement(element.getElement()); |
69 |
| - AqualityServices.get(IElementActionRetrier.class).doWithRetry(() -> |
70 |
| - function.apply(actions).build().perform()); |
| 110 | + private void performAction(BiFunction<WebElement, Actions, Actions> action) { |
| 111 | + elementActionRetrier.doWithRetry(() -> action.apply(element.getElement(), new Actions(getBrowser().getDriver())).perform()); |
71 | 112 | }
|
72 | 113 |
|
73 | 114 | /**
|
74 | 115 | * The implementation of a method for logging of MouseActions
|
75 | 116 | *
|
76 |
| - * @param key key in localization resource of message to display in the log. |
| 117 | + * @param key key in localization resource of message to display in the log. |
| 118 | + * @param args Arguments, which will be provided to template of localized message. |
77 | 119 | */
|
78 |
| - private void infoLoc(String key) { |
79 |
| - AqualityServices.getLocalizedLogger().infoElementAction(type, name, key); |
| 120 | + private void logElementAction(String key, Object... args) { |
| 121 | + logger.infoElementAction(type, name, key, args); |
80 | 122 | }
|
81 | 123 | }
|
0 commit comments