17
17
18
18
package org .openqa .selenium .devtools ;
19
19
20
+ import org .openqa .selenium .WebDriver ;
20
21
import org .openqa .selenium .devtools .idealized .Domains ;
21
22
import org .openqa .selenium .devtools .idealized .target .model .SessionID ;
22
23
import org .openqa .selenium .devtools .idealized .target .model .TargetID ;
32
33
import java .util .concurrent .TimeoutException ;
33
34
import java .util .function .Consumer ;
34
35
import java .util .function .Function ;
36
+ import java .util .logging .Level ;
37
+ import java .util .logging .Logger ;
35
38
36
39
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
37
40
38
41
public class DevTools implements Closeable {
42
+ private static final Logger log = Logger .getLogger (DevTools .class .getName ());
39
43
40
44
private final Domains protocol ;
41
45
private final Duration timeout = Duration .ofSeconds (10 );
@@ -85,22 +89,27 @@ public void clearListeners() {
85
89
}
86
90
87
91
public void createSessionIfThereIsNotOne () {
92
+ createSessionIfThereIsNotOne (null );
93
+ }
94
+
95
+ public void createSessionIfThereIsNotOne (String windowHandle ) {
88
96
if (cdpSession == null ) {
89
- createSession ();
97
+ createSession (windowHandle );
90
98
}
91
99
}
92
100
93
101
public void createSession () {
94
- // Figure out the targets.
95
- List < TargetInfo > infos = connection . sendAndWait ( cdpSession , getDomains (). target (). getTargets (), timeout );
102
+ createSession ( null );
103
+ }
96
104
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 );
104
113
105
114
// Start the session.
106
115
cdpSession = connection
@@ -114,24 +123,38 @@ public void createSession() {
114
123
// Clear the existing logs
115
124
connection .send (cdpSession , getDomains ().log ().clear ())
116
125
.exceptionally (t -> {
117
- t . printStackTrace ( );
126
+ log . log ( Level . SEVERE , t . getMessage (), t );
118
127
return null ;
119
128
})
120
129
).get (timeout .toMillis (), MILLISECONDS );
121
130
} catch (InterruptedException e ) {
122
131
Thread .currentThread ().interrupt ();
123
132
throw new IllegalStateException ("Thread has been interrupted" , e );
124
133
} 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 ));
130
135
} catch (TimeoutException e ) {
131
136
throw new org .openqa .selenium .TimeoutException (e );
132
137
}
133
138
}
134
139
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
+
135
158
public SessionID getCdpSession () {
136
159
return cdpSession ;
137
160
}
0 commit comments