Skip to content

Commit b5259d8

Browse files
LogicOscartitusfortnerdiemol
authored
[java] Add missing support events for Web Driver Listener (#13210)
* 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]>
1 parent ca9c0c1 commit b5259d8

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

java/src/org/openqa/selenium/support/events/EventFiringDecorator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ private void fireBeforeEvents(
230230
listener.beforeAnyOptionsCall((WebDriver.Options) target.getOriginal(), method, args);
231231
} else if (target.getOriginal() instanceof WebDriver.Timeouts) {
232232
listener.beforeAnyTimeoutsCall((WebDriver.Timeouts) target.getOriginal(), method, args);
233+
} else if (target.getOriginal() instanceof WebDriver.TargetLocator) {
234+
listener.beforeAnyTargetLocatorCall(
235+
(WebDriver.TargetLocator) target.getOriginal(), method, args);
233236
} else if (target.getOriginal() instanceof WebDriver.Window) {
234237
listener.beforeAnyWindowCall((WebDriver.Window) target.getOriginal(), method, args);
235238
}
@@ -287,6 +290,9 @@ private void fireAfterEvents(
287290
listener.afterAnyOptionsCall((WebDriver.Options) target.getOriginal(), method, args, res);
288291
} else if (target.getOriginal() instanceof WebDriver.Timeouts) {
289292
listener.afterAnyTimeoutsCall((WebDriver.Timeouts) target.getOriginal(), method, args, res);
293+
} else if (target.getOriginal() instanceof WebDriver.TargetLocator) {
294+
listener.afterAnyTargetLocatorCall(
295+
(WebDriver.TargetLocator) target.getOriginal(), method, args, res);
290296
} else if (target.getOriginal() instanceof WebDriver.Window) {
291297
listener.afterAnyWindowCall((WebDriver.Window) target.getOriginal(), method, args, res);
292298
}

java/src/org/openqa/selenium/support/events/WebDriverListener.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.openqa.selenium.ScriptKey;
3535
import org.openqa.selenium.WebDriver;
3636
import org.openqa.selenium.WebElement;
37+
import org.openqa.selenium.WindowType;
3738
import org.openqa.selenium.interactions.Actions;
3839
import org.openqa.selenium.interactions.Sequence;
3940
import org.openqa.selenium.remote.RemoteWebDriver;
@@ -1058,4 +1059,48 @@ default void beforeFullscreen(WebDriver.Window window) {}
10581059
* @param window The window object that will be called
10591060
*/
10601061
default void afterFullscreen(WebDriver.Window window) {}
1062+
1063+
// Target Locator
1064+
1065+
/**
1066+
* Called before any method in {@link WebDriver.TargetLocator} class.
1067+
*
1068+
* @param targetLocator the target locator being used for the action
1069+
* @param method the method being invoked
1070+
* @param args the arguments to the method
1071+
*/
1072+
default void beforeAnyTargetLocatorCall(
1073+
WebDriver.TargetLocator targetLocator, Method method, Object[] args) {}
1074+
1075+
/**
1076+
* Called after any method in {@link WebDriver.TargetLocator} class.
1077+
*
1078+
* @param targetLocator the target locator being used for the action
1079+
* @param method the method being invoked
1080+
* @param args the arguments to the method
1081+
* @param result the result of the method call
1082+
*/
1083+
default void afterAnyTargetLocatorCall(
1084+
WebDriver.TargetLocator targetLocator, Method method, Object[] args, Object result) {}
1085+
1086+
/**
1087+
* This action will be performed each time before {@link
1088+
* org.openqa.selenium.WebDriver.TargetLocator#window(String)}
1089+
*
1090+
* @param targetLocator the target locator being used for the action
1091+
* @param nameOrHandle The name of the window or the handle as returned by {@link
1092+
* org.openqa.selenium.WebDriver#getWindowHandle()} or <code>null</code> if switching to a new
1093+
* window created by {@link org.openqa.selenium.WebDriver.TargetLocator#newWindow(WindowType)}
1094+
*/
1095+
default void beforeWindow(WebDriver.TargetLocator targetLocator, String nameOrHandle) {}
1096+
1097+
/**
1098+
* @param targetLocator the target locator being used for the action
1099+
* @param nameOrHandle The name of the window or the handle as returned by {@link
1100+
* org.openqa.selenium.WebDriver#getWindowHandle()} or <code>null</code> if switching to a new
1101+
* window created by {@link org.openqa.selenium.WebDriver.TargetLocator#newWindow(WindowType)}
1102+
* @param driver WebDriver
1103+
*/
1104+
default void afterWindow(
1105+
WebDriver.TargetLocator targetLocator, String nameOrHandle, WebDriver driver) {}
10611106
}

java/test/org/openqa/selenium/support/events/EventFiringDecoratorTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,58 @@ public void afterExecuteAsyncScript(
778778
"afterAnyCall executeAsyncScript"));
779779
}
780780

781+
@Test
782+
void shouldFireTargetLocatorEvents() {
783+
WebDriver driver = mock(WebDriver.class);
784+
WebDriver.TargetLocator targetLocator = mock(WebDriver.TargetLocator.class);
785+
when(driver.switchTo()).thenReturn(targetLocator);
786+
787+
CollectorListener listener =
788+
new CollectorListener() {
789+
@Override
790+
public void beforeAnyTargetLocatorCall(
791+
WebDriver.TargetLocator targetLocator, Method method, Object[] args) {
792+
acc.append("beforeAnyTargetLocatorCall ").append(method.getName()).append("\n");
793+
}
794+
795+
@Override
796+
public void afterAnyTargetLocatorCall(
797+
WebDriver.TargetLocator targetLocator, Method method, Object[] args, Object result) {
798+
acc.append("afterAnyTargetLocatorCall ").append(method.getName()).append("\n");
799+
}
800+
801+
@Override
802+
public void beforeWindow(WebDriver.TargetLocator targetLocator, String windowName) {
803+
acc.append("beforeWindow ").append(windowName).append("\n");
804+
}
805+
806+
@Override
807+
public void afterWindow(
808+
WebDriver.TargetLocator targetLocator, String windowName, WebDriver driver) {
809+
acc.append("afterWindow ").append(windowName).append("\n");
810+
}
811+
};
812+
813+
WebDriver decorated = new EventFiringDecorator<>(listener).decorate(driver);
814+
815+
decorated.switchTo().window("windowName");
816+
817+
assertThat(listener.acc.toString().trim())
818+
.isEqualTo(
819+
String.join(
820+
"\n",
821+
"beforeAnyCall switchTo",
822+
"beforeAnyWebDriverCall switchTo",
823+
"afterAnyWebDriverCall switchTo",
824+
"afterAnyCall switchTo",
825+
"beforeAnyCall window",
826+
"beforeAnyTargetLocatorCall window",
827+
"beforeWindow windowName",
828+
"afterWindow windowName",
829+
"afterAnyTargetLocatorCall window",
830+
"afterAnyCall window"));
831+
}
832+
781833
@Test
782834
void shouldSuppressExceptionInBeforeAnyCall() {
783835
WebDriver driver = mock(WebDriver.class);

0 commit comments

Comments
 (0)