-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement native scroll actions (#142) +semver: feature
- cover with tests - replace js references with native actions in pre-existing methods - add localization values for scrolling actions - refactor MouseActions - Update to Selenium 4.23.0
- Loading branch information
Showing
15 changed files
with
167 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 63 additions & 21 deletions
84
src/main/java/aquality/selenium/elements/actions/MouseActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,123 @@ | ||
package aquality.selenium.elements.actions; | ||
|
||
import aquality.selenium.browser.AqualityServices; | ||
import aquality.selenium.core.localization.ILocalizedLogger; | ||
import aquality.selenium.core.utilities.IElementActionRetrier; | ||
import aquality.selenium.elements.interfaces.IElement; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.interactions.Actions; | ||
import org.openqa.selenium.interactions.WheelInput.ScrollOrigin; | ||
|
||
import java.util.function.UnaryOperator; | ||
import java.util.function.BiFunction; | ||
|
||
import static aquality.selenium.browser.AqualityServices.getBrowser; | ||
|
||
public class MouseActions { | ||
private final IElement element; | ||
private final String type; | ||
private final String name; | ||
private final ILocalizedLogger logger; | ||
private final IElementActionRetrier elementActionRetrier; | ||
|
||
public MouseActions(IElement element, String type) { | ||
this.element = element; | ||
this.type = type; | ||
this.name = element.getName(); | ||
this.logger = AqualityServices.getLocalizedLogger(); | ||
this.elementActionRetrier = AqualityServices.get(IElementActionRetrier.class); | ||
} | ||
|
||
/** | ||
* Click via Action. | ||
*/ | ||
public void click() { | ||
infoLoc("loc.clicking"); | ||
logElementAction("loc.clicking"); | ||
new JsActions(element, type).highlightElement(); | ||
performAction(Actions::click); | ||
performActionAfterMove((elem, actions) -> actions.click()); | ||
} | ||
|
||
/** | ||
* Click Right (calls context menu) on the element | ||
*/ | ||
public void rightClick() { | ||
infoLoc("loc.clicking.right"); | ||
performAction(actions -> actions.contextClick(element.getElement())); | ||
logElementAction("loc.clicking.right"); | ||
performActionAfterMove((elem, actions) -> actions.contextClick(elem)); | ||
} | ||
|
||
/** | ||
* Scrolling to element | ||
*/ | ||
public void scrollToElement() { | ||
logElementAction("loc.scrolling"); | ||
performAction((elem, actions) -> actions.scrollToElement(elem)); | ||
} | ||
|
||
/** | ||
* Scrolling by coordinates | ||
* | ||
* @param x horizontal coordinate | ||
* @param y vertical coordinate | ||
*/ | ||
public void scrollFromOrigin(int x, int y) { | ||
scrollFromOrigin(x, y, 0, 0); | ||
} | ||
|
||
/** | ||
* Scrolling by coordinates | ||
* | ||
* @param x horizontal coordinate | ||
* @param y vertical coordinate | ||
* @param xOffset horizontal offset | ||
* @param yOffset vertical offset | ||
*/ | ||
public void scrollFromOrigin(int x, int y, int xOffset, int yOffset) | ||
{ | ||
logElementAction("loc.scrolling.by", x, y); | ||
elementActionRetrier.doWithRetry(() -> { | ||
ScrollOrigin scrollOrigin = ScrollOrigin.fromElement(element.getElement(), xOffset, yOffset); | ||
getBrowser().scrollFromOrigin(scrollOrigin, x, y); | ||
}); | ||
} | ||
|
||
/** | ||
* Move mouse to this element. | ||
*/ | ||
public void moveMouseToElement() { | ||
infoLoc("loc.moving"); | ||
performAction(actions -> actions); | ||
logElementAction("loc.moving"); | ||
performActionAfterMove((elem, actions) -> actions); | ||
} | ||
|
||
/** | ||
* Move mouse from this element. | ||
*/ | ||
public void moveMouseFromElement() { | ||
infoLoc("loc.movingFrom"); | ||
AqualityServices.get(IElementActionRetrier.class).doWithRetry(() -> | ||
new Actions(getBrowser().getDriver()) | ||
.moveToElement(element.getElement(), -element.getElement().getSize().width, -element.getElement().getSize().height) | ||
.build().perform()); | ||
logElementAction("loc.movingFrom"); | ||
performAction(((elem, actions) -> actions.moveToElement(elem, elem.getSize().width, elem.getSize().height))); | ||
} | ||
|
||
/** | ||
* Performs double-click on the element. | ||
*/ | ||
public void doubleClick() { | ||
infoLoc("loc.clicking.double"); | ||
performAction(actions -> actions.doubleClick(element.getElement())); | ||
logElementAction("loc.clicking.double"); | ||
performActionAfterMove((elem, actions) -> actions.doubleClick(elem)); | ||
} | ||
|
||
private void performActionAfterMove(BiFunction<WebElement, Actions, Actions> function) { | ||
performAction((elem, actions) -> function.apply(elem, actions.moveToElement(elem))); | ||
} | ||
|
||
private void performAction(UnaryOperator<Actions> function) { | ||
Actions actions = new Actions(getBrowser().getDriver()).moveToElement(element.getElement()); | ||
AqualityServices.get(IElementActionRetrier.class).doWithRetry(() -> | ||
function.apply(actions).build().perform()); | ||
private void performAction(BiFunction<WebElement, Actions, Actions> action) { | ||
elementActionRetrier.doWithRetry(() -> action.apply(element.getElement(), new Actions(getBrowser().getDriver())).perform()); | ||
} | ||
|
||
/** | ||
* The implementation of a method for logging of MouseActions | ||
* | ||
* @param key key in localization resource of message to display in the log. | ||
* @param key key in localization resource of message to display in the log. | ||
* @param args Arguments, which will be provided to template of localized message. | ||
*/ | ||
private void infoLoc(String key) { | ||
AqualityServices.getLocalizedLogger().infoElementAction(type, name, key); | ||
private void logElementAction(String key, Object... args) { | ||
logger.infoElementAction(type, name, key, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters