2222import com .microsoft .playwright .Browser ;
2323import com .microsoft .playwright .BrowserType ;
2424import com .microsoft .playwright .PlaywrightException ;
25- import com .microsoft .playwright .options .HarContentPolicy ;
2625
2726import java .io .IOException ;
2827import java .nio .file .Path ;
2928import java .nio .file .Paths ;
3029import java .util .function .Consumer ;
3130
32- import static com .microsoft .playwright .impl .Serialization .addHarUrlFilter ;
3331import static com .microsoft .playwright .impl .Serialization .gson ;
3432import static com .microsoft .playwright .impl .Utils .addToProtocol ;
3533import static com .microsoft .playwright .impl .Utils .convertType ;
@@ -103,9 +101,10 @@ private Browser connectImpl(String wsEndpoint, ConnectOptions options) {
103101 }
104102 throw new PlaywrightException ("Malformed endpoint. Did you use launchServer method?" );
105103 }
104+ playwright .sharedSelectors = this .playwright .sharedSelectors ;
106105 BrowserImpl browser = connection .getExistingObject (playwright .initializer .getAsJsonObject ("preLaunchedBrowser" ).get ("guid" ).getAsString ());
107106 browser .isConnectedOverWebSocket = true ;
108- browser .browserType = this ;
107+ browser .connectToBrowserType ( this , null ) ;
109108 Consumer <JsonPipe > connectionCloseListener = t -> browser .notifyRemoteClosed ();
110109 pipe .onClose (connectionCloseListener );
111110 browser .onDisconnected (b -> {
@@ -138,12 +137,7 @@ private Browser connectOverCDPImpl(String endpointURL, ConnectOverCDPOptions opt
138137 JsonObject json = sendMessage ("connectOverCDP" , params ).getAsJsonObject ();
139138
140139 BrowserImpl browser = connection .getExistingObject (json .getAsJsonObject ("browser" ).get ("guid" ).getAsString ());
141- browser .browserType = this ;
142- if (json .has ("defaultContext" )) {
143- String contextId = json .getAsJsonObject ("defaultContext" ).get ("guid" ).getAsString ();
144- BrowserContextImpl defaultContext = connection .getExistingObject (contextId );
145- browser .contexts .add (defaultContext );
146- }
140+ browser .connectToBrowserType (this , null );
147141 return browser ;
148142 }
149143
@@ -164,43 +158,6 @@ private BrowserContextImpl launchPersistentContextImpl(Path userDataDir, LaunchP
164158 // Make a copy so that we can nullify some fields below.
165159 options = convertType (options , LaunchPersistentContextOptions .class );
166160 }
167- JsonObject recordHar = null ;
168- Path recordHarPath = options .recordHarPath ;
169- HarContentPolicy harContentPolicy = null ;
170- if (options .recordHarPath != null ) {
171- recordHar = new JsonObject ();
172- recordHar .addProperty ("path" , options .recordHarPath .toString ());
173- if (options .recordHarContent != null ) {
174- harContentPolicy = options .recordHarContent ;
175- } else if (options .recordHarOmitContent != null && options .recordHarOmitContent ) {
176- harContentPolicy = HarContentPolicy .OMIT ;
177- }
178- if (harContentPolicy != null ) {
179- recordHar .addProperty ("content" , harContentPolicy .name ().toLowerCase ());
180- }
181- if (options .recordHarMode != null ) {
182- recordHar .addProperty ("mode" , options .recordHarMode .toString ().toLowerCase ());
183- }
184- addHarUrlFilter (recordHar , options .recordHarUrlFilter );
185- options .recordHarPath = null ;
186- options .recordHarMode = null ;
187- options .recordHarOmitContent = null ;
188- options .recordHarContent = null ;
189- options .recordHarUrlFilter = null ;
190- } else {
191- if (options .recordHarOmitContent != null ) {
192- throw new PlaywrightException ("recordHarOmitContent is set but recordHarPath is null" );
193- }
194- if (options .recordHarUrlFilter != null ) {
195- throw new PlaywrightException ("recordHarUrlFilter is set but recordHarPath is null" );
196- }
197- if (options .recordHarMode != null ) {
198- throw new PlaywrightException ("recordHarMode is set but recordHarPath is null" );
199- }
200- if (options .recordHarContent != null ) {
201- throw new PlaywrightException ("recordHarContent is set but recordHarPath is null" );
202- }
203- }
204161 options .timeout = TimeoutSettings .launchTimeout (options .timeout );
205162
206163 JsonObject params = gson ().toJsonTree (options ).getAsJsonObject ();
@@ -209,9 +166,6 @@ private BrowserContextImpl launchPersistentContextImpl(Path userDataDir, LaunchP
209166 userDataDir = cwd .resolve (userDataDir );
210167 }
211168 params .addProperty ("userDataDir" , userDataDir .toString ());
212- if (recordHar != null ) {
213- params .add ("recordHar" , recordHar );
214- }
215169 if (options .recordVideoDir != null ) {
216170 JsonObject recordVideo = new JsonObject ();
217171 recordVideo .addProperty ("dir" , options .recordVideoDir .toAbsolutePath ().toString ());
@@ -239,13 +193,25 @@ private BrowserContextImpl launchPersistentContextImpl(Path userDataDir, LaunchP
239193 if (options .acceptDownloads != null ) {
240194 params .addProperty ("acceptDownloads" , options .acceptDownloads ? "accept" : "deny" );
241195 }
196+ params .add ("selectorEngines" , gson ().toJsonTree (playwright .sharedSelectors .selectorEngines ));
197+ params .addProperty ("testIdAttributeName" , playwright .sharedSelectors .testIdAttributeName );
242198 JsonObject json = sendMessage ("launchPersistentContext" , params ).getAsJsonObject ();
199+ BrowserImpl browser = connection .getExistingObject (json .getAsJsonObject ("browser" ).get ("guid" ).getAsString ());
200+ browser .connectToBrowserType (this , options .tracesDir );
243201 BrowserContextImpl context = connection .getExistingObject (json .getAsJsonObject ("context" ).get ("guid" ).getAsString ());
244202 context .videosDir = options .recordVideoDir ;
245203 if (options .baseURL != null ) {
246204 context .setBaseUrl (options .baseURL );
247205 }
248- context .setRecordHar (recordHarPath , harContentPolicy );
206+
207+ Browser .NewContextOptions harOptions = new Browser .NewContextOptions ();
208+ harOptions .recordHarContent = options .recordHarContent ;
209+ harOptions .recordHarMode = options .recordHarMode ;
210+ harOptions .recordHarOmitContent = options .recordHarOmitContent ;
211+ harOptions .recordHarPath = options .recordHarPath ;
212+ harOptions .recordHarUrlFilter = options .recordHarUrlFilter ;
213+ context .initializeHarFromOptions (harOptions );
214+
249215 context .tracing ().setTracesDir (options .tracesDir );
250216 return context ;
251217 }
0 commit comments