Skip to content

Commit

Permalink
[java] Add missing support events for Web Driver Listener (#13210)
Browse files Browse the repository at this point in the history
* add missing events to listeners w/ javadoc and tests

* run format script

* run format script

---------

Co-authored-by: Titus Fortner <[email protected]>
Co-authored-by: Diego Molina <[email protected]>
  • Loading branch information
3 people authored Jan 24, 2024
1 parent ca9c0c1 commit b5259d8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
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) {}
}
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

0 comments on commit b5259d8

Please sign in to comment.