Skip to content

Commit b9e5759

Browse files
committed
more
1 parent 1ff3029 commit b9e5759

File tree

7 files changed

+54
-20
lines changed

7 files changed

+54
-20
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import static java.util.Arrays.asList;
4343

4444
class BrowserContextImpl extends ChannelOwner implements BrowserContext {
45-
private final BrowserImpl browser;
45+
protected BrowserImpl browser;
4646
private final TracingImpl tracing;
4747
private final APIRequestContextImpl request;
4848
private final ClockImpl clock;
@@ -51,7 +51,7 @@ class BrowserContextImpl extends ChannelOwner implements BrowserContext {
5151

5252
final Router routes = new Router();
5353
final WebSocketRouter webSocketRoutes = new WebSocketRouter();
54-
private boolean closeWasCalled;
54+
private boolean closingOrClosed;
5555
private final WaitableEvent<EventType, ?> closePromise;
5656
final Map<String, BindingCallback> bindings = new HashMap<>();
5757
PageImpl ownerPage;
@@ -286,8 +286,8 @@ public List<Cookie> cookies(String url) {
286286
}
287287

288288
private void closeImpl(CloseOptions options) {
289-
if (!closeWasCalled) {
290-
closeWasCalled = true;
289+
if (!closingOrClosed) {
290+
closingOrClosed = true;
291291
if (options == null) {
292292
options = new CloseOptions();
293293
}
@@ -851,6 +851,7 @@ protected void handleEvent(String event, JsonObject params) {
851851
void didClose() {
852852
if (browser != null) {
853853
browser.contexts.remove(this);
854+
browser.browserType.playwright.sharedSelectors.contextsForSelectors.remove(this);
854855
}
855856
listeners.notify(EventType.CLOSE, this);
856857
}

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static com.microsoft.playwright.impl.Serialization.addHarUrlFilter;
3333
import static com.microsoft.playwright.impl.Serialization.gson;
3434
import static com.microsoft.playwright.impl.Utils.*;
35-
import static com.microsoft.playwright.impl.Utils.convertType;
3635

3736
class BrowserImpl extends ChannelOwner implements Browser {
3837
final Set<BrowserContextImpl> contexts = new HashSet<>();
@@ -41,7 +40,7 @@ class BrowserImpl extends ChannelOwner implements Browser {
4140
private boolean isConnected = true;
4241
BrowserTypeImpl browserType;
4342
BrowserType.LaunchOptions launchOptions;
44-
private Path tracePath;
43+
private Path tracesDir;
4544
String closeReason;
4645

4746
enum EventType {
@@ -241,7 +240,7 @@ private void startTracingImpl(Page page, StartTracingOptions options) {
241240
if (options == null) {
242241
options = new StartTracingOptions();
243242
}
244-
tracePath = options.path;
243+
tracesDir = options.path;
245244
JsonObject params = gson().toJsonTree(options).getAsJsonObject();
246245
if (page != null) {
247246
params.add("page", ((PageImpl) page).toProtocolRef());
@@ -259,14 +258,14 @@ private byte[] stopTracingImpl() {
259258
ArtifactImpl artifact = connection.getExistingObject(json.getAsJsonObject().getAsJsonObject("artifact").get("guid").getAsString());
260259
byte[] data = artifact.readAllBytes();
261260
artifact.delete();
262-
if (tracePath != null) {
261+
if (tracesDir != null) {
263262
try {
264-
Files.createDirectories(tracePath.getParent());
265-
Files.write(tracePath, data);
263+
Files.createDirectories(tracesDir.getParent());
264+
Files.write(tracesDir, data);
266265
} catch (IOException e) {
267266
throw new PlaywrightException("Failed to write trace file", e);
268267
} finally {
269-
tracePath = null;
268+
tracesDir = null;
270269
}
271270
}
272271
return data;
@@ -295,8 +294,13 @@ public String version() {
295294

296295
@Override
297296
void handleEvent(String event, JsonObject parameters) {
298-
if ("close".equals(event)) {
299-
didClose();
297+
switch (event) {
298+
case "context":
299+
didCreateContext(connection.getExistingObject(parameters.getAsJsonObject("context").get("guid").getAsString()));
300+
break;
301+
case "close":
302+
didClose();
303+
break;
300304
}
301305
}
302306

@@ -307,6 +311,29 @@ public CDPSession newBrowserCDPSession() {
307311
return connection.getExistingObject(result.getAsJsonObject("session").get("guid").getAsString());
308312
}
309313

314+
protected void connectToBrowserType(BrowserTypeImpl browserType, Path tracesDir){
315+
// Note: when using connect(), `browserType` is different from `this.parent`.
316+
// This is why browser type is not wired up in the constructor, and instead this separate method is called later on.
317+
this.browserType = browserType;
318+
this.tracesDir = tracesDir;
319+
320+
for (BrowserContextImpl context : contexts) {
321+
context.tracing().setTracesDir(tracesDir);
322+
browserType.playwright.sharedSelectors.contextsForSelectors.add(context);
323+
}
324+
}
325+
326+
private void didCreateContext(BrowserContextImpl context) {
327+
context.browser = this;
328+
contexts.add(context);
329+
// Note: when connecting to a browser, initial contexts arrive before `_browserType` is set,
330+
// and will be configured later in `ConnectToBrowserType`.
331+
if (browserType != null) {
332+
context.tracing().setTracesDir(tracesDir);
333+
browserType.playwright.sharedSelectors.contextsForSelectors.add(context);
334+
}
335+
}
336+
310337
private void didClose() {
311338
isConnected = false;
312339
listeners.notify(EventType.DISCONNECTED, this);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import static com.microsoft.playwright.impl.Utils.convertType;
3636

3737
class BrowserTypeImpl extends ChannelOwner implements BrowserType {
38+
protected PlaywrightImpl playwright;
39+
3840
BrowserTypeImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
3941
super(parent, type, guid, initializer);
4042
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,18 @@ public static PlaywrightImpl createImpl(CreateOptions options, boolean forceNewD
6262
private final BrowserTypeImpl firefox;
6363
private final BrowserTypeImpl webkit;
6464
private final APIRequestImpl apiRequest;
65-
private SharedSelectors sharedSelectors;
65+
protected SharedSelectors sharedSelectors;
6666

6767
PlaywrightImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
6868
super(parent, type, guid, initializer);
6969
chromium = parent.connection.getExistingObject(initializer.getAsJsonObject("chromium").get("guid").getAsString());
7070
firefox = parent.connection.getExistingObject(initializer.getAsJsonObject("firefox").get("guid").getAsString());
7171
webkit = parent.connection.getExistingObject(initializer.getAsJsonObject("webkit").get("guid").getAsString());
7272

73+
chromium.playwright = this;
74+
firefox.playwright = this;
75+
webkit.playwright = this;
76+
7377
sharedSelectors = new SharedSelectors();
7478
apiRequest = new APIRequestImpl(this);
7579
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import static java.nio.charset.StandardCharsets.UTF_8;
3030

3131
public class SharedSelectors extends LoggingSupport implements Selectors {
32-
private final List<BrowserContextImpl> contextsForSelectors = new ArrayList<>();
33-
private final List<JsonObject> selectorEngines = new ArrayList<>(); // FIXME: do we need this? is it ever read?
32+
protected final List<BrowserContextImpl> contextsForSelectors = new ArrayList<>();
33+
protected final List<JsonObject> selectorEngines = new ArrayList<>(); // FIXME: do we need this? is it ever read?
3434

3535
String testIdAttributeName = "data-testid";
3636

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ void defaultTimeoutHasTextFail() {
10701070
Locator locator = page.locator("div");
10711071
PlaywrightAssertions.setDefaultAssertionTimeout(1000);
10721072
AssertionFailedError exception = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasText("foo"));
1073-
assertTrue(exception.getMessage().contains("Locator.expect with timeout 1000ms"), exception.getMessage());
1073+
assertTrue(exception.getMessage().contains("Expect \"hasText\" with timeout 1000ms"), exception.getMessage());
10741074
// Restore default.
10751075
PlaywrightAssertions.setDefaultAssertionTimeout(5_000);
10761076
}
@@ -1119,7 +1119,7 @@ void containsClassFail() {
11191119
AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> {
11201120
assertThat(page.locator("div")).containsClass("does-not-exist", new ContainsClassOptions().setTimeout(1000));
11211121
});
1122-
assertTrue(e.getMessage().contains("Locator.expect with timeout 1000ms"), e.getMessage());
1122+
assertTrue(e.getMessage().contains("Expect \"containsClass\" with timeout 1000ms"), e.getMessage());
11231123
}
11241124

11251125
@Test
@@ -1137,6 +1137,6 @@ void containsClassFailWithArray() {
11371137
AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> {
11381138
assertThat(page.locator("div")).containsClass(asList("foo", "bar", "baz"), new ContainsClassOptions().setTimeout(1000));
11391139
});
1140-
assertTrue(e.getMessage().contains("Locator.expect with timeout 1000ms"), e.getMessage());
1140+
assertTrue(e.getMessage().contains("Expect \"containsClass\" with timeout 1000ms"), e.getMessage());
11411141
}
11421142
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,6 @@ void toBeEditableFailWithIndeterminateTrue() {
218218
AssertionFailedError e = assertThrows(AssertionFailedError.class, () ->
219219
assertThat(locator).isChecked(new LocatorAssertions.IsCheckedOptions().setIndeterminate(true).setTimeout(1000)));
220220
// TODO: should be "assertThat().isChecked() with timeout 1000ms"
221-
assertTrue(e.getMessage().contains("Locator.expect with timeout 1000ms"), e.getMessage());
221+
assertTrue(e.getMessage().contains("Expect \"isChecked\" with timeout 1000ms"), e.getMessage());
222222
}
223223
}

0 commit comments

Comments
 (0)