Skip to content

Commit f2325cc

Browse files
committed
some more
1 parent b9e5759 commit f2325cc

File tree

5 files changed

+87
-110
lines changed

5 files changed

+87
-110
lines changed

playwright/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,6 @@ enum EventType {
9898

9999
BrowserContextImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
100100
super(parent, type, guid, initializer);
101-
if (parent instanceof BrowserImpl) {
102-
browser = (BrowserImpl) parent;
103-
} else {
104-
browser = null;
105-
}
106101
tracing = connection.getExistingObject(initializer.getAsJsonObject("tracing").get("guid").getAsString());
107102
request = connection.getExistingObject(initializer.getAsJsonObject("requestContext").get("guid").getAsString());
108103
request.timeoutSettings = timeoutSettings;
@@ -500,7 +495,7 @@ public void routeFromHAR(Path har, RouteFromHAROptions options) {
500495
options = new RouteFromHAROptions();
501496
}
502497
if (options.update != null && options.update) {
503-
recordIntoHar(null, har, options);
498+
recordIntoHar(null, har, options, null);
504499
return;
505500
}
506501
UrlMatcher matcher = UrlMatcher.forOneOf(baseUrl, options.url, this.connection.localUtils, false);
@@ -538,24 +533,41 @@ private void routeWebSocketImpl(UrlMatcher matcher, Consumer<WebSocketRoute> han
538533
});
539534
}
540535

541-
void recordIntoHar(PageImpl page, Path har, RouteFromHAROptions options) {
536+
private HarContentPolicy routeFromHarUpdateContentPolicyToHarContentPolicy(RouteFromHarUpdateContentPolicy contentPolicy) {
537+
switch (contentPolicy) {
538+
case ATTACH:
539+
return HarContentPolicy.ATTACH;
540+
case EMBED:
541+
return HarContentPolicy.EMBED;
542+
default:
543+
return null;
544+
}
545+
}
546+
547+
void recordIntoHar(PageImpl page, Path har, RouteFromHAROptions options, HarContentPolicy contentPolicy) {
548+
if (contentPolicy == null) {
549+
contentPolicy = routeFromHarUpdateContentPolicyToHarContentPolicy(options.updateContent);
550+
}
551+
if (contentPolicy == null) {
552+
contentPolicy = HarContentPolicy.ATTACH;
553+
}
554+
542555
JsonObject params = new JsonObject();
543556
if (page != null) {
544557
params.add("page", page.toProtocolRef());
545558
}
546-
JsonObject jsonOptions = new JsonObject();
547-
jsonOptions.addProperty("path", har.toAbsolutePath().toString());
548-
jsonOptions.addProperty("content", options.updateContent == null ?
549-
HarContentPolicy.ATTACH.name().toLowerCase() :
550-
options.updateContent.name().toLowerCase());
551-
jsonOptions.addProperty("mode", options.updateMode == null ?
559+
JsonObject recordHarArgs = new JsonObject();
560+
recordHarArgs.addProperty("zip", har.endsWith(".zip"));
561+
recordHarArgs.addProperty("content", contentPolicy.name().toLowerCase());
562+
recordHarArgs.addProperty("mode", options.updateMode == null ?
552563
HarMode.MINIMAL.name().toLowerCase() :
553564
options.updateMode.name().toLowerCase());
554-
addHarUrlFilter(jsonOptions, options.url);
555-
params.add("options", jsonOptions);
565+
addHarUrlFilter(recordHarArgs, options.url);
566+
567+
params.add("options", recordHarArgs);
556568
JsonObject json = sendMessage("harStart", params).getAsJsonObject();
557569
String harId = json.get("harId").getAsString();
558-
harRecorders.put(harId, new HarRecorder(har, HarContentPolicy.ATTACH));
570+
harRecorders.put(harId, new HarRecorder(har, contentPolicy));
559571
}
560572

561573
@Override
@@ -863,4 +875,29 @@ WritableStream createTempFile(String name, long lastModifiedMs) {
863875
JsonObject json = sendMessage("createTempFile", params).getAsJsonObject();
864876
return connection.getExistingObject(json.getAsJsonObject("writableStream").get("guid").getAsString());
865877
}
878+
879+
protected void initializeHarFromOptions(Browser.NewContextOptions options) {
880+
if (options.recordHarPath == null) {
881+
return;
882+
}
883+
884+
HarContentPolicy defaultPolicy = options.recordHarPath.endsWith(".zip") ? HarContentPolicy.ATTACH : HarContentPolicy.EMBED;
885+
HarContentPolicy contentPolicy = options.recordHarContent == null ? (options.recordHarOmitContent ? HarContentPolicy.OMIT : defaultPolicy) : options.recordHarContent;
886+
RouteFromHAROptions routeFromHAROptions = new RouteFromHAROptions();
887+
888+
if (options.recordHarUrlFilter instanceof String) {
889+
routeFromHAROptions.setUrl((String) options.recordHarUrlFilter);
890+
} else if (options.recordHarUrlFilter instanceof Pattern) {
891+
routeFromHAROptions.setUrl((Pattern) options.recordHarUrlFilter);
892+
}
893+
894+
if (options.recordHarMode != null) {
895+
routeFromHAROptions.updateMode = options.recordHarMode;
896+
} else {
897+
routeFromHAROptions.updateMode = HarMode.FULL;
898+
}
899+
routeFromHAROptions.url = options.recordHarUrlFilter;
900+
901+
recordIntoHar(null, options.recordHarPath, routeFromHAROptions, contentPolicy);
902+
}
866903
}

playwright/src/main/java/com/microsoft/playwright/impl/BrowserImpl.java

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.gson.JsonElement;
2121
import com.google.gson.JsonObject;
2222
import com.microsoft.playwright.*;
23-
import com.microsoft.playwright.options.HarContentPolicy;
2423

2524
import java.io.IOException;
2625
import java.nio.charset.StandardCharsets;
@@ -140,51 +139,10 @@ private BrowserContextImpl newContextImpl(NewContextOptions options) {
140139
storageState = new Gson().fromJson(options.storageState, JsonObject.class);
141140
options.storageState = null;
142141
}
143-
JsonObject recordHar = null;
144-
Path recordHarPath = options.recordHarPath;
145-
HarContentPolicy harContentPolicy = null;
146-
if (options.recordHarPath != null) {
147-
recordHar = new JsonObject();
148-
recordHar.addProperty("path", options.recordHarPath.toString());
149-
if (options.recordHarContent != null) {
150-
harContentPolicy = options.recordHarContent;
151-
} else if (options.recordHarOmitContent != null && options.recordHarOmitContent) {
152-
harContentPolicy = HarContentPolicy.OMIT;
153-
}
154-
if (harContentPolicy != null) {
155-
recordHar.addProperty("content", harContentPolicy.name().toLowerCase());
156-
}
157-
if (options.recordHarMode != null) {
158-
recordHar.addProperty("mode", options.recordHarMode.name().toLowerCase());
159-
}
160-
addHarUrlFilter(recordHar, options.recordHarUrlFilter);
161-
options.recordHarPath = null;
162-
options.recordHarMode = null;
163-
options.recordHarOmitContent = null;
164-
options.recordHarContent = null;
165-
options.recordHarUrlFilter = null;
166-
} else {
167-
if (options.recordHarOmitContent != null) {
168-
throw new PlaywrightException("recordHarOmitContent is set but recordHarPath is null");
169-
}
170-
if (options.recordHarUrlFilter != null) {
171-
throw new PlaywrightException("recordHarUrlFilter is set but recordHarPath is null");
172-
}
173-
if (options.recordHarMode != null) {
174-
throw new PlaywrightException("recordHarMode is set but recordHarPath is null");
175-
}
176-
if (options.recordHarContent != null) {
177-
throw new PlaywrightException("recordHarContent is set but recordHarPath is null");
178-
}
179-
}
180-
181142
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
182143
if (storageState != null) {
183144
params.add("storageState", storageState);
184145
}
185-
if (recordHar != null) {
186-
params.add("recordHar", recordHar);
187-
}
188146
if (options.recordVideoDir != null) {
189147
JsonObject recordVideo = new JsonObject();
190148
recordVideo.addProperty("dir", options.recordVideoDir.toAbsolutePath().toString());
@@ -212,13 +170,15 @@ private BrowserContextImpl newContextImpl(NewContextOptions options) {
212170
if (options.acceptDownloads != null) {
213171
params.addProperty("acceptDownloads", options.acceptDownloads ? "accept" : "deny");
214172
}
173+
params.add("selectorEngines", gson().toJsonTree(browserType.playwright.sharedSelectors.selectorEngines));
174+
params.addProperty("testIdAttributeName", browserType.playwright.sharedSelectors.testIdAttributeName);
215175
JsonElement result = sendMessage("newContext", params);
216176
BrowserContextImpl context = connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("context").get("guid").getAsString());
217177
context.videosDir = options.recordVideoDir;
218178
if (options.baseURL != null) {
219179
context.setBaseUrl(options.baseURL);
220180
}
221-
context.setRecordHar(recordHarPath, harContentPolicy);
181+
context.initializeHarFromOptions(options);
222182
if (launchOptions != null) {
223183
context.tracing().setTracesDir(launchOptions.tracesDir);
224184
}

playwright/src/main/java/com/microsoft/playwright/impl/BrowserTypeImpl.java

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
import com.microsoft.playwright.Browser;
2323
import com.microsoft.playwright.BrowserType;
2424
import com.microsoft.playwright.PlaywrightException;
25-
import com.microsoft.playwright.options.HarContentPolicy;
2625

2726
import java.io.IOException;
2827
import java.nio.file.Path;
2928
import java.nio.file.Paths;
3029
import java.util.function.Consumer;
3130

32-
import static com.microsoft.playwright.impl.Serialization.addHarUrlFilter;
3331
import static com.microsoft.playwright.impl.Serialization.gson;
3432
import static com.microsoft.playwright.impl.Utils.addToProtocol;
3533
import 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
}

playwright/src/main/java/com/microsoft/playwright/impl/PageImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ public void routeFromHAR(Path har, RouteFromHAROptions options) {
11271127
options = new RouteFromHAROptions();
11281128
}
11291129
if (options.update != null && options.update) {
1130-
browserContext.recordIntoHar(this, har, convertType(options, BrowserContext.RouteFromHAROptions.class));
1130+
browserContext.recordIntoHar(this, har, convertType(options, BrowserContext.RouteFromHAROptions.class), null);
11311131
return;
11321132
}
11331133
UrlMatcher matcher = UrlMatcher.forOneOf(browserContext.baseUrl, options.url, this.connection.localUtils, false);

playwright/src/test/java/com/microsoft/playwright/TestDefaultBrowserContext2.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,18 @@ void shouldAcceptRelativeUserDataDir(@TempDir Path tmpDir) throws Exception {
309309
assertTrue(Files.list(userDataDir).count() > 0);
310310
context.close();
311311
}
312+
313+
@Test
314+
void shouldExposeBrowser() {
315+
Page page = launchPersistent();
316+
Browser browser = page.context().browser();
317+
assertNotNull(browser);
318+
Page page2 = browser.newPage();
319+
page2.navigate("data:text/html,<html><title>Title</title></html>");
320+
assertEquals("Title", page2.title());
321+
browser.close();
322+
assertEquals(0, page.context().pages().size());
323+
// Next line should not throw.
324+
page.context().close();
325+
}
312326
}

0 commit comments

Comments
 (0)