Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): ✨ added executeScript method for element #969

Merged
merged 4 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.util.Collection;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -99,6 +100,27 @@ public static <E> E getElementAttribute (final Function<WebElement, E> action, f
}
}

/**
* Gets element specific attributes.
*
* @param action action to get element specific attributes
* @param locator locator to find element
* @param defaultValue default value if any error occurred
* @param <E> attribute type
*
* @return element specific attribute.
*/
public static <E> E getElementAttribute (final BiFunction<WebDriver, WebElement, E> action, final Locator locator,
final E defaultValue) {
LOGGER.traceEntry ();
try {
prepareElementAction (find (locator, VISIBLE), "green");
return LOGGER.traceExit (action.apply (getSession ().getDriver (), find (locator, VISIBLE)));
} catch (final FrameworkError e) {
return defaultValue;
}
}

/**
* Pause till the specified delay.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.github.boykaframework.config.ui.DelaySetting;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;

Expand Down Expand Up @@ -92,6 +93,17 @@ public void clear () {
LOGGER.traceExit ();
}

@SuppressWarnings ("unchecked")
@Override
public <T> T executeScript (final String script, final Object... args) {
LOGGER.traceEntry ();
LOGGER.info ("Executing script on element");
ofNullable (this.listener).ifPresent (l -> l.onExecuteScript (this.locator, script, args));
return (T) getElementAttribute (
(driver, element) -> ((JavascriptExecutor) driver).executeScript (script, element, args), this.locator,
null);
}

@Override
public String getAttribute (final String attribute) {
LOGGER.traceEntry ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ public interface IElementActions {
*/
void clear ();

/**
* Executes javascript with the element.
*
* @param script Javascript to execute
* @param args Arguments to pass to javascript
* @param <T> Return type of the result of the script
*
* @return the result of the script
*/
<T> T executeScript (final String script, final Object... args);

/**
* Gets the value of the attribute of the element.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ default void onClear (final Locator locator) {
// not implemented.
}

/**
* Handles execute script method.
*
* @param locator Locator of the element.
* @param script JS script
* @param args Arguments of the script.
*/
default void onExecuteScript (final Locator locator, final String script, final Object... args) {
// not implemented.
}

/**
* Handle get attribute method.
*
Expand Down
133 changes: 70 additions & 63 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions website/docs/api/actions/elements/element-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ import static io.github.boykaframework.actions.elements.ElementActions.onElement
boolean enabled = onElement (locator).isEnabled ();
```

### `executeScript (script, args)` {#execute-script}

This method is used to execute the given JavaScript on the given element.

```java
import static io.github.boykaframework.actions.elements.ElementActions.onElement;
. . .
String result = onElement (locator).executeScript ("return arguments[0].innerHTML");
```

### `isSelected` {#is-selected}

This method is used to check whether the given element is selected or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ In order inject your customized logging for your reports or you want to perform

This method will get executed after clearing the value in the element located by using the provided locator.

## `onExecuteScript (script, args)` {#on-execute-script}

This method will get executed after executing the provided script on the element with the provided arguments.

## `onGetAttribute (locator, attribute)` {#on-get-attribute}

This method will get executed after getting the given attribute in the element located by using the provided locator.
Expand Down
Loading