Skip to content

Commit c138787

Browse files
authored
[java][cdp] Ensure child session is created under browser session (SeleniumHQ#11475)
Related to SeleniumHQ#11437
1 parent 96f6f4a commit c138787

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

java/src/org/openqa/selenium/devtools/DevTools.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ public void disconnectSession() {
6565
if (cdpSession != null) {
6666
SessionID id = cdpSession;
6767
cdpSession = null;
68-
connection.sendAndWait(
69-
cdpSession, getDomains().target().detachFromTarget(Optional.of(id), Optional.empty()), timeout);
68+
try {
69+
connection.sendAndWait(
70+
cdpSession, getDomains().target().detachFromTarget(Optional.of(id), Optional.empty()),
71+
timeout);
72+
} catch (Exception e) {
73+
// Exceptions should not prevent closing the connection and the web driver
74+
log.warning("Exception while detaching from target: " + e.getMessage());
75+
}
7076
}
7177
}
7278

@@ -114,9 +120,14 @@ public void createSession() {
114120
public void createSession(String windowHandle) {
115121
TargetID targetId = findTarget(windowHandle);
116122

117-
// Start the session.
123+
// Starts the session
124+
// CDP creates a parent browser session when websocket connection is made
125+
// Create session that is child of parent browser session and not child of already existing child page session
126+
// Passing null for session id helps achieve that
127+
// Child of already existing child page session throws an error when detaching from the target
128+
// CDP allows attaching to child of child session but not detaching. Maybe it does not keep track of it.
118129
cdpSession = connection
119-
.sendAndWait(cdpSession, getDomains().target().attachToTarget(targetId), timeout);
130+
.sendAndWait(null, getDomains().target().attachToTarget(targetId), timeout);
120131

121132
try {
122133
// We can do all of these in parallel, and we don't care about the result.

java/test/org/openqa/selenium/devtools/WindowSwitchingTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
2222

2323
import org.junit.jupiter.api.Test;
24+
import org.openqa.selenium.By;
2425
import org.openqa.selenium.WebDriver;
2526
import org.openqa.selenium.WindowType;
2627
import org.openqa.selenium.devtools.idealized.target.model.TargetInfo;
@@ -53,4 +54,46 @@ public void shouldBeAbleToSwitchWindowsAndCloseTheOriginal() {
5354

5455
assertThat(updatedTargets).isNotEqualTo(originalTargets);
5556
}
57+
58+
@Test
59+
@Ignore(FIREFOX)
60+
public void shouldBeAbleToCreateSessionForANewTab() {
61+
WebDriver driver = new Augmenter().augment(this.driver);
62+
63+
DevTools devTools = ((HasDevTools)driver).getDevTools();
64+
devTools.createSession();
65+
addConsoleLogListener(devTools);
66+
67+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
68+
69+
List<TargetInfo> originalTargets = devTools.send(devTools.getDomains().target().getTargets());
70+
driver.findElement(By.id("consoleLog")).click();
71+
72+
String originalWindowHandle = driver.getWindowHandle();
73+
74+
String windowHandle = driver.switchTo().newWindow(WindowType.TAB).getWindowHandle();
75+
76+
devTools.createSession(windowHandle);
77+
addConsoleLogListener(devTools);
78+
79+
driver.get("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html");
80+
driver.findElement(By.id("consoleLog")).click();
81+
82+
List<TargetInfo> updatedTargets = this.devTools.send(this.devTools.getDomains().target().getTargets());
83+
84+
assertThat(updatedTargets).isNotEqualTo(originalTargets);
85+
driver.close();
86+
87+
List<TargetInfo> leftoverTargets = devTools.send(devTools.getDomains().target().getTargets());
88+
89+
assertThat(leftoverTargets.get(0).getTargetId().toString()).isEqualTo(originalTargets.get(0).getTargetId().toString());
90+
91+
driver.switchTo().window(originalWindowHandle);
92+
}
93+
94+
public static void addConsoleLogListener(DevTools devTools) {
95+
devTools.getDomains().events().addConsoleListener(consoleEvent -> {
96+
assertThat(consoleEvent.getMessages()).contains("Hello, world!");
97+
});
98+
}
5699
}

0 commit comments

Comments
 (0)