Skip to content

Commit 4ad0533

Browse files
asolntsevdiemol
andauthored
#10810 pick the right target using given window handle (#10811)
* #10810 pick the right target using given window handle * #10810 leave parameterless methods createSession() and createSessionIfThereIsNotOne() for backward compatibility * #10810 log the exception properly * #10810 split code to smaller methods Co-authored-by: Diego Molina <diemol@users.noreply.github.com>
1 parent de0a144 commit 4ad0533

File tree

6 files changed

+45
-22
lines changed

6 files changed

+45
-22
lines changed

java/src/org/openqa/selenium/chromium/ChromiumDriver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void register(Predicate<URI> whenThisMatches, Supplier<Credentials> useTh
164164
Require.nonNull("Check to use to see how we should authenticate", whenThisMatches);
165165
Require.nonNull("Credentials to use when authenticating", useTheseCredentials);
166166

167-
getDevTools().createSessionIfThereIsNotOne();
167+
getDevTools().createSessionIfThereIsNotOne(getWindowHandle());
168168
getDevTools().getDomains().network().addAuthHandler(whenThisMatches, useTheseCredentials);
169169
}
170170

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

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.openqa.selenium.devtools;
1919

20+
import org.openqa.selenium.WebDriver;
2021
import org.openqa.selenium.devtools.idealized.Domains;
2122
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
2223
import org.openqa.selenium.devtools.idealized.target.model.TargetID;
@@ -32,10 +33,13 @@
3233
import java.util.concurrent.TimeoutException;
3334
import java.util.function.Consumer;
3435
import java.util.function.Function;
36+
import java.util.logging.Level;
37+
import java.util.logging.Logger;
3538

3639
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3740

3841
public class DevTools implements Closeable {
42+
private static final Logger log = Logger.getLogger(DevTools.class.getName());
3943

4044
private final Domains protocol;
4145
private final Duration timeout = Duration.ofSeconds(10);
@@ -85,22 +89,27 @@ public void clearListeners() {
8589
}
8690

8791
public void createSessionIfThereIsNotOne() {
92+
createSessionIfThereIsNotOne(null);
93+
}
94+
95+
public void createSessionIfThereIsNotOne(String windowHandle) {
8896
if (cdpSession == null) {
89-
createSession();
97+
createSession(windowHandle);
9098
}
9199
}
92100

93101
public void createSession() {
94-
// Figure out the targets.
95-
List<TargetInfo> infos = connection.sendAndWait(cdpSession, getDomains().target().getTargets(), timeout);
102+
createSession(null);
103+
}
96104

97-
// Grab the first "page" type, and glom on to that.
98-
// TODO: Find out which one might be the current one
99-
TargetID targetId = infos.stream()
100-
.filter(info -> "page".equals(info.getType()))
101-
.map(TargetInfo::getTargetId)
102-
.findAny()
103-
.orElseThrow(() -> new DevToolsException("Unable to find target id of a page"));
105+
/**
106+
* Create CDP session on given window/tab (aka target).
107+
* If windowHandle is null, then _some_ target will be selected. It might be a problem only if you have multiple windows/tabs opened.
108+
*
109+
* @param windowHandle result of {@link WebDriver#getWindowHandle()}, optional.
110+
*/
111+
public void createSession(String windowHandle) {
112+
TargetID targetId = findTarget(windowHandle);
104113

105114
// Start the session.
106115
cdpSession = connection
@@ -114,24 +123,38 @@ public void createSession() {
114123
// Clear the existing logs
115124
connection.send(cdpSession, getDomains().log().clear())
116125
.exceptionally(t -> {
117-
t.printStackTrace();
126+
log.log(Level.SEVERE, t.getMessage(), t);
118127
return null;
119128
})
120129
).get(timeout.toMillis(), MILLISECONDS);
121130
} catch (InterruptedException e) {
122131
Thread.currentThread().interrupt();
123132
throw new IllegalStateException("Thread has been interrupted", e);
124133
} catch (ExecutionException e) {
125-
Throwable cause = e;
126-
if (e.getCause() != null) {
127-
cause = e.getCause();
128-
}
129-
throw new DevToolsException(cause);
134+
throw new DevToolsException(unwrapCause(e));
130135
} catch (TimeoutException e) {
131136
throw new org.openqa.selenium.TimeoutException(e);
132137
}
133138
}
134139

140+
private TargetID findTarget(String windowHandle) {
141+
// Figure out the targets.
142+
List<TargetInfo> infos = connection.sendAndWait(cdpSession, getDomains().target().getTargets(), timeout);
143+
144+
// Grab the first "page" type, and glom on to that.
145+
// Find out which one might be the current one (using given window handle like "CDwindow-24426957AC62D8BC83E58C184C38AF2D")
146+
return infos.stream()
147+
.filter(info -> "page".equals(info.getType()))
148+
.map(TargetInfo::getTargetId)
149+
.filter(id -> windowHandle == null || windowHandle.contains(id.toString()))
150+
.findAny()
151+
.orElseThrow(() -> new DevToolsException("Unable to find target id of a page"));
152+
}
153+
154+
private Throwable unwrapCause(ExecutionException e) {
155+
return e.getCause() != null ? e.getCause() : e;
156+
}
157+
135158
public SessionID getCdpSession() {
136159
return cdpSession;
137160
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public NetworkInterceptor(WebDriver driver, Filter filter) {
9191
}
9292

9393
this.tools = ((HasDevTools) driver).getDevTools();
94-
tools.createSessionIfThereIsNotOne();
94+
tools.createSessionIfThereIsNotOne(driver.getWindowHandle());
9595

9696
tools.getDomains().network().interceptTrafficWith(filter);
9797
}

java/src/org/openqa/selenium/devtools/events/CdpEventTypes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void initializeListener(WebDriver webDriver) {
5959
Require.precondition(webDriver instanceof HasDevTools, "Loggable must implement HasDevTools");
6060

6161
DevTools tools = ((HasDevTools) webDriver).getDevTools();
62-
tools.createSessionIfThereIsNotOne();
62+
tools.createSessionIfThereIsNotOne(webDriver.getWindowHandle());
6363

6464
tools.getDomains().events().addConsoleListener(handler);
6565
}
@@ -91,7 +91,7 @@ public void initializeListener(WebDriver driver) {
9191
Require.precondition(driver instanceof HasDevTools, "Loggable must implement HasDevTools");
9292

9393
DevTools tools = ((HasDevTools) driver).getDevTools();
94-
tools.createSessionIfThereIsNotOne();
94+
tools.createSessionIfThereIsNotOne(driver.getWindowHandle());
9595

9696
tools.getDomains().javascript().pin("__webdriver_attribute", script);
9797

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void setUp() {
3535
assumeThat(isFirefoxVersionOlderThan(87, driver)).isFalse();
3636

3737
devTools = ((HasDevTools) driver).getDevTools();
38-
devTools.createSessionIfThereIsNotOne();
38+
devTools.createSessionIfThereIsNotOne(driver.getWindowHandle());
3939

4040
try {
4141
devTools.clearListeners();

java/test/org/openqa/selenium/grid/router/DistributedCdpTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void ensureBasicFunctionality() {
7979
driver = new Augmenter().augment(driver);
8080

8181
try (DevTools devTools = ((HasDevTools) driver).getDevTools()) {
82-
devTools.createSessionIfThereIsNotOne();
82+
devTools.createSessionIfThereIsNotOne(driver.getWindowHandle());
8383
Network<?, ?> network = devTools.getDomains().network();
8484
network.setUserAgent("Cheese-Browser 4000");
8585

0 commit comments

Comments
 (0)