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

[java] Add missing support events for Web Driver Listener #13210

Merged
merged 5 commits into from
Jan 24, 2024
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 @@ -230,6 +230,9 @@ private void fireBeforeEvents(
listener.beforeAnyOptionsCall((WebDriver.Options) target.getOriginal(), method, args);
} else if (target.getOriginal() instanceof WebDriver.Timeouts) {
listener.beforeAnyTimeoutsCall((WebDriver.Timeouts) target.getOriginal(), method, args);
} else if (target.getOriginal() instanceof WebDriver.TargetLocator) {
listener.beforeAnyTargetLocatorCall(
(WebDriver.TargetLocator) target.getOriginal(), method, args);
} else if (target.getOriginal() instanceof WebDriver.Window) {
listener.beforeAnyWindowCall((WebDriver.Window) target.getOriginal(), method, args);
}
Expand Down Expand Up @@ -287,6 +290,9 @@ private void fireAfterEvents(
listener.afterAnyOptionsCall((WebDriver.Options) target.getOriginal(), method, args, res);
} else if (target.getOriginal() instanceof WebDriver.Timeouts) {
listener.afterAnyTimeoutsCall((WebDriver.Timeouts) target.getOriginal(), method, args, res);
} else if (target.getOriginal() instanceof WebDriver.TargetLocator) {
listener.afterAnyTargetLocatorCall(
(WebDriver.TargetLocator) target.getOriginal(), method, args, res);
} else if (target.getOriginal() instanceof WebDriver.Window) {
listener.afterAnyWindowCall((WebDriver.Window) target.getOriginal(), method, args, res);
}
Expand Down
45 changes: 45 additions & 0 deletions java/src/org/openqa/selenium/support/events/WebDriverListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openqa.selenium.ScriptKey;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand Down Expand Up @@ -1058,4 +1059,48 @@ default void beforeFullscreen(WebDriver.Window window) {}
* @param window The window object that will be called
*/
default void afterFullscreen(WebDriver.Window window) {}

// Target Locator

/**
* Called before any method in {@link WebDriver.TargetLocator} class.
*
* @param targetLocator the target locator being used for the action
* @param method the method being invoked
* @param args the arguments to the method
*/
default void beforeAnyTargetLocatorCall(
WebDriver.TargetLocator targetLocator, Method method, Object[] args) {}

/**
* Called after any method in {@link WebDriver.TargetLocator} class.
*
* @param targetLocator the target locator being used for the action
* @param method the method being invoked
* @param args the arguments to the method
* @param result the result of the method call
*/
default void afterAnyTargetLocatorCall(
WebDriver.TargetLocator targetLocator, Method method, Object[] args, Object result) {}

/**
* This action will be performed each time before {@link
* org.openqa.selenium.WebDriver.TargetLocator#window(String)}
*
* @param targetLocator the target locator being used for the action
* @param nameOrHandle The name of the window or the handle as returned by {@link
* org.openqa.selenium.WebDriver#getWindowHandle()} or <code>null</code> if switching to a new
* window created by {@link org.openqa.selenium.WebDriver.TargetLocator#newWindow(WindowType)}
*/
default void beforeWindow(WebDriver.TargetLocator targetLocator, String nameOrHandle) {}

/**
* @param targetLocator the target locator being used for the action
* @param nameOrHandle The name of the window or the handle as returned by {@link
* org.openqa.selenium.WebDriver#getWindowHandle()} or <code>null</code> if switching to a new
* window created by {@link org.openqa.selenium.WebDriver.TargetLocator#newWindow(WindowType)}
* @param driver WebDriver
*/
default void afterWindow(
WebDriver.TargetLocator targetLocator, String nameOrHandle, WebDriver driver) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind adding hooks for all methods from TargetLocator?

}
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,58 @@ public void afterExecuteAsyncScript(
"afterAnyCall executeAsyncScript"));
}

@Test
void shouldFireTargetLocatorEvents() {
WebDriver driver = mock(WebDriver.class);
WebDriver.TargetLocator targetLocator = mock(WebDriver.TargetLocator.class);
when(driver.switchTo()).thenReturn(targetLocator);

CollectorListener listener =
new CollectorListener() {
@Override
public void beforeAnyTargetLocatorCall(
WebDriver.TargetLocator targetLocator, Method method, Object[] args) {
acc.append("beforeAnyTargetLocatorCall ").append(method.getName()).append("\n");
}

@Override
public void afterAnyTargetLocatorCall(
WebDriver.TargetLocator targetLocator, Method method, Object[] args, Object result) {
acc.append("afterAnyTargetLocatorCall ").append(method.getName()).append("\n");
}

@Override
public void beforeWindow(WebDriver.TargetLocator targetLocator, String windowName) {
acc.append("beforeWindow ").append(windowName).append("\n");
}

@Override
public void afterWindow(
WebDriver.TargetLocator targetLocator, String windowName, WebDriver driver) {
acc.append("afterWindow ").append(windowName).append("\n");
}
};

WebDriver decorated = new EventFiringDecorator<>(listener).decorate(driver);

decorated.switchTo().window("windowName");

assertThat(listener.acc.toString().trim())
.isEqualTo(
String.join(
"\n",
"beforeAnyCall switchTo",
"beforeAnyWebDriverCall switchTo",
"afterAnyWebDriverCall switchTo",
"afterAnyCall switchTo",
"beforeAnyCall window",
"beforeAnyTargetLocatorCall window",
"beforeWindow windowName",
"afterWindow windowName",
"afterAnyTargetLocatorCall window",
"afterAnyCall window"));
}

@Test
void shouldSuppressExceptionInBeforeAnyCall() {
WebDriver driver = mock(WebDriver.class);
Expand Down
Loading