Skip to content

Commit

Permalink
feat(java): ⚡ updated mouse related actions (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
WasiqB authored Dec 28, 2024
1 parent 6db08b2 commit dbfc7c6
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static void performMobileGestures (final Collection<Sequence> sequences)
private static void highlight (final String color, final WebElement element) {
if (getSession ().getWebSetting ()
.isHighlight ()) {
final var style = element.getAttribute ("style");
final var style = element.getDomAttribute ("style");
getSession ().setSharedData (HIGHLIGHT_STYLE, style);
withDriver ().executeScript ("arguments[0].setAttribute('style', arguments[1] + arguments[2]);", element,
style, format ("color: {0}; border: 3px solid {0};", color));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,18 @@

import static io.github.boykaframework.actions.CommonActions.pause;
import static io.github.boykaframework.actions.CommonActions.performElementAction;
import static io.github.boykaframework.actions.elements.ElementFinder.find;
import static io.github.boykaframework.enums.ApplicationType.WEB;
import static io.github.boykaframework.actions.CommonActions.performMobileGestures;
import static io.github.boykaframework.enums.ListenerType.CLICKABLE_ACTION;
import static io.github.boykaframework.enums.PlatformType.IOS;
import static io.github.boykaframework.enums.WaitStrategy.CLICKABLE;
import static io.github.boykaframework.manager.ParallelSession.getSession;
import static io.github.boykaframework.utils.Validator.validateDelay;
import static java.util.Collections.singletonList;
import static java.util.Optional.ofNullable;
import static org.apache.logging.log4j.LogManager.getLogger;

import io.github.boykaframework.actions.interfaces.elements.IClickableActions;
import io.github.boykaframework.actions.interfaces.listeners.elements.IClickableActionsListener;
import io.github.boykaframework.builders.Locator;
import io.github.boykaframework.enums.PlatformType;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

/**
* Handles all mouse related actions
Expand Down Expand Up @@ -68,16 +63,11 @@ public void click () {
LOGGER.traceEntry ();
LOGGER.info ("Clicking on element: {}", this.locator.getName ());
ofNullable (this.listener).ifPresent (l -> l.onClick (this.locator));
final var session = getSession ();
if (session.getPlatformType () == PlatformType.WEB || (session.getMobileSetting ()
.getDevice ()
.getApplication ()
.getType () == WEB && session.getPlatformType () == IOS)) {
pause (this.delaySetting.getBeforeClick ());
performElementAction (WebElement::click, this.locator);
} else {
tap ();
}
pause (this.delaySetting.getBeforeClick ());
MouseActionBuilder.builder ()
.sourceLocator (this.locator)
.build ()
.click ();
LOGGER.traceExit ();
}

Expand All @@ -86,12 +76,10 @@ public void clickAndHold () {
LOGGER.traceEntry ();
LOGGER.info ("Click and hold on element: {}", this.locator.getName ());
ofNullable (this.listener).ifPresent (l -> l.onClickAndHold (this.locator));
performElementAction ((driver, element) -> {
final var actions = new Actions (driver);
actions.pause (validateDelay (this.delaySetting.getBeforeClick ()))
.clickAndHold (element)
.perform ();
}, this.locator);
MouseActionBuilder.builder ()
.sourceLocator (this.locator)
.build ()
.clickAndHold ();
LOGGER.traceExit ();
}

Expand All @@ -100,12 +88,10 @@ public void doubleClick () {
LOGGER.traceEntry ();
LOGGER.info ("Double Click on element: {}", this.locator.getName ());
ofNullable (this.listener).ifPresent (l -> l.onDoubleClick (this.locator));
performElementAction ((driver, element) -> {
final var actions = new Actions (driver);
actions.pause (validateDelay (this.delaySetting.getBeforeClick ()))
.doubleClick (element)
.perform ();
}, this.locator);
MouseActionBuilder.builder ()
.sourceLocator (this.locator)
.build ()
.doubleClick ();
LOGGER.traceExit ();
}

Expand All @@ -114,12 +100,10 @@ public void dragTo (final Locator destination) {
LOGGER.traceEntry ();
LOGGER.info ("Drag and Drop on element: {} , {}", this.locator.getName (), destination.getName ());
ofNullable (this.listener).ifPresent (l -> l.onDragTo (this.locator, destination));
performElementAction ((driver, element) -> {
final var actions = new Actions (driver);
actions.pause (validateDelay (this.delaySetting.getBeforeMouseMove ()))
.dragAndDrop (element, find (destination, CLICKABLE))
.perform ();
}, this.locator);
MouseActionBuilder.builder ()
.sourceLocator (this.locator)
.build ()
.dragAndDrop (destination);
LOGGER.traceExit ();
}

Expand All @@ -128,12 +112,34 @@ public void hover () {
LOGGER.traceEntry ();
LOGGER.info ("Hover on element: {}", this.locator.getName ());
ofNullable (this.listener).ifPresent (l -> l.onHover (this.locator));
performElementAction ((driver, element) -> {
final var actions = new Actions (driver);
actions.pause (validateDelay (this.delaySetting.getBeforeMouseMove ()))
.moveToElement (element)
.perform ();
}, this.locator);
MouseActionBuilder.builder ()
.sourceLocator (this.locator)
.build ()
.moveTo ();
LOGGER.traceExit ();
}

@Override
public void pressBackButton () {
LOGGER.traceEntry ();
LOGGER.info ("Pressing the Mouse Back button...");
ofNullable (this.listener).ifPresent (IClickableActionsListener::onPressBackButton);
final var sequence = MouseActionBuilder.builder ()
.build ()
.backButtonClick ();
performMobileGestures (singletonList (sequence));
LOGGER.traceExit ();
}

@Override
public void pressForwardButton () {
LOGGER.traceEntry ();
LOGGER.info ("Pressing the Mouse Forward button...");
ofNullable (this.listener).ifPresent (IClickableActionsListener::onPressForwardButton);
final var sequence = MouseActionBuilder.builder ()
.build ()
.forwardButtonClick ();
performMobileGestures (singletonList (sequence));
LOGGER.traceExit ();
}

Expand All @@ -142,12 +148,22 @@ public void rightClick () {
LOGGER.traceEntry ();
LOGGER.info ("Right Click on element: {}", this.locator.getName ());
ofNullable (this.listener).ifPresent (l -> l.onRightClick (this.locator));
performElementAction ((driver, element) -> {
final var actions = new Actions (driver);
actions.pause (validateDelay (this.delaySetting.getBeforeClick ()))
.contextClick (element)
.perform ();
}, this.locator);
MouseActionBuilder.builder ()
.sourceLocator (this.locator)
.build ()
.rightClick ();
LOGGER.traceExit ();
}

@Override
public void scrollToElement () {
LOGGER.traceEntry ();
LOGGER.info ("Scrolling to element: {}", this.locator.getName ());
ofNullable (this.listener).ifPresent (l -> l.onScrollToElement (this.locator));
MouseActionBuilder.builder ()
.sourceLocator (this.locator)
.build ()
.scrollTo ();
LOGGER.traceExit ();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* MIT License
*
* Copyright (c) 2024, Boyka Framework
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package io.github.boykaframework.actions.elements;

import static io.github.boykaframework.actions.CommonActions.performDriverAction;
import static io.github.boykaframework.actions.CommonActions.performElementAction;
import static io.github.boykaframework.actions.elements.ElementFinder.find;
import static io.github.boykaframework.enums.Message.ELEMENT_CANNOT_BE_NULL;
import static io.github.boykaframework.enums.WaitStrategy.CLICKABLE;
import static io.github.boykaframework.manager.ParallelSession.getSession;
import static io.github.boykaframework.utils.Validator.requireNonNull;
import static java.time.Duration.ofMillis;
import static java.util.Objects.isNull;
import static org.openqa.selenium.interactions.PointerInput.Kind.MOUSE;
import static org.openqa.selenium.interactions.PointerInput.MouseButton.BACK;
import static org.openqa.selenium.interactions.PointerInput.MouseButton.FORWARD;

import java.util.function.BiFunction;

import io.github.boykaframework.builders.Locator;
import lombok.Builder;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;

/**
* Handle all the mouse specific actions.
*
* @author Wasiq Bhamla
* @since 27-Dec-2024
*/
@Builder
public class MouseActionBuilder {
private Locator sourceLocator;

Sequence backButtonClick () {
return mouseAction (BACK);
}

void click () {
performElementAction ((d, e) -> new Actions (d).click (e)
.perform (), this.sourceLocator);
}

void clickAndHold () {
performElementAction ((d, e) -> new Actions (d).clickAndHold (e)
.perform (), this.sourceLocator);
}

void doubleClick () {
performElementAction ((d, e) -> new Actions (d).doubleClick (e)
.perform (), this.sourceLocator);
}

void dragAndDrop (final Locator targetLocator) {
final var targetElement = find (requireNonNull (targetLocator, ELEMENT_CANNOT_BE_NULL), CLICKABLE);
performElementAction ((d, e) -> new Actions (d).dragAndDrop (e, targetElement)
.perform (), this.sourceLocator);
}

Sequence forwardButtonClick () {
return mouseAction (FORWARD);
}

void moveTo () {
performElementAction ((d, e) -> new Actions (d).moveToElement (e)
.perform (), this.sourceLocator);
}

void rightClick () {
if (isNull (this.sourceLocator)) {
performDriverAction ((d) -> new Actions (d).contextClick ()
.perform ());
} else {
performElementAction ((d, e) -> new Actions (d).contextClick (e)
.perform (), this.sourceLocator);
}
}

void scrollTo () {
performElementAction ((d, e) -> new Actions (d).scrollToElement (e)
.perform (), this.sourceLocator);
}

private Sequence composeMouseSequence (final BiFunction<PointerInput, Sequence, Sequence> steps) {
final var mouse = new PointerInput (MOUSE, "Default Mouse");
final var sequence = new Sequence (mouse, 0);
return steps.apply (mouse, sequence);
}

private Sequence mouseAction (final PointerInput.MouseButton button) {
final var delaySetting = getSession ().getSetting ()
.getUi ()
.getDelay ();

return composeMouseSequence ((mouse, steps) -> {
steps.addAction (mouse.createPointerDown (button.asArg ()));
steps.addAction (new Pause (mouse, ofMillis (delaySetting.getBeforeSwipe ())));
steps.addAction (mouse.createPointerUp (button.asArg ()));
return steps;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,26 @@ public interface IClickableActions extends IFingersActions {
*/
void hover ();

/**
* Presses the back button on Mouse.
*/
void pressBackButton ();

/**
* Presses the forward button on Mouse.
*/
void pressForwardButton ();

/**
* RightClick on element
*/
void rightClick ();

/**
* Scroll to element.
*/
void scrollToElement ();

/**
* Submit the element.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ default void onHover (final Locator locator) {
// not implemented.
}

/**
* Handles the pressBackButton method.
*/
default void onPressBackButton () {
// not implemented.
}

/**
* Handles the pressForwardButton method.
*/
default void onPressForwardButton () {
// not implemented.
}

/**
* Handle right click method.
*
Expand All @@ -81,6 +95,15 @@ default void onRightClick (final Locator locator) {
// not implemented.
}

/**
* Scroll to the element.
*
* @param locator locator of the element.
*/
default void onScrollToElement (final Locator locator) {
// not implemented.
}

/**
* Handle submit method.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"lerna": "8.1.9",
"lerna-changelog": "^2.2.0",
"lerna-version": "^6.6.2",
"lint-staged": "^15.2.11",
"lint-staged": "^15.3.0",
"lodash": "^4.17.21",
"nx": "^20.3.0",
"prettier": "^3.4.2",
Expand Down
Loading

0 comments on commit dbfc7c6

Please sign in to comment.