Skip to content

Commit 0b2265c

Browse files
valfirstpujagani
andauthored
[java] Add missing event handlers for TargetLocator interface (#13559)
* [java] Add missing event handlers for `TargetLocator` interface * Fix formatting --------- Co-authored-by: Puja Jagani <[email protected]>
1 parent 85dd3b4 commit 0b2265c

File tree

2 files changed

+309
-13
lines changed

2 files changed

+309
-13
lines changed

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

Lines changed: 160 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,24 +1083,177 @@ default void beforeAnyTargetLocatorCall(
10831083
default void afterAnyTargetLocatorCall(
10841084
WebDriver.TargetLocator targetLocator, Method method, Object[] args, Object result) {}
10851085

1086+
/**
1087+
* This action will be performed each time before {@link WebDriver.TargetLocator#frame(int)} ()}
1088+
* is called.
1089+
*
1090+
* @param targetLocator The target locator being used for the action.
1091+
* @param index The zero-based index.
1092+
*/
1093+
default void beforeFrame(WebDriver.TargetLocator targetLocator, int index) {}
1094+
1095+
/**
1096+
* This action will be performed each time after {@link WebDriver.TargetLocator#frame(int)} ()} is
1097+
* called.
1098+
*
1099+
* @param targetLocator The target locator being used for the action.
1100+
* @param index The zero-based index.
1101+
*/
1102+
default void afterFrame(WebDriver.TargetLocator targetLocator, int index, WebDriver driver) {}
1103+
1104+
/**
1105+
* This action will be performed each time before {@link WebDriver.TargetLocator#frame(String)}
1106+
* ()} is called.
1107+
*
1108+
* @param targetLocator The target locator being used for the action.
1109+
* @param nameOrId The name of the frame window, the id of the &lt;frame&gt; or &lt;iframe&gt;
1110+
* element, or the (zero-based) index.
1111+
*/
1112+
default void beforeFrame(WebDriver.TargetLocator targetLocator, String nameOrId) {}
1113+
1114+
/**
1115+
* This action will be performed each time after {@link WebDriver.TargetLocator#frame(String)} ()}
1116+
* is called.
1117+
*
1118+
* @param targetLocator The target locator being used for the action.
1119+
* @param nameOrId The name of the frame window, the id of the &lt;frame&gt; or &lt;iframe&gt;
1120+
* element, or the (zero-based) index.
1121+
* @param driver The current driver instance.
1122+
*/
1123+
default void afterFrame(
1124+
WebDriver.TargetLocator targetLocator, String nameOrId, WebDriver driver) {}
1125+
10861126
/**
10871127
* This action will be performed each time before {@link
1088-
* org.openqa.selenium.WebDriver.TargetLocator#window(String)}
1128+
* WebDriver.TargetLocator#frame(WebElement)} ()} is called.
1129+
*
1130+
* @param targetLocator The target locator being used for the action.
1131+
* @param frameElement The frame element to switch to.
1132+
*/
1133+
default void beforeFrame(WebDriver.TargetLocator targetLocator, WebElement frameElement) {}
1134+
1135+
/**
1136+
* This action will be performed each time after {@link WebDriver.TargetLocator#frame(WebElement)}
1137+
* ()} is called.
1138+
*
1139+
* @param targetLocator The target locator being used for the action.
1140+
* @param frameElement The frame element to switch to.
1141+
* @param driver The current driver instance.
1142+
*/
1143+
default void afterFrame(
1144+
WebDriver.TargetLocator targetLocator, WebElement frameElement, WebDriver driver) {}
1145+
1146+
/**
1147+
* This action will be performed each time before {@link WebDriver.TargetLocator#parentFrame()} is
1148+
* called.
1149+
*
1150+
* @param targetLocator The target locator being used for the action.
1151+
*/
1152+
default void beforeParentFrame(WebDriver.TargetLocator targetLocator) {}
1153+
1154+
/**
1155+
* This action will be performed each time after {@link WebDriver.TargetLocator#parentFrame()} is
1156+
* called.
1157+
*
1158+
* @param targetLocator The target locator being used for the action.
1159+
* @param driver The current driver instance.
1160+
*/
1161+
default void afterParentFrame(WebDriver.TargetLocator targetLocator, WebDriver driver) {}
1162+
1163+
/**
1164+
* This action will be performed each time before {@link WebDriver.TargetLocator#window(String)}.
10891165
*
10901166
* @param targetLocator the target locator being used for the action
10911167
* @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)}
1168+
* WebDriver#getWindowHandle()} or <code>null</code> if switching to a new window created by
1169+
* {@link WebDriver.TargetLocator#newWindow(WindowType)}
10941170
*/
10951171
default void beforeWindow(WebDriver.TargetLocator targetLocator, String nameOrHandle) {}
10961172

10971173
/**
1098-
* @param targetLocator the target locator being used for the action
1174+
* This action will be performed each time after {@link WebDriver.TargetLocator#window(String)}.
1175+
*
1176+
* @param targetLocator The target locator being used for the action.
10991177
* @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
1178+
* WebDriver#getWindowHandle()} or <code>null</code> if switching to a new window created by
1179+
* {@link WebDriver.TargetLocator#newWindow(WindowType)}.
1180+
* @param driver The current driver instance.
11031181
*/
11041182
default void afterWindow(
11051183
WebDriver.TargetLocator targetLocator, String nameOrHandle, WebDriver driver) {}
1184+
1185+
/**
1186+
* This action will be performed each time before {@link
1187+
* WebDriver.TargetLocator#newWindow(WindowType)} is called.
1188+
*
1189+
* @param targetLocator The target locator being used for the action.
1190+
* @param typeHint The type of new browser window to be created. The created window is not
1191+
* guaranteed to be of the requested type; if the driver does not support the requested type,
1192+
* a new browser window will be created of whatever type the driver does support.
1193+
*/
1194+
default void beforeNewWindow(WebDriver.TargetLocator targetLocator, WindowType typeHint) {}
1195+
1196+
/**
1197+
* This action will be performed each time after {@link
1198+
* WebDriver.TargetLocator#newWindow(WindowType)} is called.
1199+
*
1200+
* @param targetLocator The target locator being used for the action.
1201+
* @param typeHint The type of new browser window to be created. The created window is not
1202+
* guaranteed to be of the requested type; if the driver does not support the requested type,
1203+
* a new browser window will be created of whatever type the driver does support.
1204+
* @param driver The current driver instance.
1205+
*/
1206+
default void afterNewWindow(
1207+
WebDriver.TargetLocator targetLocator, WindowType typeHint, WebDriver driver) {}
1208+
1209+
/**
1210+
* This action will be performed each time before {@link WebDriver.TargetLocator#defaultContent()}
1211+
* is called.
1212+
*
1213+
* @param targetLocator The target locator being used for the action.
1214+
*/
1215+
default void beforeDefaultContent(WebDriver.TargetLocator targetLocator) {}
1216+
1217+
/**
1218+
* This action will be performed each time after {@link WebDriver.TargetLocator#defaultContent()}
1219+
* is called.
1220+
*
1221+
* @param targetLocator The target locator being used for the action.
1222+
* @param driver The current driver instance.
1223+
*/
1224+
default void afterDefaultContent(WebDriver.TargetLocator targetLocator, WebDriver driver) {}
1225+
1226+
/**
1227+
* This action will be performed each time before {@link WebDriver.TargetLocator#activeElement()}
1228+
* is called.
1229+
*
1230+
* @param targetLocator The target locator being used for the action.
1231+
*/
1232+
default void beforeActiveElement(WebDriver.TargetLocator targetLocator) {}
1233+
1234+
/**
1235+
* This action will be performed each time after {@link WebDriver.TargetLocator#activeElement()}
1236+
* is called.
1237+
*
1238+
* @param targetLocator The target locator being used for the action.
1239+
* @param driver The current driver instance.
1240+
*/
1241+
default void afterActiveElement(WebDriver.TargetLocator targetLocator, WebDriver driver) {}
1242+
1243+
/**
1244+
* This action will be performed each time before {@link WebDriver.TargetLocator#alert()} is
1245+
* called.
1246+
*
1247+
* @param targetLocator The target locator being used for the action.
1248+
*/
1249+
default void beforeAlert(WebDriver.TargetLocator targetLocator) {}
1250+
1251+
/**
1252+
* This action will be performed each time after {@link WebDriver.TargetLocator#alert()} is
1253+
* called.
1254+
*
1255+
* @param targetLocator The target locator being used for the action.
1256+
* @param alert The found alert.
1257+
*/
1258+
default void afterAlert(WebDriver.TargetLocator targetLocator, Alert alert) {}
11061259
}

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

Lines changed: 149 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.openqa.selenium.WebDriver;
4949
import org.openqa.selenium.WebDriverException;
5050
import org.openqa.selenium.WebElement;
51+
import org.openqa.selenium.WindowType;
5152
import org.openqa.selenium.interactions.Actions;
5253
import org.openqa.selenium.interactions.Sequence;
5354
import org.openqa.selenium.remote.RemoteWebDriver;
@@ -780,8 +781,8 @@ public void afterExecuteAsyncScript(
780781

781782
@Test
782783
void shouldFireTargetLocatorEvents() {
783-
WebDriver driver = mock(WebDriver.class);
784-
WebDriver.TargetLocator targetLocator = mock(WebDriver.TargetLocator.class);
784+
WebDriver driver = mock();
785+
WebDriver.TargetLocator targetLocator = mock();
785786
when(driver.switchTo()).thenReturn(targetLocator);
786787

787788
CollectorListener listener =
@@ -798,6 +799,49 @@ public void afterAnyTargetLocatorCall(
798799
acc.append("afterAnyTargetLocatorCall ").append(method.getName()).append("\n");
799800
}
800801

802+
@Override
803+
public void beforeFrame(WebDriver.TargetLocator targetLocator, int index) {
804+
acc.append("beforeFrame ").append(index).append("\n");
805+
}
806+
807+
@Override
808+
public void afterFrame(
809+
WebDriver.TargetLocator targetLocator, int index, WebDriver driver) {
810+
acc.append("afterFrame ").append(index).append("\n");
811+
}
812+
813+
@Override
814+
public void beforeFrame(WebDriver.TargetLocator targetLocator, String nameOrId) {
815+
acc.append("beforeFrame ").append(nameOrId).append("\n");
816+
}
817+
818+
@Override
819+
public void afterFrame(
820+
WebDriver.TargetLocator targetLocator, String nameOrId, WebDriver driver) {
821+
acc.append("afterFrame ").append(nameOrId).append("\n");
822+
}
823+
824+
@Override
825+
public void beforeFrame(WebDriver.TargetLocator targetLocator, WebElement frameElement) {
826+
acc.append("beforeFrame ").append(frameElement).append("\n");
827+
}
828+
829+
@Override
830+
public void afterFrame(
831+
WebDriver.TargetLocator targetLocator, WebElement frameElement, WebDriver driver) {
832+
acc.append("afterFrame ").append(frameElement).append("\n");
833+
}
834+
835+
@Override
836+
public void beforeParentFrame(WebDriver.TargetLocator targetLocator) {
837+
acc.append("beforeParentFrame").append("\n");
838+
}
839+
840+
@Override
841+
public void afterParentFrame(WebDriver.TargetLocator targetLocator, WebDriver driver) {
842+
acc.append("afterParentFrame").append("\n");
843+
}
844+
801845
@Override
802846
public void beforeWindow(WebDriver.TargetLocator targetLocator, String windowName) {
803847
acc.append("beforeWindow ").append(windowName).append("\n");
@@ -808,11 +852,62 @@ public void afterWindow(
808852
WebDriver.TargetLocator targetLocator, String windowName, WebDriver driver) {
809853
acc.append("afterWindow ").append(windowName).append("\n");
810854
}
855+
856+
@Override
857+
public void beforeNewWindow(WebDriver.TargetLocator targetLocator, WindowType typeHint) {
858+
acc.append("beforeNewWindow ").append(typeHint).append("\n");
859+
}
860+
861+
@Override
862+
public void afterNewWindow(
863+
WebDriver.TargetLocator targetLocator, WindowType typeHint, WebDriver driver) {
864+
acc.append("afterNewWindow ").append(typeHint).append("\n");
865+
}
866+
867+
@Override
868+
public void beforeDefaultContent(WebDriver.TargetLocator targetLocator) {
869+
acc.append("beforeDefaultContent").append("\n");
870+
}
871+
872+
@Override
873+
public void afterDefaultContent(WebDriver.TargetLocator targetLocator, WebDriver driver) {
874+
acc.append("afterDefaultContent").append("\n");
875+
}
876+
877+
@Override
878+
public void beforeActiveElement(WebDriver.TargetLocator targetLocator) {
879+
acc.append("beforeActiveElement").append("\n");
880+
}
881+
882+
@Override
883+
public void afterActiveElement(WebDriver.TargetLocator targetLocator, WebDriver driver) {
884+
acc.append("afterActiveElement").append("\n");
885+
}
886+
887+
@Override
888+
public void beforeAlert(WebDriver.TargetLocator targetLocator) {
889+
acc.append("beforeAlert").append("\n");
890+
}
891+
892+
@Override
893+
public void afterAlert(WebDriver.TargetLocator targetLocator, Alert alert) {
894+
acc.append("afterAlert").append("\n");
895+
}
811896
};
812897

813898
WebDriver decorated = new EventFiringDecorator<>(listener).decorate(driver);
814-
815-
decorated.switchTo().window("windowName");
899+
WebDriver.TargetLocator decoratedTargetLocator = decorated.switchTo();
900+
901+
decoratedTargetLocator.frame(3);
902+
decoratedTargetLocator.frame("frame-id");
903+
WebElement frameElement = mock();
904+
decoratedTargetLocator.frame(frameElement);
905+
decoratedTargetLocator.parentFrame();
906+
decoratedTargetLocator.window("windowName");
907+
decoratedTargetLocator.newWindow(WindowType.TAB);
908+
decoratedTargetLocator.defaultContent();
909+
decoratedTargetLocator.activeElement();
910+
decoratedTargetLocator.alert();
816911

817912
assertThat(listener.acc.toString().trim())
818913
.isEqualTo(
@@ -822,12 +917,60 @@ public void afterWindow(
822917
"beforeAnyWebDriverCall switchTo",
823918
"afterAnyWebDriverCall switchTo",
824919
"afterAnyCall switchTo",
920+
"beforeAnyCall frame",
921+
"beforeAnyTargetLocatorCall frame",
922+
"beforeFrame 3",
923+
"afterFrame 3",
924+
"afterAnyTargetLocatorCall frame",
925+
"afterAnyCall frame",
926+
"beforeAnyCall frame",
927+
"beforeAnyTargetLocatorCall frame",
928+
"beforeFrame frame-id",
929+
"afterFrame frame-id",
930+
"afterAnyTargetLocatorCall frame",
931+
"afterAnyCall frame",
932+
"beforeAnyCall frame",
933+
"beforeAnyTargetLocatorCall frame",
934+
"beforeFrame " + frameElement,
935+
"afterFrame " + frameElement,
936+
"afterAnyTargetLocatorCall frame",
937+
"afterAnyCall frame",
938+
"beforeAnyCall parentFrame",
939+
"beforeAnyTargetLocatorCall parentFrame",
940+
"beforeParentFrame",
941+
"afterParentFrame",
942+
"afterAnyTargetLocatorCall parentFrame",
943+
"afterAnyCall parentFrame",
825944
"beforeAnyCall window",
826945
"beforeAnyTargetLocatorCall window",
827946
"beforeWindow windowName",
828947
"afterWindow windowName",
829948
"afterAnyTargetLocatorCall window",
830-
"afterAnyCall window"));
949+
"afterAnyCall window",
950+
"beforeAnyCall newWindow",
951+
"beforeAnyTargetLocatorCall newWindow",
952+
"beforeNewWindow tab",
953+
"afterNewWindow tab",
954+
"afterAnyTargetLocatorCall newWindow",
955+
"afterAnyCall newWindow",
956+
"beforeAnyCall defaultContent",
957+
"beforeAnyTargetLocatorCall defaultContent",
958+
"beforeDefaultContent",
959+
"afterDefaultContent",
960+
"afterAnyTargetLocatorCall defaultContent",
961+
"afterAnyCall defaultContent",
962+
"beforeAnyCall activeElement",
963+
"beforeAnyTargetLocatorCall activeElement",
964+
"beforeActiveElement",
965+
"afterActiveElement",
966+
"afterAnyTargetLocatorCall activeElement",
967+
"afterAnyCall activeElement",
968+
"beforeAnyCall alert",
969+
"beforeAnyTargetLocatorCall alert",
970+
"beforeAlert",
971+
"afterAlert",
972+
"afterAnyTargetLocatorCall alert",
973+
"afterAnyCall alert"));
831974
}
832975

833976
@Test
@@ -857,7 +1000,7 @@ public void beforeAnyWebDriverCall(WebDriver driver, Method method, Object[] arg
8571000
}
8581001
};
8591002

860-
WebDriver decorated = new EventFiringDecorator(listener).decorate(driver);
1003+
WebDriver decorated = new EventFiringDecorator<>(listener).decorate(driver);
8611004

8621005
assertThatNoException().isThrownBy(decorated::getWindowHandle);
8631006
}

0 commit comments

Comments
 (0)