-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SoftAssertions Implementation (#1340)
- Loading branch information
Showing
7 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsImplProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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<Throwable> results) { | ||
super(results); | ||
this.pageAssertions = new PageAssertionsImpl(page); | ||
} | ||
|
||
private PageAssertionsImplProxy(List<Throwable> 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)); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
playwright/src/main/java/com/microsoft/playwright/assertions/PageAssertionsProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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<Throwable> 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)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.microsoft.playwright.assertions; | ||
|
||
import com.microsoft.playwright.PlaywrightException; | ||
import org.opentest4j.AssertionFailedError; | ||
|
||
import java.util.List; | ||
|
||
class SoftAssertionsBase { | ||
final List<Throwable> results; | ||
|
||
public SoftAssertionsBase(List<Throwable> results) { | ||
this.results = results; | ||
} | ||
|
||
void assertAndCaptureResult(Runnable assertion) { | ||
try { | ||
assertion.run(); | ||
} catch (AssertionFailedError | PlaywrightException failure) { | ||
results.add(failure); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
playwright/src/main/java/com/microsoft/playwright/assertions/SoftAssertionsImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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<Throwable> 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(); | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
playwright/src/test/java/com/microsoft/playwright/TestSoftPageAssertions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
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,<div>A</div>"); | ||
softly.assertThat(page).hasURL("data:text/html,<div>A</div>"); | ||
softly.assertAll(); | ||
assertFailureCount(softly, 0); | ||
} | ||
|
||
@Test | ||
void hasURLTextFail() { | ||
page.navigate("data:text/html,<div>B</div>"); | ||
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,<div>B</div>"); | ||
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,<div>A</div>"); | ||
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,<div>B</div>"); | ||
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("<title> Foo Bar </title>"); | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters