|
| 1 | +package com.microsoft.playwright; |
| 2 | + |
| 3 | +import com.microsoft.playwright.assertions.PageAssertions; |
| 4 | +import com.microsoft.playwright.assertions.SoftAssertions; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.opentest4j.AssertionFailedError; |
| 8 | + |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +import static com.microsoft.playwright.Utils.assertFailureCount; |
| 12 | +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | + |
| 16 | +public class TestSoftPageAssertions extends TestBase { |
| 17 | + private SoftAssertions softly; |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + void beforeEach() { |
| 21 | + softly = SoftAssertions.create(); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void hasUrlTextPass() { |
| 26 | + page.navigate("data:text/html,<div>A</div>"); |
| 27 | + softly.assertThat(page).hasURL("data:text/html,<div>A</div>"); |
| 28 | + softly.assertAll(); |
| 29 | + assertFailureCount(softly, 0); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void hasURLTextFail() { |
| 34 | + page.navigate("data:text/html,<div>B</div>"); |
| 35 | + softly.assertThat(page).hasURL("foo", new PageAssertions.HasURLOptions().setTimeout(1_000)); |
| 36 | + AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> softly.assertAll()); |
| 37 | + assertTrue(e.getMessage().contains("1 assertion(s) failed"), e.getMessage()); |
| 38 | + assertTrue(e.getMessage().contains("Page URL expected to be"), e.getMessage()); |
| 39 | + assertFailureCount(softly, 1); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void shouldSupportHasUrlWithBaseUrl() { |
| 44 | + try (BrowserContext context = browser.newContext(new Browser.NewContextOptions().setBaseURL(server.PREFIX))) { |
| 45 | + Page page = context.newPage(); |
| 46 | + page.navigate(server.EMPTY_PAGE); |
| 47 | + softly.assertThat(page).hasURL("/empty.html", new PageAssertions.HasURLOptions().setTimeout(1_000)); |
| 48 | + softly.assertAll(); |
| 49 | + assertFailureCount(softly, 0); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void notHasUrlText() { |
| 55 | + page.navigate("data:text/html,<div>B</div>"); |
| 56 | + softly.assertThat(page).not().hasURL("about:blank", new PageAssertions.HasURLOptions().setTimeout(1000)); |
| 57 | + softly.assertAll(); |
| 58 | + assertFailureCount(softly, 0); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void hasURLRegexPass() { |
| 63 | + page.navigate("data:text/html,<div>A</div>"); |
| 64 | + softly.assertThat(page).hasURL(Pattern.compile("text")); |
| 65 | + softly.assertAll(); |
| 66 | + assertFailureCount(softly, 0); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void hasURLRegexFail() { |
| 71 | + page.navigate(server.EMPTY_PAGE); |
| 72 | + softly.assertThat(page).hasURL(Pattern.compile(".*foo.*"), new PageAssertions.HasURLOptions().setTimeout(1_000)); |
| 73 | + AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> softly.assertAll()); |
| 74 | + assertTrue(e.getMessage().contains("1 assertion(s) failed"), e.getMessage()); |
| 75 | + assertTrue(e.getMessage().contains("Page URL expected to match regex"), e.getMessage()); |
| 76 | + assertFailureCount(softly, 1); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void notHasUrlRegEx() { |
| 81 | + page.navigate("data:text/html,<div>B</div>"); |
| 82 | + softly.assertThat(page).not().hasURL(Pattern.compile("about"), new PageAssertions.HasURLOptions().setTimeout(1000)); |
| 83 | + softly.assertAll(); |
| 84 | + assertFailureCount(softly, 0); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void hasTitleTextPass() { |
| 89 | + page.navigate(server.PREFIX + "/title.html"); |
| 90 | + softly.assertThat(page).hasTitle("Woof-Woof", new PageAssertions.HasTitleOptions().setTimeout(1_000)); |
| 91 | + softly.assertAll(); |
| 92 | + assertFailureCount(softly, 0); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void hasTitleTextNormalizeWhitespaces() { |
| 97 | + page.setContent("<title> Foo Bar </title>"); |
| 98 | + softly.assertThat(page).hasTitle(" Foo Bar", new PageAssertions.HasTitleOptions().setTimeout(1_000)); |
| 99 | + softly.assertAll(); |
| 100 | + assertFailureCount(softly, 0); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void hasTitleTextFail() { |
| 105 | + page.navigate(server.PREFIX + "/title.html"); |
| 106 | + softly.assertThat(page).hasTitle("foo", new PageAssertions.HasTitleOptions().setTimeout(1_000)); |
| 107 | + AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> softly.assertAll()); |
| 108 | + assertTrue(e.getMessage().contains("1 assertion(s) failed"), e.getMessage()); |
| 109 | + assertTrue(e.getMessage().contains("Page title expected to be: foo\nReceived: Woof-Woof"), e.getMessage()); |
| 110 | + assertFailureCount(softly, 1); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void hasTitleRegexPass() { |
| 115 | + page.navigate(server.PREFIX + "/title.html"); |
| 116 | + softly.assertThat(page).hasTitle(Pattern.compile("^.oof.+oof$")); |
| 117 | + softly.assertAll(); |
| 118 | + assertFailureCount(softly, 0); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void hasTitleRegexFail() { |
| 123 | + page.navigate(server.PREFIX + "/title.html"); |
| 124 | + softly.assertThat(page).hasTitle(Pattern.compile("^foo[AB]"), new PageAssertions.HasTitleOptions().setTimeout(1_000)); |
| 125 | + AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> softly.assertAll()); |
| 126 | + assertTrue(e.getMessage().contains("1 assertion(s) failed"), e.getMessage()); |
| 127 | + assertTrue(e.getMessage().contains("Page title expected to match regex: ^foo[AB]\nReceived: Woof-Woof"), e.getMessage()); |
| 128 | + assertFailureCount(softly, 1); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void notHasTitleRegEx() { |
| 133 | + page.navigate(server.PREFIX + "/title.html"); |
| 134 | + softly.assertThat(page).not().hasTitle(Pattern.compile("ab.ut")); |
| 135 | + softly.assertAll(); |
| 136 | + assertFailureCount(softly, 0); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void hasTitleRegExCaseInsensitivePass() { |
| 141 | + page.navigate(server.PREFIX + "/title.html"); |
| 142 | + softly.assertThat(page).hasTitle(Pattern.compile("woof-woof", Pattern.CASE_INSENSITIVE)); |
| 143 | + softly.assertAll(); |
| 144 | + assertFailureCount(softly, 0); |
| 145 | + } |
| 146 | +} |
0 commit comments