From 7d5953c96eeb1425f25cbdbe63f62eb3b7ae17aa Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Thu, 17 Aug 2023 10:11:30 -0700 Subject: [PATCH] Revert "SoftAssertions Implementation (#1340)" (#1357) This reverts commit 632fba54a53174a011ed75a5a05a8d7cc618c422. --- .../assertions/PageAssertionsImplProxy.java | 46 ------ .../assertions/PageAssertionsProxy.java | 41 ----- .../playwright/assertions/SoftAssertions.java | 13 -- .../assertions/SoftAssertionsBase.java | 22 --- .../assertions/SoftAssertionsImpl.java | 42 ----- .../playwright/TestSoftPageAssertions.java | 146 ------------------ .../java/com/microsoft/playwright/Utils.java | 14 -- 7 files changed, 324 deletions(-) delete mode 100644 playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsImplProxy.java delete mode 100644 playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsProxy.java delete mode 100644 playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertions.java delete mode 100644 playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsBase.java delete mode 100644 playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsImpl.java delete mode 100644 playwright/src/test/java/com/microsoft/playwright/TestSoftPageAssertions.java diff --git a/playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsImplProxy.java b/playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsImplProxy.java deleted file mode 100644 index dfcaeef4f..000000000 --- a/playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsImplProxy.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.microsoft.playwright.assertions; - -import com.microsoft.playwright.Page; -import com.microsoft.playwright.impl.PageAssertionsImpl; - -import java.util.List; -import java.util.regex.Pattern; - -class PageAssertionsImplProxy extends SoftAssertionsBase implements PageAssertions { - private final PageAssertionsImpl pageAssertions; - - PageAssertionsImplProxy(Page page, List results) { - super(results); - this.pageAssertions = new PageAssertionsImpl(page); - } - - private PageAssertionsImplProxy(List results, PageAssertionsImpl pageAssertions) { - super(results); - this.pageAssertions = pageAssertions; - } - - @Override - public PageAssertions not() { - return new PageAssertionsImplProxy(super.results, (PageAssertionsImpl) pageAssertions.not()); - } - - @Override - public void hasTitle(String titleOrRegExp, HasTitleOptions options) { - assertAndCaptureResult(() -> pageAssertions.hasTitle(titleOrRegExp, options)); - } - - @Override - public void hasTitle(Pattern titleOrRegExp, HasTitleOptions options) { - assertAndCaptureResult(() -> pageAssertions.hasTitle(titleOrRegExp, options)); - } - - @Override - public void hasURL(String urlOrRegExp, HasURLOptions options) { - assertAndCaptureResult(() -> pageAssertions.hasURL(urlOrRegExp, options)); - } - - @Override - public void hasURL(Pattern urlOrRegExp, HasURLOptions options) { - assertAndCaptureResult(() -> pageAssertions.hasURL(urlOrRegExp, options)); - } -} diff --git a/playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsProxy.java b/playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsProxy.java deleted file mode 100644 index 2b73dd9e7..000000000 --- a/playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsProxy.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.microsoft.playwright.assertions; - -import com.microsoft.playwright.Page; -import com.microsoft.playwright.impl.PageAssertionsImpl; - -import java.util.List; -import java.util.regex.Pattern; - -class PageAssertionsProxy extends SoftAssertionsBase implements PageAssertions { - private final PageAssertionsImpl pageAssertions; - - PageAssertionsProxy(Page page, List results) { - super(results); - this.pageAssertions = new PageAssertionsImpl(page); - } - - @Override - public PageAssertions not() { - return pageAssertions.not(); - } - - @Override - public void hasTitle(String titleOrRegExp, HasTitleOptions options) { - assertAndCaptureResult(() -> pageAssertions.hasTitle(titleOrRegExp, options)); - } - - @Override - public void hasTitle(Pattern titleOrRegExp, HasTitleOptions options) { - assertAndCaptureResult(() -> pageAssertions.hasTitle(titleOrRegExp, options)); - } - - @Override - public void hasURL(String urlOrRegExp, HasURLOptions options) { - assertAndCaptureResult(() -> pageAssertions.hasURL(urlOrRegExp, options)); - } - - @Override - public void hasURL(Pattern urlOrRegExp, HasURLOptions options) { - assertAndCaptureResult(() -> pageAssertions.hasURL(urlOrRegExp, options)); - } -} diff --git a/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertions.java b/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertions.java deleted file mode 100644 index e99e89db8..000000000 --- a/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertions.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.microsoft.playwright.assertions; - -import com.microsoft.playwright.Page; - -public interface SoftAssertions { - PageAssertions assertThat(Page page); - - void assertAll(); - - static SoftAssertions create() { - return new SoftAssertionsImpl(); - } -} diff --git a/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsBase.java b/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsBase.java deleted file mode 100644 index 436ed0f80..000000000 --- a/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsBase.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.microsoft.playwright.assertions; - -import com.microsoft.playwright.PlaywrightException; -import org.opentest4j.AssertionFailedError; - -import java.util.List; - -class SoftAssertionsBase { - final List results; - - public SoftAssertionsBase(List results) { - this.results = results; - } - - void assertAndCaptureResult(Runnable assertion) { - try { - assertion.run(); - } catch (AssertionFailedError | PlaywrightException failure) { - results.add(failure); - } - } -} diff --git a/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsImpl.java b/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsImpl.java deleted file mode 100644 index 24de7ec1c..000000000 --- a/playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsImpl.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.microsoft.playwright.assertions; - -import com.microsoft.playwright.Page; -import org.opentest4j.AssertionFailedError; - -import java.util.ArrayList; -import java.util.List; - -class SoftAssertionsImpl implements SoftAssertions { - final List results; - - SoftAssertionsImpl() { - this.results = new ArrayList<>(); - } - - @Override - public PageAssertions assertThat(Page page) { - return new PageAssertionsImplProxy(page, results); - } - - @Override - public void assertAll() { - if (!results.isEmpty()) { - throw new AssertionFailedError(getFormattedErrorMessage()); - } - } - - private String getFormattedErrorMessage() { - StringBuilder message = new StringBuilder(); - message - .append(results.size()) - .append(" assertion(s) failed:"); - - for (Throwable t : results) { - message.append("\n"); - message.append("----------------------------------------\n"); - message.append(t.getMessage()); - } - - return message.toString(); - } -} diff --git a/playwright/src/test/java/com/microsoft/playwright/TestSoftPageAssertions.java b/playwright/src/test/java/com/microsoft/playwright/TestSoftPageAssertions.java deleted file mode 100644 index 6c2ddd854..000000000 --- a/playwright/src/test/java/com/microsoft/playwright/TestSoftPageAssertions.java +++ /dev/null @@ -1,146 +0,0 @@ -package com.microsoft.playwright; - -import com.microsoft.playwright.assertions.PageAssertions; -import com.microsoft.playwright.assertions.SoftAssertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.opentest4j.AssertionFailedError; - -import java.util.regex.Pattern; - -import static com.microsoft.playwright.Utils.assertFailureCount; -import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class TestSoftPageAssertions extends TestBase { - private SoftAssertions softly; - - @BeforeEach - void beforeEach() { - softly = SoftAssertions.create(); - } - - @Test - void hasUrlTextPass() { - page.navigate("data:text/html,
A
"); - softly.assertThat(page).hasURL("data:text/html,
A
"); - softly.assertAll(); - assertFailureCount(softly, 0); - } - - @Test - void hasURLTextFail() { - page.navigate("data:text/html,
B
"); - softly.assertThat(page).hasURL("foo", new PageAssertions.HasURLOptions().setTimeout(1_000)); - AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> softly.assertAll()); - assertTrue(e.getMessage().contains("1 assertion(s) failed"), e.getMessage()); - assertTrue(e.getMessage().contains("Page URL expected to be"), e.getMessage()); - assertFailureCount(softly, 1); - } - - @Test - void shouldSupportHasUrlWithBaseUrl() { - try (BrowserContext context = browser.newContext(new Browser.NewContextOptions().setBaseURL(server.PREFIX))) { - Page page = context.newPage(); - page.navigate(server.EMPTY_PAGE); - softly.assertThat(page).hasURL("/empty.html", new PageAssertions.HasURLOptions().setTimeout(1_000)); - softly.assertAll(); - assertFailureCount(softly, 0); - } - } - - @Test - void notHasUrlText() { - page.navigate("data:text/html,
B
"); - softly.assertThat(page).not().hasURL("about:blank", new PageAssertions.HasURLOptions().setTimeout(1000)); - softly.assertAll(); - assertFailureCount(softly, 0); - } - - @Test - void hasURLRegexPass() { - page.navigate("data:text/html,
A
"); - softly.assertThat(page).hasURL(Pattern.compile("text")); - softly.assertAll(); - assertFailureCount(softly, 0); - } - - @Test - void hasURLRegexFail() { - page.navigate(server.EMPTY_PAGE); - softly.assertThat(page).hasURL(Pattern.compile(".*foo.*"), new PageAssertions.HasURLOptions().setTimeout(1_000)); - AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> softly.assertAll()); - assertTrue(e.getMessage().contains("1 assertion(s) failed"), e.getMessage()); - assertTrue(e.getMessage().contains("Page URL expected to match regex"), e.getMessage()); - assertFailureCount(softly, 1); - } - - @Test - void notHasUrlRegEx() { - page.navigate("data:text/html,
B
"); - softly.assertThat(page).not().hasURL(Pattern.compile("about"), new PageAssertions.HasURLOptions().setTimeout(1000)); - softly.assertAll(); - assertFailureCount(softly, 0); - } - - @Test - void hasTitleTextPass() { - page.navigate(server.PREFIX + "/title.html"); - softly.assertThat(page).hasTitle("Woof-Woof", new PageAssertions.HasTitleOptions().setTimeout(1_000)); - softly.assertAll(); - assertFailureCount(softly, 0); - } - - @Test - void hasTitleTextNormalizeWhitespaces() { - page.setContent(" Foo Bar "); - softly.assertThat(page).hasTitle(" Foo Bar", new PageAssertions.HasTitleOptions().setTimeout(1_000)); - softly.assertAll(); - assertFailureCount(softly, 0); - } - - @Test - void hasTitleTextFail() { - page.navigate(server.PREFIX + "/title.html"); - softly.assertThat(page).hasTitle("foo", new PageAssertions.HasTitleOptions().setTimeout(1_000)); - AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> softly.assertAll()); - assertTrue(e.getMessage().contains("1 assertion(s) failed"), e.getMessage()); - assertTrue(e.getMessage().contains("Page title expected to be: foo\nReceived: Woof-Woof"), e.getMessage()); - assertFailureCount(softly, 1); - } - - @Test - void hasTitleRegexPass() { - page.navigate(server.PREFIX + "/title.html"); - softly.assertThat(page).hasTitle(Pattern.compile("^.oof.+oof$")); - softly.assertAll(); - assertFailureCount(softly, 0); - } - - @Test - void hasTitleRegexFail() { - page.navigate(server.PREFIX + "/title.html"); - softly.assertThat(page).hasTitle(Pattern.compile("^foo[AB]"), new PageAssertions.HasTitleOptions().setTimeout(1_000)); - AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> softly.assertAll()); - assertTrue(e.getMessage().contains("1 assertion(s) failed"), e.getMessage()); - assertTrue(e.getMessage().contains("Page title expected to match regex: ^foo[AB]\nReceived: Woof-Woof"), e.getMessage()); - assertFailureCount(softly, 1); - } - - @Test - void notHasTitleRegEx() { - page.navigate(server.PREFIX + "/title.html"); - softly.assertThat(page).not().hasTitle(Pattern.compile("ab.ut")); - softly.assertAll(); - assertFailureCount(softly, 0); - } - - @Test - void hasTitleRegExCaseInsensitivePass() { - page.navigate(server.PREFIX + "/title.html"); - softly.assertThat(page).hasTitle(Pattern.compile("woof-woof", Pattern.CASE_INSENSITIVE)); - softly.assertAll(); - assertFailureCount(softly, 0); - } -} diff --git a/playwright/src/test/java/com/microsoft/playwright/Utils.java b/playwright/src/test/java/com/microsoft/playwright/Utils.java index a8fe8c1a1..dff6bab20 100644 --- a/playwright/src/test/java/com/microsoft/playwright/Utils.java +++ b/playwright/src/test/java/com/microsoft/playwright/Utils.java @@ -19,10 +19,8 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import com.microsoft.playwright.assertions.SoftAssertions; import java.io.*; -import java.lang.reflect.Field; import java.net.ServerSocket; import java.nio.file.Files; import java.nio.file.Path; @@ -216,16 +214,4 @@ static String generateDifferentOriginHostname(final Server server){ static String generateDifferentOriginPort(final Server server){ return server.PREFIX.replace(String.valueOf(server.PORT), String.valueOf(server.PORT+1)); } - - static void assertFailureCount(SoftAssertions softAssertions, int expectedCount) { - try { - Class clazz = softAssertions.getClass(); - Field resultsField = clazz.getDeclaredField("results"); - resultsField.setAccessible(true); - List results = (List) resultsField.get(softAssertions); - assertEquals(results.size(), expectedCount); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } - } }