Skip to content

Commit 4fee61a

Browse files
authored
test: unflake TestHar.shouldAttachContent (#961)
1 parent efb281e commit 4fee61a

File tree

1 file changed

+16
-8
lines changed
  • playwright/src/test/java/com/microsoft/playwright

1 file changed

+16
-8
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
package com.microsoft.playwright;
1818

19-
import com.google.gson.Gson;
20-
import com.google.gson.JsonArray;
21-
import com.google.gson.JsonElement;
22-
import com.google.gson.JsonObject;
19+
import com.google.gson.*;
2320
import com.microsoft.playwright.options.HarContentPolicy;
2421
import org.junit.jupiter.api.AfterEach;
2522
import org.junit.jupiter.api.BeforeEach;
@@ -281,7 +278,7 @@ void shouldAttachContent(@TempDir Path tmpDir) throws IOException {
281278

282279
JsonArray entries = log.getAsJsonArray("entries");
283280
{
284-
JsonObject content = entries.get(0).getAsJsonObject()
281+
JsonObject content = firstEntryFor(entries, "har.html")
285282
.getAsJsonObject("response")
286283
.getAsJsonObject("content");
287284
assertFalse(content.has("encoding"));
@@ -291,7 +288,8 @@ void shouldAttachContent(@TempDir Path tmpDir) throws IOException {
291288
assertEquals(0, content.get("compression").getAsInt());
292289
}
293290
{
294-
JsonObject content = entries.get(1).getAsJsonObject()
291+
// TODO: figure out why there is more than one entry in Firefox.
292+
JsonObject content = firstEntryFor(entries, "one-style.css")
295293
.getAsJsonObject("response")
296294
.getAsJsonObject("content");
297295
assertFalse(content.has("encoding"));
@@ -301,7 +299,7 @@ void shouldAttachContent(@TempDir Path tmpDir) throws IOException {
301299
assertEquals(0, content.get("compression").getAsInt());
302300
}
303301
{
304-
JsonObject content = entries.get(2).getAsJsonObject()
302+
JsonObject content = firstEntryFor(entries, "pptr.png")
305303
.getAsJsonObject("response")
306304
.getAsJsonObject("content");
307305
assertFalse(content.has("encoding"));
@@ -312,9 +310,19 @@ void shouldAttachContent(@TempDir Path tmpDir) throws IOException {
312310
}
313311
assertTrue(new String(zip.get("75841480e2606c03389077304342fac2c58ccb1b.html"), StandardCharsets.UTF_8).contains("HAR Page"));
314312
assertTrue(new String(zip.get("79f739d7bc88e80f55b9891a22bf13a2b4e18adb.css"), StandardCharsets.UTF_8).contains("pink"));
315-
assertEquals(entries.get(2).getAsJsonObject()
313+
assertEquals(firstEntryFor(entries, "pptr.png")
316314
.getAsJsonObject("response")
317315
.getAsJsonObject("content")
318316
.get("size").getAsInt(), zip.get("a4c3a18f0bb83f5d9fe7ce561e065c36205762fa.png").length);
319317
}
318+
private static JsonObject firstEntryFor(JsonArray entries, String name) {
319+
for (int i = 0; i < entries.size(); i++) {
320+
JsonObject entry = entries.get(i).getAsJsonObject();
321+
String url = entry.getAsJsonObject("request").get("url").getAsString();
322+
if (url.endsWith(name)) {
323+
return entry;
324+
}
325+
}
326+
return null;
327+
}
320328
}

0 commit comments

Comments
 (0)