From 1dca05d57adabf425e00b157ec80fdf50860cc77 Mon Sep 17 00:00:00 2001 From: Heramb Joshi <62691524+hermya@users.noreply.github.com> Date: Fri, 15 Nov 2024 09:48:53 -0600 Subject: [PATCH] Fix flaky test-cases in HtmlFormatterTest (#2939) Co-authored-by: M.P. Korstanje --- .../core/plugin/HtmlFormatterTest.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/cucumber-core/src/test/java/io/cucumber/core/plugin/HtmlFormatterTest.java b/cucumber-core/src/test/java/io/cucumber/core/plugin/HtmlFormatterTest.java index 1e5132b263..aa6ddf5fb0 100644 --- a/cucumber-core/src/test/java/io/cucumber/core/plugin/HtmlFormatterTest.java +++ b/cucumber-core/src/test/java/io/cucumber/core/plugin/HtmlFormatterTest.java @@ -18,10 +18,13 @@ import java.time.Clock; import java.util.Collections; import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; -import static io.cucumber.core.plugin.Bytes.bytes; -import static org.hamcrest.CoreMatchers.containsString; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.hamcrest.MatcherAssert.assertThat; +import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; +import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT; class HtmlFormatterTest { @@ -38,11 +41,11 @@ void writes_index_html() throws Throwable { TestRunFinished testRunFinished = new TestRunFinished(null, true, new Timestamp(15L, 0L), null); bus.send(Envelope.of(testRunFinished)); - assertThat(bytes, bytes(containsString("" + - "window.CUCUMBER_MESSAGES = [" + - "{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}," + - "{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}" + - "];\n"))); + assertEquals("[" + + "{\"testRunStarted\":{\"timestamp\":{\"nanos\":0,\"seconds\":10}}}," + + "{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"nanos\":0,\"seconds\":15}}}" + + "]", + extractCucumberMessages(bytes), STRICT); } @Test @@ -86,11 +89,17 @@ void ignores_step_definitions() throws Throwable { null); bus.send(Envelope.of(testRunFinished)); - assertThat(bytes, bytes(containsString("" + - "window.CUCUMBER_MESSAGES = [" + - "{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}," + - "{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}" + - "];\n"))); + assertEquals("[" + + "{\"testRunStarted\":{\"timestamp\":{\"nanos\":0,\"seconds\":10}}}," + + "{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"nanos\":0,\"seconds\":15}}}" + + "]", + extractCucumberMessages(bytes), STRICT); } + private static String extractCucumberMessages(ByteArrayOutputStream bytes) { + Pattern pattern = Pattern.compile("^.*window\\.CUCUMBER_MESSAGES = (\\[.+]);.*$", Pattern.DOTALL); + Matcher matcher = pattern.matcher(new String(bytes.toByteArray(), UTF_8)); + assertThat("bytes must match " + pattern, matcher.find()); + return matcher.group(1); + } }