diff --git a/playwright/src/main/AGENTS.md b/playwright/src/main/AGENTS.md index 7cc3afaee..8fb20f46c 100644 --- a/playwright/src/main/AGENTS.md +++ b/playwright/src/main/AGENTS.md @@ -27,6 +27,16 @@ Java code around the place it goes to, and write the new lines the same way. One upstream file can have several Java files, one per class (`network.ts` is `RequestImpl`, `ResponseImpl`, `RouteImpl`, `WebSocketImpl`, ...). A new upstream file is a new Java file with that header. +- The assertions are the port of `packages/playwright/src/matchers`: `matchers.ts` is + `LocatorAssertionsImpl`, `PageAssertionsImpl` and `APIResponseAssertionsImpl`; `toBeTruthy.ts`, + `toEqual.ts`, `toMatchText.ts`, `toMatchAriaSnapshot.ts` and `matcherHint.ts` are the classes + of the same name; `AssertionsBase` is the call of a matcher from `expect.ts`. The `utils` of a + matcher are `MatcherUtils`: jest is not ported: the values are printed in the same layout, and + where upstream prints a diff both values are printed in full. A failure message has the upstream layout with the + Java call: `assertThat(locator).not().hasText(expected) failed` for + `expect(locator).not.toHaveText(expected) failed`, and the title `Assert "hasText"`. +- `isomorphic/locatorGenerators.ts` and the selector parser are not ported: `Locator.toString()` + asks the driver (`LocalUtils.asLocatorDescription`, language `java`). - A class that implements a public interface is `Impl` (`Page` is `PageImpl`). A helper class keeps the upstream name (`Waiter`, `HarRouter`, `TimeoutSettings`). - The members keep the upstream order. A new upstream member goes where it is in the `.ts` file, diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/APIResponseAssertionsImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/APIResponseAssertionsImpl.java index e3ad8aa0a..89951f8d7 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/APIResponseAssertionsImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/APIResponseAssertionsImpl.java @@ -14,23 +14,26 @@ * limitations under the License. */ +// Port of matchers.ts. package com.microsoft.playwright.impl; -import com.microsoft.playwright.impl.protocol.*; import com.microsoft.playwright.APIResponse; import com.microsoft.playwright.assertions.APIResponseAssertions; -import org.opentest4j.AssertionFailedError; +import com.microsoft.playwright.impl.MatcherHint.MatcherMessageDetails; +import com.microsoft.playwright.impl.MatcherHint.MatcherResult; import java.util.List; import java.util.regex.Pattern; -public class APIResponseAssertionsImpl implements APIResponseAssertions { - private final APIResponse actual; - private final boolean isNot; +import static com.microsoft.playwright.impl.MatcherHint.formatMatcherMessage; + +// java: the matcher of an APIResponse, see LocatorAssertionsImpl. +public class APIResponseAssertionsImpl extends AssertionsBase implements APIResponseAssertions { + private final APIResponseImpl actual; APIResponseAssertionsImpl(APIResponse response, boolean isNot) { - this.actual = response; - this.isNot = isNot; + super(isNot); + this.actual = (APIResponseImpl) response; } public APIResponseAssertionsImpl(APIResponse response) { @@ -44,31 +47,39 @@ public APIResponseAssertions not() { @Override public void isOK() { - if (actual.ok() == !isNot) { - return; - } - String message = "Response status expected to be within [200..299] range, was " + actual.status(); - if (isNot) { - message = message.replace("expected to", "expected not to"); - } - List logList = ((APIResponseImpl) actual).fetchLog(); - String log = String.join("\n", logList); - if (!log.isEmpty()) { - log = "\nCall log:\n" + log; - } + callMatcherAsStep("isOK", state -> { + APIResponseImpl response = actual; + String matcherName = "isOK"; - String contentType = actual.headers().get("content-type"); - boolean isTextEncoding = contentType == null ? false : isTextualMimeType(contentType); - String responseText = ""; - if (isTextEncoding) { - String text = actual.text(); - if (text != null) { - responseText = "\nResponse text:\n" + (text.length() > 1000 ? text.substring(0, 1000) : text); + String contentType = response.headers().get("content-type"); + boolean isTextEncoding = contentType != null && !contentType.isEmpty() && isTextualMimeType(contentType); + List log = null; + String text = null; + if (state.isNot == response.ok()) { + log = response.fetchLog(); + text = isTextEncoding ? response.text() : null; } - } - throw new AssertionFailedError(message + log + responseText); + List finalLog = log; + String finalText = text; + MatcherResult result = new MatcherResult(); + result.message = () -> { + MatcherMessageDetails details = new MatcherMessageDetails(); + details.isNot = state.isNot; + details.promise = state.promise; + details.matcherName = matcherName; + details.receiver = "response"; + details.expectation = ""; + details.log = finalLog; + return formatMatcherMessage(details) + (finalText == null ? "" : "\nResponse text:\n" + (finalText.length() > 1000 ? finalText.substring(0, 1000) : finalText)); + }; + + result.pass = response.ok(); + return result; + }); } + + // java: isTextualMimeType of isomorphic/mimeType.ts. static boolean isTextualMimeType(String mimeType) { return Pattern.matches("^(text/.*?|application/(json|(x-)?javascript|xml.*?|ecmascript|graphql|x-www-form-urlencoded)|image/svg(\\+xml)?|application/.*?(\\+json|\\+xml))(;\\s*charset=.*)?$", mimeType); } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/AssertionsBase.java b/playwright/src/main/java/com/microsoft/playwright/impl/AssertionsBase.java index dd2cc0968..cbcff7396 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/AssertionsBase.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/AssertionsBase.java @@ -14,21 +14,20 @@ * limitations under the License. */ +// Port of expect.ts. package com.microsoft.playwright.impl; -import com.microsoft.playwright.impl.protocol.*; +import com.microsoft.playwright.impl.MatcherHint.MatcherResult; import org.opentest4j.AssertionFailedError; import org.opentest4j.ValueWrapper; -import java.lang.reflect.Field; import java.util.Collection; -import java.util.List; -import java.util.regex.Pattern; import java.util.stream.Collectors; -import static com.microsoft.playwright.impl.Utils.toJsRegexFlags; import static java.util.Arrays.asList; +// java: only the call of a matcher is ported: callMatcherAsStep and invokeMatcher. The expect function, its +// configuration, the custom, soft, poll and promise matchers and the steps belong to the test runner. abstract class AssertionsBase { final boolean isNot; @@ -36,56 +35,56 @@ abstract class AssertionsBase { this.isNot = isNot; } - void expectImpl(String expression, ExpectedTextValue textValue, Object expected, String message, FrameExpectOptions options, String title) { - expectImpl(expression, asList(textValue), expected, message, options, title); + // java: the fields of ExpectMatcherStateInternal that the matchers read, utils is MatcherUtils. + static class ExpectMatcherState { + boolean isNot; + String promise; + double timeout; + String title; } - void expectImpl(String expression, List expectedText, Object expected, String message, FrameExpectOptions options, String title) { - if (options == null) { - options = new FrameExpectOptions(); - } - options.expectedText = expectedText; - expectImpl(expression, options, expected, message, title); + // java: the type of a matcher bound to its receiver and arguments. + interface RawMatcherFn { + MatcherResult call(ExpectMatcherState state); } - void expectImpl(String expression, FrameExpectOptions expectOptions, Object expected, String message, String title) { - if (expectOptions.timeout == null) { - expectOptions.timeout = AssertionsTimeout.defaultTimeout; - } - expectOptions.isNot = isNot; - if (isNot) { - message = message.replace("expected to", "expected not to"); - } - FrameExpectResult result = doExpect(expression, expectOptions, title); - if (result.matches == isNot) { - Object actual; - if (result.received == null) { - actual = null; - } else if (result.received.value != null) { - actual = Serialization.deserialize(result.received.value); - } else { - actual = result.received.ariaSnapshot; - } - String log = (result.log == null) ? "" : String.join("\n", result.log); - if (!log.isEmpty()) { - log = "\nCall log:\n" + log; - } - if (result.errorMessage != null) { - message += "\n" + result.errorMessage; - } - if (expected == null) { - throw new AssertionFailedError(message + log); - } - ValueWrapper expectedValue = formatValue(expected); - ValueWrapper actualValue = formatValue(actual); - message += "\nExpected: " + expectedValue.getStringRepresentation() + "\nReceived: " + actualValue.getStringRepresentation() + "\n"; - throw new AssertionFailedError(message + log, expectedValue, actualValue); + // java: the type of the query argument of toBeTruthy, toEqual and toMatchText. + interface Query { + FrameExpectResult query(boolean isNot, double timeout); + } + + void callMatcherAsStep(String matcherName, RawMatcherFn matcher) { + // java: no custom message and no title suffix, escapeWithQuotes has nothing to escape in a matcher name. + String defaultTitle = (isNot ? "not " : "") + matcherName; + // java: the title of the Java client is Assert, with the Java name of the matcher. + String title = "Assert \"" + defaultTitle + "\""; + + MatcherResult result = invokeMatcher(matcherName, matcher, title); + if (result.pass == isNot) { + // java: ExpectError is an AssertionFailedError with the expected and the actual value of the result. + String message = result.message.get(); + if (result.expected == null && (result.actual == null || result.actual == MatcherUtils.UNDEFINED)) + throw new AssertionFailedError(message); + throw new AssertionFailedError(message, formatValue(result.expected), formatValue(result.actual)); } } - abstract FrameExpectResult doExpect(String expression, FrameExpectOptions expectOptions, String title); + private MatcherResult invokeMatcher(String matcherName, RawMatcherFn matcher, String title) { + double timeout = AssertionsTimeout.defaultTimeout; + ExpectMatcherState matcherContext = new ExpectMatcherState(); + matcherContext.isNot = isNot; + matcherContext.promise = ""; + matcherContext.timeout = timeout; + matcherContext.title = title; + + return matcher.call(matcherContext); + } + // java: a helper with no counterpart upstream, the value of an AssertionFailedError. protected static ValueWrapper formatValue(Object value) { + if (value == MatcherUtils.UNDEFINED) { + return ValueWrapper.create(null); + } if (value == null || !value.getClass().isArray()) { return ValueWrapper.create(value); } @@ -93,26 +92,4 @@ protected static ValueWrapper formatValue(Object value) { String stringRepresentation = "[" + String.join(", ", values) + "]"; return ValueWrapper.create(value, stringRepresentation); } - - static ExpectedTextValue expectedRegex(Pattern pattern) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.regexSource = pattern.pattern(); - if (pattern.flags() != 0) { - expected.regexFlags = toJsRegexFlags(pattern); - } - return expected; - } - - static Boolean shouldIgnoreCase(Object options) { - if (options == null) { - return null; - } - try { - Field fromField = options.getClass().getDeclaredField("ignoreCase"); - Object value = fromField.get(options); - return (Boolean) value; - } catch (NoSuchFieldException | IllegalAccessException e) { - return null; - } - } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/FrameExpectOptions.java b/playwright/src/main/java/com/microsoft/playwright/impl/FrameExpectOptions.java index 77f19a2e1..05000a149 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/FrameExpectOptions.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/FrameExpectOptions.java @@ -17,20 +17,20 @@ package com.microsoft.playwright.impl; import com.microsoft.playwright.impl.protocol.ExpectedTextValue; -import com.microsoft.playwright.impl.protocol.SerializedArgument; import com.microsoft.playwright.options.PseudoElement; import java.util.List; -// java: the options of Frame._expect, FrameExpectParams of types.ts. +// java: the options of Frame._expect: FrameExpectParams of types.ts with the timeout and the title. class FrameExpectOptions { Object expressionArg; List expectedText; String selector; Double expectedNumber; - SerializedArgument expectedValue; + Object expectedValue; Boolean useInnerText; PseudoElement pseudo; boolean isNot; - Double timeout; + double timeout; + String title; } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/FrameExpectResult.java b/playwright/src/main/java/com/microsoft/playwright/impl/FrameExpectResult.java index 8a4d3477d..5bc4b4290 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/FrameExpectResult.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/FrameExpectResult.java @@ -16,14 +16,18 @@ package com.microsoft.playwright.impl; -import com.microsoft.playwright.impl.protocol.FrameExpectErrorDetails; - import java.util.List; -// java: ExpectResult of frame.ts; the command has no result, a mismatch arrives as an error with FrameExpectErrorDetails. +// java: ExpectResult and ExpectReceived of frame.ts. class FrameExpectResult { boolean matches; - FrameExpectErrorDetails.FrameExpectErrorDetailsReceived received; - String errorMessage; + ExpectReceived received; List log; + Boolean timedOut; + String errorMessage; + + static class ExpectReceived { + Object value; + String ariaSnapshot; + } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java index afbeca621..ba40b4617 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/FrameImpl.java @@ -39,6 +39,7 @@ import static com.microsoft.playwright.impl.isomorphic.LocatorUtils.*; import static com.microsoft.playwright.impl.Serialization.deserialize; import static com.microsoft.playwright.impl.Serialization.gson; +import static com.microsoft.playwright.impl.Serialization.parseResult; import static com.microsoft.playwright.impl.Serialization.assertEvaluateOptions; import static com.microsoft.playwright.impl.Serialization.serializeArgument; import static com.microsoft.playwright.impl.Serialization.serializeArgumentWithCallbacks; @@ -915,25 +916,42 @@ public String title() { return channel.title(NO_TIMEOUT).value; } - // java: the title is a parameter, the options are the seed FrameExpectOptions, the result the seed FrameExpectResult. - FrameExpectResult expect(String expression, FrameExpectOptions options, String title) { + // java: the options are FrameExpectOptions, the result is FrameExpectResult. + FrameExpectResult expect(String expression, FrameExpectOptions options) { + double timeout = options.timeout; + String title = options.title; return wrapApiCall(() -> { - FrameExpectParams params = toProtocol(this, options, FrameExpectParams.class); + // java: ...rest is one assignment per field, expectedValue is not a protocol value before it is serialized. + FrameExpectParams params = new FrameExpectParams(); params.expression = expression; + params.selector = options.selector; + params.expressionArg = options.expressionArg == null ? null : gson().toJsonTree(options.expressionArg); + params.pseudo = options.pseudo == null ? null : gson().toJsonTree(options.pseudo).getAsString(); + params.expectedText = options.expectedText; + params.expectedNumber = options.expectedNumber; + params.useInnerText = options.useInnerText; params.isNot = options.isNot; - // java: expectedValue is serialized by the assertion classes. - FrameExpectResult result = new FrameExpectResult(); + params.expectedValue = serializeArgument(options.expectedValue); try { - channel.expect(params, options.timeout); + channel.expect(params, timeout); + FrameExpectResult result = new FrameExpectResult(); result.matches = !params.isNot; return result; } catch (ServerErrorWithDetails e) { - // java: no AbortSignal, so no AbortError branch; only errors with details are expect mismatches. + // java: no AbortSignal, so no AbortError branch; only an error with details is a PlaywrightError of the server. FrameExpectErrorDetails details = gson().fromJson(e.errorDetails(), FrameExpectErrorDetails.class); + FrameExpectResult.ExpectReceived received = null; + if (details.received != null) { + received = new FrameExpectResult.ExpectReceived(); + // java: an undefined value is MatcherUtils.UNDEFINED, null is the JavaScript null. + received.value = details.received.value != null && !"undefined".equals(details.received.value.v) ? parseResult(details.received.value) : MatcherUtils.UNDEFINED; + received.ariaSnapshot = details.received.ariaSnapshot; + } + FrameExpectResult result = new FrameExpectResult(); result.matches = params.isNot; - // java: AssertionsBase parses the received value, FrameExpectResult has no timedOut. - result.received = details.received; + result.received = received; result.log = e.log(); + result.timedOut = details.timedOut; result.errorMessage = details.customErrorMessage != null ? "Error: " + details.customErrorMessage : null; return result; } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LocalUtils.java b/playwright/src/main/java/com/microsoft/playwright/impl/LocalUtils.java index 87be2ac77..619f1f4aa 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LocalUtils.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LocalUtils.java @@ -85,6 +85,14 @@ JsonArray deviceDescriptors() { return initializer.getAsJsonArray("deviceDescriptors"); } + // java: the locator generators run on the driver, with the Java language. + String asLocatorDescription(String selector) { + LocalUtilsAsLocatorDescriptionParams params = new LocalUtilsAsLocatorDescriptionParams(); + params.selector = selector; + params.sdkLanguage = "java"; + return wrapApiCall(() -> channel.asLocatorDescription(params, NO_TIMEOUT).description, null, true); + } + // java: the Java API matches globs on the driver. public Pattern globToRegex(String glob, String baseURL, boolean webSocketUrl) { LocalUtilsGlobToRegexParams params = new LocalUtilsGlobToRegexParams(); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java index fa481ef86..983d28dbe 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java @@ -14,24 +14,32 @@ * limitations under the License. */ +// Port of matchers.ts. package com.microsoft.playwright.impl; -import com.microsoft.playwright.impl.protocol.*; import com.microsoft.playwright.Locator; import com.microsoft.playwright.assertions.LocatorAssertions; +import com.microsoft.playwright.impl.protocol.ExpectedTextValue; import com.microsoft.playwright.options.AriaRole; import java.util.ArrayList; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; -import static com.microsoft.playwright.impl.Serialization.serializeArgument; -import static com.microsoft.playwright.impl.Utils.convertType; +import static com.microsoft.playwright.impl.ToBeTruthy.toBeTruthy; +import static com.microsoft.playwright.impl.ToEqual.toEqual; +import static com.microsoft.playwright.impl.ToMatchAriaSnapshot.toMatchAriaSnapshot; +import static com.microsoft.playwright.impl.ToMatchText.toMatchText; +import static com.microsoft.playwright.impl.Utils.toJsRegexFlags; +// java: the matchers of a Locator; those of a Page are in PageAssertionsImpl, toBeOK is in APIResponseAssertionsImpl. +// A matcher is the method of the public interface: it calls its upstream body through callMatcherAsStep, and its +// name in a message and in a title is the Java name (hasText for toHaveText). The +// options are read as options?.name, a matcher with a string, a pattern or an array has one overload per type. public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAssertions { - LocatorImpl actualLocator; + final LocatorImpl actualLocator; public LocatorAssertionsImpl(Locator locator) { this(locator, false); @@ -43,445 +51,611 @@ private LocatorAssertionsImpl(Locator locator, boolean isNot) { } @Override - FrameExpectResult doExpect(String expression, FrameExpectOptions expectOptions, String title) { - return actualLocator.expect(expression, expectOptions, title); + public LocatorAssertions not() { + return new LocatorAssertionsImpl(actualLocator, !isNot); } + // java: items is an array of String or Pattern, the options are three parameters. + static List serializeExpectedTextValues(Object[] items, Boolean matchSubstring, Boolean normalizeWhiteSpace, Boolean ignoreCase) { + List result = new ArrayList<>(); + for (Object i : items) { + ExpectedTextValue value = new ExpectedTextValue(); + value.string = i instanceof String ? (String) i : null; + value.regexSource = i instanceof Pattern ? ((Pattern) i).pattern() : null; + value.regexFlags = i instanceof Pattern ? toJsRegexFlags((Pattern) i) : null; + value.matchSubstring = matchSubstring; + value.ignoreCase = ignoreCase; + value.normalizeWhiteSpace = normalizeWhiteSpace; + result.add(value); + } + return result; + } @Override - public void containsClass(String classname, ContainsClassOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = classname; - expectImpl("to.contain.class", expected, classname, "Locator expected to contain class", convertType(options, FrameExpectOptions.class), "Assert \"containsClass\""); + public void isAttached(IsAttachedOptions options) { + callMatcherAsStep("isAttached", state -> { + boolean attached = options == null || options.attached == null || options.attached; + String expected = attached ? "attached" : "detached"; + String arg = attached ? "" : "{ attached: false }"; + return toBeTruthy(state, "isAttached", actualLocator, "Locator", expected, arg, (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect(attached ? "to.be.attached" : "to.be.detached", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void containsClass(List classnames, ContainsClassOptions options) { - List list = new ArrayList<>(); - for (String text : classnames) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - list.add(expected); - } - expectImpl("to.contain.class.array", list, classnames, "Locator expected to contain classes", convertType(options, FrameExpectOptions.class), "Assert \"containsClass\""); + public void isChecked(IsCheckedOptions options) { + callMatcherAsStep("isChecked", state -> { + Boolean checked = options == null ? null : options.checked; + Boolean indeterminate = options == null ? null : options.indeterminate; + // java: an undefined property is a missing key. + Map expectedValue = new LinkedHashMap<>(); + if (checked != null) + expectedValue.put("checked", checked); + if (indeterminate != null) + expectedValue.put("indeterminate", indeterminate); + String expected; + String arg; + if (options != null && options.indeterminate != null && options.indeterminate) { + expected = "indeterminate"; + arg = "{ indeterminate: true }"; + } else { + expected = options != null && options.checked != null && !options.checked ? "unchecked" : "checked"; + arg = options != null && options.checked != null && !options.checked ? "{ checked: false }" : ""; + } + return toBeTruthy(state, "isChecked", actualLocator, "Locator", expected, arg, (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.expectedValue = expectedValue; + expectOptions.title = state.title; + return actualLocator.expect("to.be.checked", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void containsText(String text, ContainsTextOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - expected.ignoreCase = shouldIgnoreCase(options); - expected.matchSubstring = true; - expected.normalizeWhiteSpace = true; - expectImpl("to.have.text", expected, text, "Locator expected to contain text", convertType(options, FrameExpectOptions.class), "Assert \"containsText\""); + public void isDisabled(IsDisabledOptions options) { + callMatcherAsStep("isDisabled", state -> { + return toBeTruthy(state, "isDisabled", actualLocator, "Locator", "disabled", "", (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.be.disabled", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void containsText(Pattern pattern, ContainsTextOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.ignoreCase = shouldIgnoreCase(options); - expected.matchSubstring = true; - expected.normalizeWhiteSpace = true; - expectImpl("to.have.text", expected, pattern, "Locator expected to contain regex", convertType(options, FrameExpectOptions.class), "Assert \"containsText\""); + public void isEditable(IsEditableOptions options) { + callMatcherAsStep("isEditable", state -> { + boolean editable = options == null || options.editable == null || options.editable; + String expected = editable ? "editable" : "readOnly"; + String arg = editable ? "" : "{ editable: false }"; + return toBeTruthy(state, "isEditable", actualLocator, "Locator", expected, arg, (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect(editable ? "to.be.editable" : "to.be.readonly", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void containsText(String[] strings, ContainsTextOptions options) { - List list = new ArrayList<>(); - for (String text : strings) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - expected.ignoreCase = shouldIgnoreCase(options); - expected.matchSubstring = true; - expected.normalizeWhiteSpace = true; - list.add(expected); - } - expectImpl("to.contain.text.array", list, strings, "Locator expected to contain text", convertType(options, FrameExpectOptions.class), "Assert \"containsText\""); + public void isEmpty(IsEmptyOptions options) { + callMatcherAsStep("isEmpty", state -> { + return toBeTruthy(state, "isEmpty", actualLocator, "Locator", "empty", "", (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.be.empty", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void containsText(Pattern[] patterns, ContainsTextOptions options) { - List list = new ArrayList<>(); - for (Pattern pattern : patterns) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.ignoreCase = shouldIgnoreCase(options); - expected.matchSubstring = true; - expected.normalizeWhiteSpace = true; - list.add(expected); - } - expectImpl("to.contain.text.array", list, patterns, "Locator expected to contain text", convertType(options, FrameExpectOptions.class), "Assert \"containsText\""); + public void isEnabled(IsEnabledOptions options) { + callMatcherAsStep("isEnabled", state -> { + boolean enabled = options == null || options.enabled == null || options.enabled; + String expected = enabled ? "enabled" : "disabled"; + String arg = enabled ? "" : "{ enabled: false }"; + return toBeTruthy(state, "isEnabled", actualLocator, "Locator", expected, arg, (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect(enabled ? "to.be.enabled" : "to.be.disabled", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void hasAccessibleDescription(String description, HasAccessibleDescriptionOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = description; - expected.ignoreCase = shouldIgnoreCase(options); - expected.normalizeWhiteSpace = true; - expectImpl("to.have.accessible.description", expected, description, "Locator expected to have accessible description", convertType(options, FrameExpectOptions.class), "Assert \"hasAccessibleDescription\""); + public void isFocused(IsFocusedOptions options) { + callMatcherAsStep("isFocused", state -> { + return toBeTruthy(state, "isFocused", actualLocator, "Locator", "focused", "", (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.be.focused", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void hasAccessibleDescription(Pattern pattern, HasAccessibleDescriptionOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.ignoreCase = shouldIgnoreCase(options); - expected.normalizeWhiteSpace = true; - expectImpl("to.have.accessible.description", expected, pattern, "Locator expected to have accessible description", convertType(options, FrameExpectOptions.class), "Assert \"hasAccessibleDescription\""); + public void isHidden(IsHiddenOptions options) { + callMatcherAsStep("isHidden", state -> { + return toBeTruthy(state, "isHidden", actualLocator, "Locator", "hidden", "", (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.be.hidden", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void hasAccessibleErrorMessage(String errorMessage, HasAccessibleErrorMessageOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = errorMessage; - expected.ignoreCase = shouldIgnoreCase(options); - expected.normalizeWhiteSpace = true; - expectImpl("to.have.accessible.error.message", expected, errorMessage, "Locator expected to have accessible error message", convertType(options, FrameExpectOptions.class), "Assert \"hasAccessibleErrorMessage\""); + public void isVisible(IsVisibleOptions options) { + callMatcherAsStep("isVisible", state -> { + boolean visible = options == null || options.visible == null || options.visible; + String expected = visible ? "visible" : "hidden"; + String arg = visible ? "" : "{ visible: false }"; + return toBeTruthy(state, "isVisible", actualLocator, "Locator", expected, arg, (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect(visible ? "to.be.visible" : "to.be.hidden", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void hasAccessibleErrorMessage(Pattern pattern, HasAccessibleErrorMessageOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.ignoreCase = shouldIgnoreCase(options); - expected.normalizeWhiteSpace = true; - expectImpl("to.have.accessible.error.message", expected, pattern, "Locator expected to have accessible error message", convertType(options, FrameExpectOptions.class), "Assert \"hasAccessibleErrorMessage\""); + public void isInViewport(IsInViewportOptions options) { + callMatcherAsStep("isInViewport", state -> { + return toBeTruthy(state, "isInViewport", actualLocator, "Locator", "in viewport", "", (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.isNot = isNot; + expectOptions.expectedNumber = options == null ? null : options.ratio; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.be.in.viewport", expectOptions); + }, options == null ? null : options.timeout); + }); } @Override - public void hasAccessibleName(String name, HasAccessibleNameOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = name; - expected.ignoreCase = shouldIgnoreCase(options); - expected.normalizeWhiteSpace = true; - expectImpl("to.have.accessible.name", expected, name, "Locator expected to have accessible name", convertType(options, FrameExpectOptions.class), "Assert \"hasAccessibleName\""); + public void containsText(String expected, ContainsTextOptions options) { + toContainText(expected, options); } @Override - public void hasAccessibleName(Pattern pattern, HasAccessibleNameOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.ignoreCase = shouldIgnoreCase(options); - expected.normalizeWhiteSpace = true; - expectImpl("to.have.accessible.name", expected, pattern, "Locator expected to have accessible name", convertType(options, FrameExpectOptions.class), "Assert \"hasAccessibleName\""); + public void containsText(Pattern expected, ContainsTextOptions options) { + toContainText(expected, options); } @Override - public void hasAttribute(String name, String text, HasAttributeOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - expected.ignoreCase = shouldIgnoreCase(options); - hasAttribute(name, expected, text, options); + public void containsText(String[] expected, ContainsTextOptions options) { + toContainText(expected, options); } @Override - public void hasAttribute(String name, Pattern pattern, HasAttributeOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.ignoreCase = shouldIgnoreCase(options); - hasAttribute(name, expected, pattern, options); + public void containsText(Pattern[] expected, ContainsTextOptions options) { + toContainText(expected, options); } - private void hasAttribute(String name, ExpectedTextValue expectedText, Object expectedValue, HasAttributeOptions options) { - if (options == null) { - options = new HasAttributeOptions(); - } - FrameExpectOptions commonOptions = convertType(options, FrameExpectOptions.class); - commonOptions.expressionArg = name; - String message = "Locator expected to have attribute '" + name + "'"; - if (expectedValue instanceof Pattern) { - message += " matching regex"; - } - expectImpl("to.have.attribute.value", expectedText, expectedValue, message, commonOptions, "Assert \"hasAttribute\""); + private void toContainText(Object expected, ContainsTextOptions options) { + callMatcherAsStep("containsText", state -> { + if (expected instanceof Object[]) { + return toEqual(state, "containsText", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues((Object[]) expected, true, true, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.useInnerText = options == null ? null : options.useInnerText; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.contain.text.array", expectOptions); + }, expected, options == null ? null : options.timeout); + } else { + return toMatchText(state, "containsText", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, true, true, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.useInnerText = options == null ? null : options.useInnerText; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.text", expectOptions); + }, expected, options == null ? null : options.timeout, true); + } + }); } @Override - public void hasClass(String text, HasClassOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - expectImpl("to.have.class", expected, text, "Locator expected to have class", convertType(options, FrameExpectOptions.class), "Assert \"hasClass\""); + public void hasAccessibleDescription(String expected, HasAccessibleDescriptionOptions options) { + toHaveAccessibleDescription(expected, options); } @Override - public void hasClass(Pattern pattern, HasClassOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expectImpl("to.have.class", expected, pattern, "Locator expected to have class matching regex", convertType(options, FrameExpectOptions.class), "Assert \"hasClass\""); + public void hasAccessibleDescription(Pattern expected, HasAccessibleDescriptionOptions options) { + toHaveAccessibleDescription(expected, options); } - @Override - public void hasClass(String[] strings, HasClassOptions options) { - List list = new ArrayList<>(); - for (String text : strings) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - list.add(expected); - } - expectImpl("to.have.class.array", list, strings, "Locator expected to have class", convertType(options, FrameExpectOptions.class), "Assert \"hasClass\""); + private void toHaveAccessibleDescription(Object expected, HasAccessibleDescriptionOptions options) { + callMatcherAsStep("hasAccessibleDescription", state -> { + return toMatchText(state, "hasAccessibleDescription", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, true, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.accessible.description", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } @Override - public void hasClass(Pattern[] patterns, HasClassOptions options) { - List list = new ArrayList<>(); - for (Pattern pattern : patterns) { - ExpectedTextValue expected = expectedRegex(pattern); - list.add(expected); - } - expectImpl("to.have.class.array", list, patterns, "Locator expected to have class matching regex", convertType(options, FrameExpectOptions.class), "Assert \"hasClass\""); + public void hasAccessibleName(String expected, HasAccessibleNameOptions options) { + toHaveAccessibleName(expected, options); } @Override - public void hasCount(int count, HasCountOptions options) { - if (options == null) { - options = new HasCountOptions(); - } - FrameExpectOptions commonOptions = convertType(options, FrameExpectOptions.class); - commonOptions.expectedNumber = (double) count; - List expectedText = null; - expectImpl("to.have.count", expectedText, count, "Locator expected to have count", commonOptions, "Assert \"hasCount\""); + public void hasAccessibleName(Pattern expected, HasAccessibleNameOptions options) { + toHaveAccessibleName(expected, options); + } + + private void toHaveAccessibleName(Object expected, HasAccessibleNameOptions options) { + callMatcherAsStep("hasAccessibleName", state -> { + return toMatchText(state, "hasAccessibleName", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, true, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.accessible.name", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } @Override - public void hasCSS(String name, String value, HasCSSOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = value; - hasCSS(name, expected, value, options); + public void hasAccessibleErrorMessage(String expected, HasAccessibleErrorMessageOptions options) { + toHaveAccessibleErrorMessage(expected, options); } @Override - public void hasCSS(String name, Pattern pattern, HasCSSOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - hasCSS(name, expected, pattern, options); + public void hasAccessibleErrorMessage(Pattern expected, HasAccessibleErrorMessageOptions options) { + toHaveAccessibleErrorMessage(expected, options); } - private void hasCSS(String name, ExpectedTextValue expectedText, Object expectedValue, HasCSSOptions options) { - if (options == null) { - options = new HasCSSOptions(); - } - FrameExpectOptions commonOptions = convertType(options, FrameExpectOptions.class); - commonOptions.expressionArg = name; - String message = "Locator expected to have CSS property '" + name + "'"; - if (expectedValue instanceof Pattern) { - message += " matching regex"; - } - expectImpl("to.have.css", expectedText, expectedValue, message, commonOptions, "Assert \"hasCSS\""); + private void toHaveAccessibleErrorMessage(Object expected, HasAccessibleErrorMessageOptions options) { + callMatcherAsStep("hasAccessibleErrorMessage", state -> { + return toMatchText(state, "hasAccessibleErrorMessage", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, true, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.accessible.error.message", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } @Override - public void hasId(String id, HasIdOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = id; - expectImpl("to.have.id", expected, id, "Locator expected to have ID", convertType(options, FrameExpectOptions.class), "Assert \"hasId\""); + public void hasAttribute(String name, String expected, HasAttributeOptions options) { + toHaveAttribute(name, expected, options); } @Override - public void hasId(Pattern pattern, HasIdOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expectImpl("to.have.id", expected, pattern, "Locator expected to have ID matching regex", convertType(options, FrameExpectOptions.class), "Assert \"hasId\""); + public void hasAttribute(String name, Pattern expected, HasAttributeOptions options) { + toHaveAttribute(name, expected, options); } - @Override - public void hasJSProperty(String name, Object value, HasJSPropertyOptions options) { - if (options == null) { - options = new HasJSPropertyOptions(); - } - FrameExpectOptions commonOptions = convertType(options, FrameExpectOptions.class); - commonOptions.expressionArg = name; - commonOptions.expectedValue = serializeArgument(value); - List list = null; - expectImpl("to.have.property", list, value, "Locator expected to have JavaScript property '" + name + "'", commonOptions, "Assert \"hasJSProperty\""); + // java: the public API has no hasAttribute(name), so there is no toBeTruthy branch for a missing expected value. + private void toHaveAttribute(String name, Object expected, HasAttributeOptions options) { + callMatcherAsStep("hasAttribute", state -> { + return toMatchText(state, "hasAttribute", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, null, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expressionArg = name; + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.attribute.value", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } @Override - public void hasRole(AriaRole role, HasRoleOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = role.toString().toLowerCase(); - expectImpl("to.have.role", expected, expected.string, "Locator expected to have role", convertType(options, FrameExpectOptions.class), "Assert \"hasRole\""); + public void hasClass(String expected, HasClassOptions options) { + toHaveClass(expected, options); } @Override - public void hasText(String text, HasTextOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - expected.ignoreCase = shouldIgnoreCase(options); - expected.matchSubstring = false; - expected.normalizeWhiteSpace = true; - expectImpl("to.have.text", expected, text, "Locator expected to have text", convertType(options, FrameExpectOptions.class), "Assert \"hasText\""); + public void hasClass(Pattern expected, HasClassOptions options) { + toHaveClass(expected, options); } @Override - public void hasText(Pattern pattern, HasTextOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.ignoreCase = shouldIgnoreCase(options); - // Just match substring, same as containsText. - expected.matchSubstring = true; - expected.normalizeWhiteSpace = true; - expectImpl("to.have.text", expected, pattern, "Locator expected to have text matching regex", convertType(options, FrameExpectOptions.class), "Assert \"hasText\""); + public void hasClass(String[] expected, HasClassOptions options) { + toHaveClass(expected, options); } @Override - public void hasText(String[] strings, HasTextOptions options) { - List list = new ArrayList<>(); - for (String text : strings) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - expected.ignoreCase = shouldIgnoreCase(options); - expected.matchSubstring = false; - expected.normalizeWhiteSpace = true; - list.add(expected); - } - expectImpl("to.have.text.array", list, strings, "Locator expected to have text", convertType(options, FrameExpectOptions.class), "Assert \"hasText\""); + public void hasClass(Pattern[] expected, HasClassOptions options) { + toHaveClass(expected, options); } - @Override - public void hasText(Pattern[] patterns, HasTextOptions options) { - List list = new ArrayList<>(); - for (Pattern pattern : patterns) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.ignoreCase = shouldIgnoreCase(options); - expected.matchSubstring = true; - expected.normalizeWhiteSpace = true; - list.add(expected); - } - expectImpl("to.have.text.array", list, patterns, "Locator expected to have text matching regex", convertType(options, FrameExpectOptions.class), "Assert \"hasText\""); + private void toHaveClass(Object expected, HasClassOptions options) { + callMatcherAsStep("hasClass", state -> { + if (expected instanceof Object[]) { + return toEqual(state, "hasClass", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues((Object[]) expected, null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.class.array", expectOptions); + }, expected, options == null ? null : options.timeout); + } else { + return toMatchText(state, "hasClass", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.class", expectOptions); + }, expected, options == null ? null : options.timeout, false); + } + }); } @Override - public void hasValue(String value, HasValueOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = value; - expectImpl("to.have.value", expected, value, "Locator expected to have value", convertType(options, FrameExpectOptions.class), "Assert \"hasValue\""); + public void containsClass(String expected, ContainsClassOptions options) { + toContainClass(expected, options); } @Override - public void hasValue(Pattern pattern, HasValueOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expectImpl("to.have.value", expected, pattern, "Locator expected to have value matching regex", convertType(options, FrameExpectOptions.class), "Assert \"hasValue\""); + public void containsClass(List expected, ContainsClassOptions options) { + toContainClass(expected, options); } - @Override - public void hasValues(String[] values, HasValuesOptions options) { - List list = new ArrayList<>(); - for (String text : values) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = text; - list.add(expected); - } - expectImpl("to.have.values", list, values, "Locator expected to have values", convertType(options, FrameExpectOptions.class), "Assert \"hasValues\""); + // java: expected is a String or a List of String, so there is no check for a RegExp. + private void toContainClass(Object expected, ContainsClassOptions options) { + callMatcherAsStep("containsClass", state -> { + if (expected instanceof List) { + return toEqual(state, "containsClass", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(((List) expected).toArray(), null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.contain.class.array", expectOptions); + }, expected, options == null ? null : options.timeout); + } else { + return toMatchText(state, "containsClass", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.contain.class", expectOptions); + }, expected, options == null ? null : options.timeout, false); + } + }); } @Override - public void hasValues(Pattern[] patterns, HasValuesOptions options) { - List list = new ArrayList<>(); - for (Pattern pattern : patterns) { - ExpectedTextValue expected = expectedRegex(pattern); - expected.matchSubstring = true; - list.add(expected); - } - expectImpl("to.have.values", list, patterns, "Locator expected to have values matching regex", convertType(options, FrameExpectOptions.class), "Assert \"hasValues\""); + public void hasCount(int expected, HasCountOptions options) { + callMatcherAsStep("hasCount", state -> { + return toEqual(state, "hasCount", actualLocator, "Locator", (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedNumber = (double) expected; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.count", expectOptions); + }, expected, options == null ? null : options.timeout); + }); } @Override - public void matchesAriaSnapshot(String expected, MatchesAriaSnapshotOptions snapshotOptions) { - if (snapshotOptions == null) { - snapshotOptions = new MatchesAriaSnapshotOptions(); - } - FrameExpectOptions options = convertType(snapshotOptions, FrameExpectOptions.class); - options.expectedValue = serializeArgument(expected); - expectImpl("to.match.aria", options, expected,"Locator expected to match Aria snapshot", "Assert \"matchesAriaSnapshot\""); + public void hasCSS(String name, String expected, HasCSSOptions options) { + toHaveCSS(name, expected, options); } @Override - public void isChecked(IsCheckedOptions options) { - if (options == null) { - options = new IsCheckedOptions(); - } - - Map expectedValue = new HashMap<>(); - if (options.indeterminate != null) { - expectedValue.put("indeterminate", options.indeterminate); - } - if (options.checked != null) { - expectedValue.put("checked", options.checked); - } + public void hasCSS(String name, Pattern expected, HasCSSOptions options) { + toHaveCSS(name, expected, options); + } - String expected; - if (options.indeterminate != null && options.indeterminate) { - expected = "indeterminate"; - } else { - boolean unchecked = options.checked != null && !options.checked; - expected = unchecked ? "unchecked" : "checked"; - } + private void toHaveCSS(String name, Object expected, HasCSSOptions options) { + callMatcherAsStep("hasCSS", state -> { + return toMatchText(state, "hasCSS", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expressionArg = name; + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.pseudo = options == null ? null : options.pseudo; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.css", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); + } + + @Override + public void hasId(String expected, HasIdOptions options) { + toHaveId(expected, options); + } + + @Override + public void hasId(Pattern expected, HasIdOptions options) { + toHaveId(expected, options); + } - String message = "Locator expected to be"; - FrameExpectOptions expectOptions = convertType(options, FrameExpectOptions.class); - expectOptions.expectedValue = serializeArgument(expectedValue); - expectImpl("to.be.checked", expectOptions, expected, message, "Assert \"isChecked\""); + private void toHaveId(Object expected, HasIdOptions options) { + callMatcherAsStep("hasId", state -> { + return toMatchText(state, "hasId", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.id", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } @Override - public void isDisabled(IsDisabledOptions options) { - expectTrue("to.be.disabled", "Locator expected to be disabled", convertType(options, FrameExpectOptions.class), "Assert \"isDisabled\""); + public void hasJSProperty(String name, Object expected, HasJSPropertyOptions options) { + callMatcherAsStep("hasJSProperty", state -> { + return toEqual(state, "hasJSProperty", actualLocator, "Locator", (isNot, timeout) -> { + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expressionArg = name; + expectOptions.expectedValue = expected; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.property", expectOptions); + }, expected, options == null ? null : options.timeout); + }); } @Override - public void isEditable(IsEditableOptions options) { - FrameExpectOptions frameOptions = convertType(options, FrameExpectOptions.class); - boolean editable = options == null || options.editable == null || options.editable == true; - String message = "Locator expected to be " + (editable ? "editable" : "readonly"); - expectTrue(editable ? "to.be.editable" : "to.be.readonly", message, frameOptions, "Assert \"isEditable\""); + public void hasRole(AriaRole role, HasRoleOptions options) { + // java: the role is an enum, its string is the lower case name; there is no check that it is a string. + String expected = role.toString().toLowerCase(); + callMatcherAsStep("hasRole", state -> { + return toMatchText(state, "hasRole", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.role", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } @Override - public void isEmpty(IsEmptyOptions options) { - expectTrue("to.be.empty", "Locator expected to be empty", convertType(options, FrameExpectOptions.class), "Assert \"isEmpty\""); + public void hasText(String expected, HasTextOptions options) { + toHaveText(expected, options); } @Override - public void isEnabled(IsEnabledOptions options) { - FrameExpectOptions frameOptions = convertType(options, FrameExpectOptions.class); - boolean enabled = options == null || options.enabled == null || options.enabled == true; - String message = "Locator expected to be " + (enabled ? "enabled" : "disabled"); - expectTrue(enabled ? "to.be.enabled" : "to.be.disabled", message, frameOptions, "Assert \"isEnabled\""); + public void hasText(Pattern expected, HasTextOptions options) { + toHaveText(expected, options); } @Override - public void isFocused(IsFocusedOptions options) { - expectTrue("to.be.focused", "Locator expected to be focused", convertType(options, FrameExpectOptions.class), "Assert \"isFocused\""); + public void hasText(String[] expected, HasTextOptions options) { + toHaveText(expected, options); } @Override - public void isHidden(IsHiddenOptions options) { - expectTrue("to.be.hidden", "Locator expected to be hidden", convertType(options, FrameExpectOptions.class), "Assert \"isHidden\""); + public void hasText(Pattern[] expected, HasTextOptions options) { + toHaveText(expected, options); + } + + private void toHaveText(Object expected, HasTextOptions options) { + callMatcherAsStep("hasText", state -> { + if (expected instanceof Object[]) { + return toEqual(state, "hasText", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues((Object[]) expected, null, true, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.useInnerText = options == null ? null : options.useInnerText; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.text.array", expectOptions); + }, expected, options == null ? null : options.timeout); + } else { + return toMatchText(state, "hasText", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, true, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.useInnerText = options == null ? null : options.useInnerText; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.text", expectOptions); + }, expected, options == null ? null : options.timeout, false); + } + }); } @Override - public void isInViewport(IsInViewportOptions options) { - FrameExpectOptions expectOptions = convertType(options, FrameExpectOptions.class); - if (options != null && options.ratio != null) { - expectOptions.expectedNumber = options.ratio; - } - expectTrue("to.be.in.viewport", "Locator expected to be in viewport", expectOptions, "Assert \"isInViewport\""); + public void hasValue(String expected, HasValueOptions options) { + toHaveValue(expected, options); } @Override - public void isVisible(IsVisibleOptions options) { - FrameExpectOptions frameOptions = convertType(options, FrameExpectOptions.class); - boolean visible = options == null || options.visible == null || options.visible == true; - String message = "Locator expected to be " + (visible ? "visible" : "hidden"); - expectTrue(visible ? "to.be.visible" : "to.be.hidden", message, frameOptions, "Assert \"isVisible\""); + public void hasValue(Pattern expected, HasValueOptions options) { + toHaveValue(expected, options); } - private void expectTrue(String expression, String message, FrameExpectOptions options, String title) { - List expectedText = null; - expectImpl(expression, expectedText, null, message, options, title); + private void toHaveValue(Object expected, HasValueOptions options) { + callMatcherAsStep("hasValue", state -> { + return toMatchText(state, "hasValue", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.value", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } @Override - public LocatorAssertions not() { - return new LocatorAssertionsImpl(actualLocator, !isNot); + public void hasValues(String[] expected, HasValuesOptions options) { + toHaveValues(expected, options); } @Override - public void isAttached(IsAttachedOptions options) { - FrameExpectOptions frameOptions = convertType(options, FrameExpectOptions.class); - boolean attached = options == null || options.attached == null || options.attached == true; - String message = "Locator expected to be " + (attached ? "attached" : "detached"); - expectTrue(attached ? "to.be.attached" : "to.be.detached", message, frameOptions, "Assert \"isAttached\""); + public void hasValues(Pattern[] expected, HasValuesOptions options) { + toHaveValues(expected, options); + } + + private void toHaveValues(Object[] expected, HasValuesOptions options) { + callMatcherAsStep("hasValues", state -> { + return toEqual(state, "hasValues", actualLocator, "Locator", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(expected, null, null, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualLocator.expect("to.have.values", expectOptions); + }, expected, options == null ? null : options.timeout); + }); + } + + // java: toMatchAriaSnapshot of toMatchAriaSnapshot.ts. + @Override + public void matchesAriaSnapshot(String expected, MatchesAriaSnapshotOptions options) { + callMatcherAsStep("matchesAriaSnapshot", state -> toMatchAriaSnapshot(state, actualLocator, expected, options == null ? null : options.timeout)); } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java index 0bfca68f2..c3b519ddd 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java @@ -363,7 +363,8 @@ public String description() { if (!matcher.find()) return null; try { - return gson().fromJson(matcher.group(1), String.class); + String description = gson().fromJson(matcher.group(1), String.class); + return description == null || description.isEmpty() ? null : description; } catch (RuntimeException e) { return null; } @@ -420,16 +421,12 @@ public int count() { return frame.queryCount(selector); } - // java: sends sdkLanguage so that toString() prints the Java locator code the server returns. @Override public Locator normalize() { FrameResolveSelectorParams params = new FrameResolveSelectorParams(); params.selector = selector; - params.sdkLanguage = "java"; FrameResolveSelectorResult result = frame.channel.resolveSelector(params, NO_TIMEOUT); - LocatorImpl locator = new LocatorImpl(frame, result.resolvedSelector); - locator.stringRepresentation = result.locatorCode; - return locator; + return new LocatorImpl(frame, result.resolvedSelector); } @Override @@ -702,23 +699,25 @@ public void waitForFunction(String expression, Object arg, WaitForFunctionOption } // java: upstream options.title is the separate title parameter, as in FrameImpl.expect. - FrameExpectResult expect(String expression, FrameExpectOptions options, String title) { + FrameExpectResult expect(String expression, FrameExpectOptions options) { options.selector = selector; - return frame.expect(expression, options, title); + return frame.expect(expression, options); } // java: no _inspect, it serves inspect.custom. - // java: there is no port of the locator generators. The text of the old Java client is kept, - // tests pin it: the server's Java locator code after normalize(), the description, or Locator@. + // java: there is no port of the locator generators: the driver prints the Java locator code. When it cannot + // be asked (the connection is closed), the text is the description or Locator@. @Override public String toString() { - if (stringRepresentation != null) - return stringRepresentation; - String description = description(); - if (description != null) - return description; - return "Locator@" + selector; + try { + return frame.connection.localUtils().asLocatorDescription(selector); + } catch (PlaywrightException e) { + String description = description(); + if (description != null) + return description; + return "Locator@" + selector; + } } // java: the module-level _testIdAttributeName is SelectorsImpl.testIdAttributeName of the Playwright instance, @@ -754,8 +753,6 @@ static String cssObjectToString(Map style) { } // java: helpers and members with no upstream counterpart. - private String stringRepresentation; - private Frame.SelectOptionOptions frameSelectOptionOptions(SelectOptionOptions options) { if (options == null) options = new SelectOptionOptions(); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/MatcherHint.java b/playwright/src/main/java/com/microsoft/playwright/impl/MatcherHint.java new file mode 100644 index 000000000..6108bcfb6 --- /dev/null +++ b/playwright/src/main/java/com/microsoft/playwright/impl/MatcherHint.java @@ -0,0 +1,138 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Port of matcherHint.ts. +package com.microsoft.playwright.impl; + +import java.util.List; +import java.util.function.Supplier; +import java.util.regex.Matcher; + +import static com.microsoft.playwright.impl.MatcherUtils.numberToString; +import static com.microsoft.playwright.impl.MatcherUtils.printReceived; + +class MatcherHint { + // java: no attachments, suggestedRebaseline, softError and shouldNotRetryTest, they belong to the test runner. + static class MatcherResult { + String name; + Object expected; + Supplier message; + boolean pass; + Object actual; + List log; + Double timeout; + String ariaSnapshot; + } + + // java: no ExpectError, AssertionsBase throws an AssertionFailedError of opentest4j. + + // java: no expectTypes, the receiver of a matcher is typed. + + // java: InternalMatcherUtils is MatcherUtils, its methods are static; there are no colors. + + private static String printSubstring(String val) { + return val.replaceAll("\"|\\\\", "\\\\$0"); + } + + static String printReceivedStringContainExpectedSubstring(String received, int start, int length) { + return "\"" + + printSubstring(slice(received, 0, start)) + + printSubstring(slice(received, start, start + length)) + + printSubstring(slice(received, start + length, received.length())) + + "\""; + } + + // java: result is the Matcher after a successful find(), or null. + static String printReceivedStringContainExpectedResult(String received, Matcher result) { + return result == null + ? printReceived(received) + : printReceivedStringContainExpectedSubstring( + received, + result.start(), + result.group().length()); + } + + static class MatcherMessageDetails { + String promise; + boolean isNot; + String receiver; + String matcherName; + String expectation; + String locator; + String printedExpected; + String printedReceived; + String printedDiff; + Boolean timedOut; + Double timeout; + String errorMessage; + List log; + } + + static String formatMatcherMessage(MatcherMessageDetails details) { + String receiver = details.receiver != null ? details.receiver : (details.locator != null && !details.locator.isEmpty() ? "locator" : "page"); + // java: the call is the Java one: assertThat(locator).not().hasText(expected). + String message = "assertThat(" + receiver + + ")" + (details.promise != null && !details.promise.isEmpty() ? "." + details.promise : "") + (details.isNot ? ".not()" : "") + "." + + details.matcherName + + "(" + details.expectation + ")" + + " failed\n\n"; + + String[] diffLines = details.printedDiff == null ? null : details.printedDiff.split("\n", -1); + if (diffLines != null && diffLines.length == 2) { + details.printedExpected = diffLines[0]; + details.printedReceived = diffLines[1]; + details.printedDiff = null; + } + + boolean align = isEmpty(details.errorMessage) && details.printedExpected != null && details.printedExpected.startsWith("Expected:") + && (isEmpty(details.printedReceived) || details.printedReceived.startsWith("Received:")); + if (!isEmpty(details.locator)) + message += "Locator: " + (align ? " " : "") + details.locator + "\n"; + if (!isEmpty(details.printedExpected)) + message += details.printedExpected + "\n"; + if (!isEmpty(details.printedReceived)) + message += details.printedReceived + "\n"; + if (details.timedOut != null && details.timedOut && details.timeout != null && details.timeout != 0) + message += "Timeout: " + (align ? " " : "") + numberToString(details.timeout) + "ms\n"; + if (!isEmpty(details.printedDiff)) + message += details.printedDiff + "\n"; + if (!isEmpty(details.errorMessage)) { + message += details.errorMessage; + if (!details.errorMessage.endsWith("\n")) + message += "\n"; + } + message += callLogText(details.log); + return message; + } + + static String callLogText(List log) { + if (log == null || !log.stream().anyMatch(l -> !isEmpty(l))) + return ""; + return "\nCall log:\n" + (String.join("\n", log)) + "\n"; + } + + // java: helpers with no counterpart upstream: String.prototype.slice, and the truthiness of a string. + private static String slice(String string, int start, int end) { + int length = string.length(); + int from = start < 0 ? Math.max(length + start, 0) : Math.min(start, length); + int to = end < 0 ? Math.max(length + end, 0) : Math.min(end, length); + return from < to ? string.substring(from, to) : ""; + } + + private static boolean isEmpty(String string) { + return string == null || string.isEmpty(); + } +} diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/MatcherUtils.java b/playwright/src/main/java/com/microsoft/playwright/impl/MatcherUtils.java new file mode 100644 index 000000000..9abae2996 --- /dev/null +++ b/playwright/src/main/java/com/microsoft/playwright/impl/MatcherUtils.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.microsoft.playwright.impl; + +import java.lang.reflect.Array; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.regex.Pattern; + +import static com.microsoft.playwright.impl.Utils.toJsRegexFlags; + +// java: the utils of a matcher. Upstream they are jest-matcher-utils, which is not ported: these methods print the +// Java values in the same format, without colors, and there is no diff. +class MatcherUtils { + // java: the undefined value: Frame.expect returns it for a received value that is undefined. + static final Object UNDEFINED = new Object() { + @Override + public String toString() { + return "undefined"; + } + }; + + static String printExpected(Object value) { + return stringify(value); + } + + static String printReceived(Object value) { + return stringify(value); + } + + static String stringify(Object value) { + if (isArray(value)) { + List items = new ArrayList<>(); + for (Object item : toList(value)) + items.add(stringify(item)); + return "[" + String.join(", ", items) + "]"; + } + if (value instanceof Map) { + List entries = new ArrayList<>(); + for (Map.Entry entry : sortedEntries((Map) value).entrySet()) + entries.add(quote(entry.getKey()) + ": " + stringify(entry.getValue())); + return "{" + String.join(", ", entries) + "}"; + } + return formatBasicValue(value); + } + + // java: where upstream prints a diff (printDiffOrStringify of jest), both values are printed in full. + static String[] printExpectedAndReceived(Object expected, Object received, String expectedLabel, String receivedLabel) { + int width = Math.max(expectedLabel.length(), receivedLabel.length()); + String printedExpected = printExpected(expected); + String printedReceived = printReceived(received); + return new String[] { + pad(expectedLabel + ":", width + 2) + printedExpected, + pad(receivedLabel + ":", width + 2) + (printedExpected.equals(printedReceived) ? "serializes to the same string" : printedReceived) + }; + } + + private static Map sortedEntries(Map map) { + Map entries = new TreeMap<>(); + for (Map.Entry entry : map.entrySet()) + entries.put(String.valueOf(entry.getKey()), entry.getValue()); + return entries; + } + + private static String formatBasicValue(Object value) { + if (value == null) + return "null"; + if (value instanceof Number) + return numberToString(((Number) value).doubleValue()); + if (value instanceof String) + return quote((String) value); + if (value instanceof Pattern) + return "/" + ((Pattern) value).pattern() + "/" + toJsRegexFlags((Pattern) value); + if (value instanceof Date) + return DateTimeFormatter.ISO_INSTANT.format(((Date) value).toInstant().truncatedTo(ChronoUnit.MILLIS)).replaceFirst("(:\\d\\d)Z$", "$1.000Z"); + return String.valueOf(value); + } + + static String numberToString(double value) { + if (Double.isNaN(value)) + return "NaN"; + if (Double.isInfinite(value)) + return value > 0 ? "Infinity" : "-Infinity"; + if (value == Math.rint(value) && Math.abs(value) < 1e15) + return String.valueOf((long) value); + return Double.toString(value); + } + + private static String quote(String value) { + return "\"" + value.replaceAll("\"|\\\\", "\\\\$0") + "\""; + } + + private static boolean isArray(Object value) { + return value instanceof List || value != null && value.getClass().isArray(); + } + + @SuppressWarnings("unchecked") + private static List toList(Object value) { + if (value instanceof List) + return (List) value; + List result = new ArrayList<>(); + for (int i = 0; i < Array.getLength(value); i++) + result.add(Array.get(value, i)); + return result; + } + + private static String pad(String value, int width) { + StringBuilder result = new StringBuilder(value); + while (result.length() < width) + result.append(' '); + return result.toString(); + } + +} diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/PageAssertionsImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/PageAssertionsImpl.java index 97e986886..07e1a1579 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/PageAssertionsImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/PageAssertionsImpl.java @@ -14,17 +14,22 @@ * limitations under the License. */ +// Port of matchers.ts. package com.microsoft.playwright.impl; -import com.microsoft.playwright.impl.protocol.*; import com.microsoft.playwright.Page; import com.microsoft.playwright.assertions.PageAssertions; +import com.microsoft.playwright.impl.protocol.ExpectedTextValue; +import java.util.List; import java.util.regex.Pattern; +import static com.microsoft.playwright.impl.LocatorAssertionsImpl.serializeExpectedTextValues; +import static com.microsoft.playwright.impl.ToMatchAriaSnapshot.toMatchAriaSnapshot; +import static com.microsoft.playwright.impl.ToMatchText.toMatchText; import static com.microsoft.playwright.impl.UrlMatcher.constructURLBasedOnBaseURL; -import static com.microsoft.playwright.impl.Utils.convertType; +// java: the matchers of a Page, see LocatorAssertionsImpl. public class PageAssertionsImpl extends AssertionsBase implements PageAssertions { private final PageImpl actualPage; @@ -38,55 +43,64 @@ private PageAssertionsImpl(Page page, boolean isNot) { } @Override - FrameExpectResult doExpect(String expression, FrameExpectOptions expectOptions, String title) { - FrameImpl frame = (FrameImpl) actualPage.mainFrame(); - return frame.expect(expression, expectOptions, title); + public PageAssertions not() { + return new PageAssertionsImpl(actualPage, !isNot); } @Override - public void hasTitle(String title, HasTitleOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - expected.string = title; - expected.normalizeWhiteSpace = true; - expectImpl("to.have.title", expected, title, "Page title expected to be", convertType(options, FrameExpectOptions.class), "Assert \"hasTitle\""); + public void hasTitle(String expected, HasTitleOptions options) { + toHaveTitle(expected, options); } @Override - public void hasTitle(Pattern pattern, HasTitleOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expectImpl("to.have.title", expected, pattern, "Page title expected to match regex", convertType(options, FrameExpectOptions.class), "Assert \"hasTitle\""); + public void hasTitle(Pattern expected, HasTitleOptions options) { + toHaveTitle(expected, options); } - @Override - public void hasURL(String url, HasURLOptions options) { - ExpectedTextValue expected = new ExpectedTextValue(); - if (actualPage.context().baseUrl() != null) { - url = constructURLBasedOnBaseURL(actualPage.context().baseUrl().toString(), url); - } - expected.string = url; - expected.ignoreCase = shouldIgnoreCase(options); - expectImpl("to.have.url", expected, url, "Page URL expected to be", convertType(options, FrameExpectOptions.class), "Assert \"hasURL\""); + private void toHaveTitle(Object expected, HasTitleOptions options) { + callMatcherAsStep("hasTitle", state -> { + return toMatchText(state, "hasTitle", actualPage, "Page", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, true, null); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualPage.mainFrame().expect("to.have.title", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } @Override - public void hasURL(Pattern pattern, HasURLOptions options) { - ExpectedTextValue expected = expectedRegex(pattern); - expectImpl("to.have.url", expected, pattern, "Page URL expected to match regex", convertType(options, FrameExpectOptions.class), "Assert \"hasURL\""); + public void hasURL(String expected, HasURLOptions options) { + toHaveURL(expected, options); } @Override - public void matchesAriaSnapshot(String expected, MatchesAriaSnapshotOptions snapshotOptions) { - if (snapshotOptions == null) { - snapshotOptions = new MatchesAriaSnapshotOptions(); - } - FrameExpectOptions options = convertType(snapshotOptions, FrameExpectOptions.class); - options.expectedValue = Serialization.serializeArgument(expected); - expectImpl("to.match.aria", options, expected, "Page expected to match Aria snapshot", "Assert \"matchesAriaSnapshot\""); + public void hasURL(Pattern expected, HasURLOptions options) { + toHaveURL(expected, options); + } + + // java: the public API has no URLPattern and no predicate, so toHaveURLWithPredicate of toHaveURL.ts is not ported. + private void toHaveURL(Object expectedParam, HasURLOptions options) { + callMatcherAsStep("hasURL", state -> { + String baseURL = actualPage.context().options.baseURL; + Object expected = expectedParam instanceof String ? constructURLBasedOnBaseURL(baseURL, (String) expectedParam) : expectedParam; + return toMatchText(state, "hasURL", actualPage, "Page", (isNot, timeout) -> { + List expectedText = serializeExpectedTextValues(new Object[] {expected}, null, null, options == null ? null : options.ignoreCase); + FrameExpectOptions expectOptions = new FrameExpectOptions(); + expectOptions.expectedText = expectedText; + expectOptions.isNot = isNot; + expectOptions.timeout = timeout; + expectOptions.title = state.title; + return actualPage.mainFrame().expect("to.have.url", expectOptions); + }, expected, options == null ? null : options.timeout, false); + }); } + // java: toMatchAriaSnapshot of toMatchAriaSnapshot.ts. @Override - public PageAssertions not() { - return new PageAssertionsImpl(actualPage, !isNot); + public void matchesAriaSnapshot(String expected, MatchesAriaSnapshotOptions options) { + callMatcherAsStep("matchesAriaSnapshot", state -> toMatchAriaSnapshot(state, actualPage, expected, options == null ? null : options.timeout)); } } - diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ToBeTruthy.java b/playwright/src/main/java/com/microsoft/playwright/impl/ToBeTruthy.java new file mode 100644 index 000000000..e86837f97 --- /dev/null +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ToBeTruthy.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Port of toBeTruthy.ts. +package com.microsoft.playwright.impl; + +import com.microsoft.playwright.impl.AssertionsBase.ExpectMatcherState; +import com.microsoft.playwright.impl.AssertionsBase.Query; +import com.microsoft.playwright.impl.MatcherHint.MatcherMessageDetails; +import com.microsoft.playwright.impl.MatcherHint.MatcherResult; + +import java.util.List; + +import static com.microsoft.playwright.impl.MatcherHint.formatMatcherMessage; + +class ToBeTruthy { + // java: the options are their timeout, there is no AbortSignal. + static MatcherResult toBeTruthy( + ExpectMatcherState state, + String matcherName, + LocatorImpl locator, + String receiverType, + String expected, + String arg, + Query query, + Double optionsTimeout) { + double timeout = optionsTimeout != null ? optionsTimeout : state.timeout; + + FrameExpectResult queryResult = query.query(state.isNot, timeout); + boolean pass = queryResult.matches; + List log = queryResult.log; + Boolean timedOut = queryResult.timedOut; + FrameExpectResult.ExpectReceived received = queryResult.received; + String errorMessage = queryResult.errorMessage; + Object receivedValue = received == null ? MatcherUtils.UNDEFINED : received.value; + + if (pass == !state.isNot) { + MatcherResult result = new MatcherResult(); + result.name = matcherName; + result.message = () -> ""; + result.pass = pass; + result.expected = expected; + return result; + } + + String printedReceived; + String printedExpected; + if (pass) { + printedExpected = "Expected: not " + expected; + printedReceived = errorMessage != null ? "" : "Received: " + expected; + } else { + printedExpected = "Expected: " + expected; + printedReceived = errorMessage != null ? "" : "Received: " + receivedValue; + } + MatcherResult result = new MatcherResult(); + result.message = () -> { + MatcherMessageDetails details = new MatcherMessageDetails(); + details.isNot = state.isNot; + details.promise = state.promise; + details.matcherName = matcherName; + details.expectation = arg; + details.locator = locator.toString(); + details.timeout = timeout; + details.timedOut = timedOut; + details.printedExpected = printedExpected; + details.printedReceived = printedReceived; + details.errorMessage = errorMessage; + details.log = log; + return formatMatcherMessage(details); + }; + result.pass = pass; + result.actual = receivedValue; + result.name = matcherName; + result.expected = expected; + result.log = log; + result.timeout = timedOut != null && timedOut ? timeout : null; + result.ariaSnapshot = received == null ? null : received.ariaSnapshot; + return result; + } +} diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ToEqual.java b/playwright/src/main/java/com/microsoft/playwright/impl/ToEqual.java new file mode 100644 index 000000000..8a6e5b9fd --- /dev/null +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ToEqual.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Port of toEqual.ts. +package com.microsoft.playwright.impl; + +import com.microsoft.playwright.impl.AssertionsBase.ExpectMatcherState; +import com.microsoft.playwright.impl.AssertionsBase.Query; +import com.microsoft.playwright.impl.MatcherHint.MatcherMessageDetails; +import com.microsoft.playwright.impl.MatcherHint.MatcherResult; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; + +import static com.microsoft.playwright.impl.MatcherUtils.printExpectedAndReceived; +import static com.microsoft.playwright.impl.MatcherUtils.printExpected; +import static com.microsoft.playwright.impl.MatcherUtils.printReceived; +import static com.microsoft.playwright.impl.MatcherHint.formatMatcherMessage; + +class ToEqual { + private static final String EXPECTED_LABEL = "Expected"; + private static final String RECEIVED_LABEL = "Received"; + + // java: the options are their timeout, contains is not read upstream and there is no AbortSignal. + static MatcherResult toEqual( + ExpectMatcherState state, + String matcherName, + LocatorImpl locator, + String receiverType, + Query query, + Object expected, + Double optionsTimeout) { + double timeout = optionsTimeout != null ? optionsTimeout : state.timeout; + + FrameExpectResult queryResult = query.query(state.isNot, timeout); + boolean pass = queryResult.matches; + FrameExpectResult.ExpectReceived received = queryResult.received; + List log = queryResult.log; + Boolean timedOut = queryResult.timedOut; + String errorMessage = queryResult.errorMessage; + Object receivedValue = received == null ? MatcherUtils.UNDEFINED : received.value; + + if (pass == !state.isNot) { + MatcherResult result = new MatcherResult(); + result.name = matcherName; + result.message = () -> ""; + result.pass = pass; + result.expected = expected; + return result; + } + + String printedReceived = null; + String printedExpected = null; + String printedDiff = null; + if (pass) { + printedExpected = "Expected: not " + printExpected(expected); + printedReceived = errorMessage != null ? "" : "Received: " + printReceived(receivedValue); + } else if (errorMessage != null) { + printedExpected = "Expected: " + printExpected(expected); + } else if (isArray(expected) && isArray(receivedValue)) { + List expectedList = toList(expected); + List receivedList = toList(receivedValue); + List normalizedExpected = new ArrayList<>(); + for (int index = 0; index < expectedList.size(); index++) { + Object exp = expectedList.get(index); + Object rec = index < receivedList.size() ? receivedList.get(index) : null; + if (exp instanceof Pattern) { + // java: RegExp.test() converts its argument to a string, a missing element is "undefined". + normalizedExpected.add(((Pattern) exp).matcher(rec == null ? "undefined" : String.valueOf(rec)).find() ? rec : exp); + continue; + } + + normalizedExpected.add(exp); + } + String[] printed = printExpectedAndReceived( + normalizedExpected, + receivedValue, + EXPECTED_LABEL, + RECEIVED_LABEL); + printedExpected = printed[0]; + printedReceived = printed[1]; + } else { + String[] printed = printExpectedAndReceived( + expected, + receivedValue, + EXPECTED_LABEL, + RECEIVED_LABEL); + printedExpected = printed[0]; + printedReceived = printed[1]; + } + String finalPrintedExpected = printedExpected; + String finalPrintedReceived = printedReceived; + String finalPrintedDiff = printedDiff; + MatcherResult result = new MatcherResult(); + result.message = () -> { + MatcherMessageDetails details = new MatcherMessageDetails(); + details.isNot = state.isNot; + details.promise = state.promise; + details.matcherName = matcherName; + details.expectation = "expected"; + details.locator = locator.toString(); + details.timeout = timeout; + details.timedOut = timedOut; + details.printedExpected = finalPrintedExpected; + details.printedReceived = finalPrintedReceived; + details.printedDiff = finalPrintedDiff; + details.errorMessage = errorMessage; + details.log = log; + return formatMatcherMessage(details); + }; + + result.actual = receivedValue; + result.expected = expected; + result.name = matcherName; + result.pass = pass; + result.log = log; + result.timeout = timedOut != null && timedOut ? timeout : null; + result.ariaSnapshot = received == null ? null : received.ariaSnapshot; + return result; + } + + // java: helpers with no counterpart upstream: a JS array is a List or a Java array. + private static boolean isArray(Object value) { + return value instanceof List || value instanceof Object[]; + } + + @SuppressWarnings("unchecked") + private static List toList(Object value) { + return value instanceof List ? (List) value : Arrays.asList((Object[]) value); + } +} diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ToMatchAriaSnapshot.java b/playwright/src/main/java/com/microsoft/playwright/impl/ToMatchAriaSnapshot.java new file mode 100644 index 000000000..36bee245b --- /dev/null +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ToMatchAriaSnapshot.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Port of toMatchAriaSnapshot.ts. +package com.microsoft.playwright.impl; + +import com.microsoft.playwright.impl.AssertionsBase.ExpectMatcherState; +import com.microsoft.playwright.impl.MatcherHint.MatcherMessageDetails; +import com.microsoft.playwright.impl.MatcherHint.MatcherResult; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static com.microsoft.playwright.impl.MatcherUtils.printExpectedAndReceived; +import static com.microsoft.playwright.impl.MatcherUtils.printExpected; +import static com.microsoft.playwright.impl.MatcherHint.formatMatcherMessage; +import static com.microsoft.playwright.impl.MatcherHint.printReceivedStringContainExpectedSubstring; + +class ToMatchAriaSnapshot { + // java: the receiver is a LocatorImpl or a PageImpl, expected is the snapshot text, the options are their timeout. + @SuppressWarnings("unchecked") + static MatcherResult toMatchAriaSnapshot( + ExpectMatcherState state, + Object receiver, + String expectedParam, + Double optionsTimeout) { + String matcherName = "matchesAriaSnapshot"; + LocatorImpl locator = receiver instanceof PageImpl ? null : (LocatorImpl) receiver; + + // java: there is no testInfo: no ignoreSnapshots, no updateSnapshots, no snapshot file, no baseline + // generation and no global children option. + String expected; + double timeout; + expected = expectedParam; + timeout = optionsTimeout != null ? optionsTimeout : state.timeout; + + expected = unshift(expected); + + FrameExpectOptions expectParams = new FrameExpectOptions(); + expectParams.expectedValue = expected; + expectParams.isNot = state.isNot; + expectParams.timeout = timeout; + expectParams.title = state.title; + FrameExpectResult queryResult = locator != null ? + locator.expect("to.match.aria", expectParams) : + ((PageImpl) receiver).mainFrame().expect("to.match.aria", expectParams); + boolean pass = queryResult.matches; + FrameExpectResult.ExpectReceived received = queryResult.received; + List log = queryResult.log; + Boolean timedOut = queryResult.timedOut; + String errorMessage = queryResult.errorMessage; + // java: MatcherReceived is the parsed object, a Map with raw and regex. + Map typedReceived = received == null || received.value == MatcherUtils.UNDEFINED ? null : (Map) received.value; + + String finalExpected = expected; + MatcherResult result = new MatcherResult(); + result.message = () -> { + String printedExpected = null; + String printedReceived = null; + String printedDiff = null; + if (errorMessage != null) { + printedExpected = "Expected: " + (state.isNot ? "not " : "") + printExpected(finalExpected); + } else if (pass) { + String raw = (String) typedReceived.get("raw"); + String receivedString = printReceivedStringContainExpectedSubstring(raw, raw.indexOf(finalExpected), finalExpected.length()); + printedExpected = "Expected: not " + printExpected(finalExpected); + printedReceived = "Received: " + receivedString; + } else { + String[] printed = printExpectedAndReceived(finalExpected, typedReceived.get("raw"), "Expected", "Received"); + printedExpected = printed[0]; + printedReceived = printed[1]; + } + MatcherMessageDetails details = new MatcherMessageDetails(); + details.isNot = state.isNot; + details.promise = state.promise; + details.matcherName = matcherName; + details.expectation = "expected"; + details.locator = locator == null ? null : locator.toString(); + details.timeout = timeout; + details.timedOut = timedOut; + details.printedExpected = printedExpected; + details.printedReceived = printedReceived; + details.printedDiff = printedDiff; + details.errorMessage = errorMessage; + details.log = log; + return formatMatcherMessage(details); + }; + + if (errorMessage != null) { + result.pass = state.isNot; + result.name = "matchesAriaSnapshot"; + result.expected = expected; + return result; + } + + result.name = matcherName; + result.expected = expected; + result.pass = pass; + result.actual = typedReceived == null ? null : typedReceived.get("raw"); + result.log = log; + result.timeout = timedOut != null && timedOut ? timeout : null; + return result; + } + + private static String unshift(String snapshot) { + String[] lines = snapshot.split("\n", -1); + int whitespacePrefixLength = 100; + for (String line : lines) { + if (line.trim().isEmpty()) + continue; + int length = 0; + while (length < line.length() && Character.isWhitespace(line.charAt(length))) + length++; + if (length < whitespacePrefixLength) + whitespacePrefixLength = length; + } + List result = new ArrayList<>(); + for (String line : lines) { + if (line.trim().isEmpty()) + continue; + result.add(line.substring(Math.min(whitespacePrefixLength, line.length()))); + } + return String.join("\n", result); + } + + // java: no indent, it formats the suggested rebaseline. +} diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ToMatchText.java b/playwright/src/main/java/com/microsoft/playwright/impl/ToMatchText.java new file mode 100644 index 000000000..31f1c157c --- /dev/null +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ToMatchText.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Port of toMatchText.ts. +package com.microsoft.playwright.impl; + +import com.microsoft.playwright.PlaywrightException; +import com.microsoft.playwright.impl.AssertionsBase.ExpectMatcherState; +import com.microsoft.playwright.impl.AssertionsBase.Query; +import com.microsoft.playwright.impl.MatcherHint.MatcherMessageDetails; +import com.microsoft.playwright.impl.MatcherHint.MatcherResult; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static com.microsoft.playwright.impl.MatcherUtils.printExpectedAndReceived; +import static com.microsoft.playwright.impl.MatcherUtils.printExpected; +import static com.microsoft.playwright.impl.MatcherHint.formatMatcherMessage; +import static com.microsoft.playwright.impl.MatcherHint.printReceivedStringContainExpectedResult; +import static com.microsoft.playwright.impl.MatcherHint.printReceivedStringContainExpectedSubstring; + +class ToMatchText { + // java: the receiver is a LocatorImpl or a PageImpl, expected is a String or a Pattern; the options are their + // timeout and matchSubstring, there is no AbortSignal. + static MatcherResult toMatchText( + ExpectMatcherState state, + String matcherName, + Object receiver, + String receiverType, + Query query, + Object expected, + Double optionsTimeout, + boolean matchSubstring) { + LocatorImpl locator = "Locator".equals(receiverType) ? (LocatorImpl) receiver : null; + + if ( + !(expected instanceof String) && + !(expected instanceof Pattern) + ) { + String errorMessage = "Error: expected value must be a string or regular expression\n" + "Expected has value: " + printExpected(expected); + MatcherMessageDetails details = new MatcherMessageDetails(); + details.promise = state.promise; + details.isNot = state.isNot; + details.locator = locator == null ? null : locator.toString(); + details.matcherName = matcherName; + details.expectation = "expected"; + details.errorMessage = errorMessage; + throw new PlaywrightException(formatMatcherMessage(details)); + } + + double timeout = optionsTimeout != null ? optionsTimeout : state.timeout; + + FrameExpectResult queryResult = query.query(state.isNot, timeout); + boolean pass = queryResult.matches; + FrameExpectResult.ExpectReceived received = queryResult.received; + List log = queryResult.log; + Boolean timedOut = queryResult.timedOut; + String errorMessage = queryResult.errorMessage; + + if (pass == !state.isNot) { + MatcherResult result = new MatcherResult(); + result.name = matcherName; + result.message = () -> ""; + result.pass = pass; + result.expected = expected; + return result; + } + + String expectedSuffix = expected instanceof String ? (matchSubstring ? " substring" : "") : " pattern"; + String receivedSuffix = expected instanceof String ? (matchSubstring ? " string" : "") : " string"; + // java: a received value that is not a string keeps its text. + String receivedValue = received == null || received.value == null || received.value == MatcherUtils.UNDEFINED ? null : String.valueOf(received.value); + String receivedString = receivedValue == null ? "" : receivedValue; + String printedReceived = null; + String printedExpected = null; + String printedDiff = null; + if (pass) { + if (expected instanceof String) { + printedExpected = "Expected" + expectedSuffix + ": not " + printExpected(expected); + if (errorMessage == null) { + String formattedReceived = printReceivedStringContainExpectedSubstring(receivedString, receivedString.indexOf((String) expected), ((String) expected).length()); + printedReceived = "Received" + receivedSuffix + ": " + formattedReceived; + } + } else { + printedExpected = "Expected" + expectedSuffix + ": not " + printExpected(expected); + if (errorMessage == null) { + Matcher matcher = ((Pattern) expected).matcher(receivedString); + String formattedReceived = printReceivedStringContainExpectedResult(receivedString, matcher.find() ? matcher : null); + printedReceived = "Received" + receivedSuffix + ": " + formattedReceived; + } + } + } else { + if (errorMessage != null) + printedExpected = "Expected" + expectedSuffix + ": " + printExpected(expected); + else { + String[] printed = printExpectedAndReceived(expected, receivedString, "Expected" + expectedSuffix, "Received" + receivedSuffix); + printedExpected = printed[0]; + printedReceived = printed[1]; + } + } + + String finalPrintedExpected = printedExpected; + String finalPrintedReceived = printedReceived; + String finalPrintedDiff = printedDiff; + MatcherResult result = new MatcherResult(); + result.message = () -> { + MatcherMessageDetails details = new MatcherMessageDetails(); + details.promise = state.promise; + details.isNot = state.isNot; + details.matcherName = matcherName; + details.expectation = "expected"; + details.locator = locator == null ? null : locator.toString(); + details.timeout = timeout; + details.timedOut = timedOut; + details.printedExpected = finalPrintedExpected; + details.printedReceived = finalPrintedReceived; + details.printedDiff = finalPrintedDiff; + details.log = log; + details.errorMessage = errorMessage; + return formatMatcherMessage(details); + }; + + result.name = matcherName; + result.expected = expected; + result.pass = pass; + result.actual = receivedValue; + result.log = log; + result.timeout = timedOut != null && timedOut ? timeout : null; + result.ariaSnapshot = received == null ? null : received.ariaSnapshot; + return result; + } +} diff --git a/playwright/src/test/AGENTS.md b/playwright/src/test/AGENTS.md index 2744cdbd7..c482b265e 100644 --- a/playwright/src/test/AGENTS.md +++ b/playwright/src/test/AGENTS.md @@ -153,6 +153,13 @@ handler of the request has returned, as in the Node.js server, so a response sto `assertTrue(a instanceof X)`. A matcher on a locator, page or response (`await expect(locator).toHaveText(...)`) is the same matcher of the Java API: `assertThat(locator).hasText(...)` (`PlaywrightAssertions.assertThat`), `.not.` is `.not()`. + The text of a matcher failure is translated the same way: `expect(locator).not.toHaveText(expected) failed` + is `assertThat(locator).not().hasText(expected) failed`, and `Expect "not toHaveText"` in a call + log is `Assert "not hasText"`. A locator in that text is Java code: `locator('div')` is + `locator("div")`. The upstream test config sets the expect timeout to 10000ms, the Java default is + 5000ms: `with timeout 10000ms` of a matcher without a timeout is `with timeout 5000ms`. + The Java client prints no diff: where upstream pins `- expected` and `+ received` diff lines, the Java + test pins the same content in the `Expected:` and `Received:` values. - `await expect(promise).rejects.toThrow('text')` and `const error = await promise.catch(e => e); expect(error.message).toContain('text')` are `PlaywrightException error = assertThrows(PlaywrightException.class, () -> ...)` then diff --git a/playwright/src/test/java/com/microsoft/playwright/TestAssertThatIsInViewport.java b/playwright/src/test/java/com/microsoft/playwright/TestAssertThatIsInViewport.java index cb631f72f..fe13518d4 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestAssertThatIsInViewport.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestAssertThatIsInViewport.java @@ -58,7 +58,7 @@ void shouldRespectRatioOption(Page page) { void shouldHaveGoodStack(Page page) { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("body")).not().isInViewport(new LocatorAssertions.IsInViewportOptions().setTimeout(100))); assertNotNull(error); - assertTrue(error.getMessage().contains("Locator expected not to be in viewport"), error.getMessage()); + assertTrue(error.getMessage().contains("assertThat(locator).not().isInViewport() failed"), error.getMessage()); } @PlaywrightTest diff --git a/playwright/src/test/java/com/microsoft/playwright/TestAssertionsDefaultTimeout.java b/playwright/src/test/java/com/microsoft/playwright/TestAssertionsDefaultTimeout.java new file mode 100644 index 000000000..4b219554d --- /dev/null +++ b/playwright/src/test/java/com/microsoft/playwright/TestAssertionsDefaultTimeout.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.microsoft.playwright; + +import com.microsoft.playwright.assertions.PlaywrightAssertions; +import com.microsoft.playwright.fixtures.PlaywrightTest; +import org.junit.jupiter.api.parallel.Isolated; +import org.opentest4j.AssertionFailedError; + +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +// The default assertion timeout is global, so these tests do not run in parallel with any other test. +@Isolated +public class TestAssertionsDefaultTimeout { + @PlaywrightTest + void defaultTimeoutHasTextFail(Page page) { + page.setContent("
"); + Locator locator = page.locator("div"); + PlaywrightAssertions.setDefaultAssertionTimeout(1000); + AssertionFailedError exception = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasText("foo")); + assertTrue(exception.getMessage().contains("Assert \"hasText\" locator(\"div\") with timeout 1000ms"), exception.getMessage()); + // Restore default. + PlaywrightAssertions.setDefaultAssertionTimeout(5_000); + } + + @PlaywrightTest + void defaultTimeoutHasTextPass(Page page) { + page.setContent("
foo
"); + Locator locator = page.locator("div"); + PlaywrightAssertions.setDefaultAssertionTimeout(1000); + assertThat(locator).hasText("foo"); + // Restore default. + PlaywrightAssertions.setDefaultAssertionTimeout(5_000); + } + + @PlaywrightTest + void defaultTimeoutZeroHasTextPass(Page page) { + page.setContent("
foo
"); + Locator locator = page.locator("div"); + PlaywrightAssertions.setDefaultAssertionTimeout(0); + assertThat(locator).hasText("foo"); + // Restore default. + PlaywrightAssertions.setDefaultAssertionTimeout(5_000); + } +} diff --git a/playwright/src/test/java/com/microsoft/playwright/TestExpectMisc.java b/playwright/src/test/java/com/microsoft/playwright/TestExpectMisc.java index d348c046b..41d13c5b3 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestExpectMisc.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestExpectMisc.java @@ -30,7 +30,7 @@ void strictModeViolationErrorFormat(Page page) { page.setContent("
hello
hi
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("div")).isVisible()); - assertTrue(error.getMessage().contains("Locator expected to be visible"), error.getMessage()); + assertTrue(error.getMessage().contains("assertThat(locator).isVisible() failed"), error.getMessage()); assertTrue(error.getMessage().contains("Error: strict mode violation: locator(\"div\") resolved to 2 elements:"), error.getMessage()); } @@ -39,7 +39,7 @@ void invalidSelectorErrorFormat(Page page) { page.setContent("
hello
hi
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("##")).isVisible()); - assertTrue(error.getMessage().contains("Locator expected to be visible"), error.getMessage()); + assertTrue(error.getMessage().contains("assertThat(locator).isVisible() failed"), error.getMessage()); assertTrue(error.getMessage().contains("Error: Unexpected token \"#\" while parsing css selector \"##\"."), error.getMessage()); } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java b/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java index 204701aea..f2e85eae0 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java @@ -18,7 +18,6 @@ import com.microsoft.playwright.assertions.LocatorAssertions; import com.microsoft.playwright.assertions.LocatorAssertions.ContainsClassOptions; -import com.microsoft.playwright.assertions.PlaywrightAssertions; import com.microsoft.playwright.fixtures.PlaywrightTest; import org.opentest4j.AssertionFailedError; @@ -69,7 +68,7 @@ void containsTextWRegexFail(Page page) { }); assertEquals("ex2", e.getExpected().getStringRepresentation()); assertEquals("Text content", e.getActual().getValue()); - assertTrue(e.getMessage().contains("Locator expected to contain regex"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).containsText(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -116,7 +115,7 @@ void hasTextWRegexFail(Page page) { }); assertEquals("Text 2", e.getExpected().getStringRepresentation()); assertEquals("Text content", e.getActual().getValue()); - assertTrue(e.getMessage().contains("Locator expected to have text matching regex"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasText(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -141,7 +140,7 @@ void hasTextWTextFail(Page page) { }); assertEquals("Text", e.getExpected().getStringRepresentation()); assertEquals("Text content", e.getActual().getValue()); - assertTrue(e.getMessage().contains("Locator expected to have text"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasText(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -195,7 +194,7 @@ void hasTextWTextArrayFailOnNotEmpty(Page page) { }); assertEquals("[]", e.getExpected().getStringRepresentation()); assertEquals("[]", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected not to have text"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).not().hasText(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -222,8 +221,8 @@ void hasTextWTextArrayFail(Page page) { }); assertEquals("[Text 1, Text 3, Extra]", e.getExpected().getStringRepresentation()); assertEquals("[Text 1, Text 3]", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have text\nExpected: [Text 1, Text 3, Extra]"), e.getMessage()); - assertTrue(e.getMessage().contains("Received: [Text 1, Text 3]"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasText(expected) failed"), e.getMessage()); + assertTrue(e.getMessage().contains("Expected: [\"Text 1\", \"Text 3\", \"Extra\"]"), e.getMessage()); } @PlaywrightTest @@ -244,7 +243,7 @@ void hasTextWRegExArrayFail(Page page) { }); assertEquals("[Text 1, Text \\d, Extra]", e.getExpected().getStringRepresentation()); assertEquals("[Text 1, Text 3]", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have text"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasText(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -271,7 +270,7 @@ void hasAttributeTextFail(Page page) { }); assertEquals("foo", e.getExpected().getStringRepresentation()); assertEquals("node", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have attribute 'id'\nExpected: foo\nReceived: node"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasAttribute(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -290,7 +289,7 @@ void hasAttributeRegExpFail(Page page) { }); assertEquals(".Nod..", e.getExpected().getStringRepresentation()); assertEquals("node", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have attribute 'id' matching regex\nExpected: .Nod..\nReceived: node"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasAttribute(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -309,7 +308,7 @@ void hasClassTextFail(Page page) { }); assertEquals("foo bar baz", e.getExpected().getStringRepresentation()); assertEquals("bar baz", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have class"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasClass(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -328,7 +327,7 @@ void hasClassRegExpFail(Page page) { }); assertEquals("foo Z.*", e.getExpected().getStringRepresentation()); assertEquals("bar baz", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have class matching regex"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasClass(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -347,7 +346,7 @@ void hasClassTextArrayFail(Page page) { }); assertEquals("[foo, bar, missing]", e.getExpected().getStringRepresentation()); assertEquals("[foo, bar, baz]", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have class"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasClass(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -366,7 +365,7 @@ void hasClassRegExpArrayFail(Page page) { }); assertEquals("[fo.*, .ar, baz, extra]", e.getExpected().getStringRepresentation()); assertEquals("[foo, bar, baz]", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have class matching regex"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasClass(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -385,7 +384,7 @@ void hasCountFail(Page page) { }); assertEquals("1", e.getExpected().getStringRepresentation()); assertEquals("2", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have count"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasCount(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -412,7 +411,7 @@ void hasCSSFail(Page page) { }); assertEquals("red", e.getExpected().getStringRepresentation()); assertEquals("rgb(255, 0, 0)", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have CSS property 'color'"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasCSS(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -431,7 +430,7 @@ void hasCSSRegExFail(Page page) { }); assertEquals("red", e.getExpected().getStringRepresentation()); assertEquals("rgb(255, 0, 0)", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have CSS property 'color' matching regex"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasCSS(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -450,7 +449,7 @@ void hasIdFail(Page page) { }); assertEquals("foo", e.getExpected().getStringRepresentation()); assertEquals("node", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have ID"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasId(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -480,7 +479,7 @@ void hasJSPropertyNumberFail(Page page) { }); assertEquals("1", e.getExpected().getStringRepresentation()); assertEquals("2021", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have JavaScript property 'foo'"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasJSProperty(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -493,7 +492,7 @@ void hasJSPropertyObjectFail(Page page) { }); assertEquals("{a=2}", e.getExpected().getStringRepresentation()); assertEquals("{a=1, b=string}", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have JavaScript property 'foo'"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasJSProperty(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -505,7 +504,7 @@ void hasJSPropertyStringFail(Page page) { }); assertEquals("foo", e.getExpected().getStringRepresentation()); assertEquals("node", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have JavaScript property 'id'"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasJSProperty(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -526,7 +525,7 @@ void hasValueTextFail(Page page) { }); assertEquals("Text2", e.getExpected().getStringRepresentation()); assertEquals("Text content", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have value"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasValue(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -555,7 +554,7 @@ void hasValueRegExpFail(Page page) { }); assertEquals("Text2", e.getExpected().getStringRepresentation()); assertEquals("Text content", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have value matching regex"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasValue(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -596,7 +595,7 @@ void hasValuesExactMatchWithText(Page page) { }); assertEquals("[R, G]", e.getExpected().getStringRepresentation()); assertEquals("[RR, GG]", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have values"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasValues(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -625,7 +624,7 @@ void hasValuesFailsWhenItemsNotSelected(Page page) { }); assertEquals("[R, G]", e.getExpected().getStringRepresentation()); assertEquals("[B]", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have values matching regex"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).hasValues(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -669,7 +668,7 @@ void isCheckedFail(Page page) { }); assertEquals("checked", e.getExpected().getStringRepresentation()); assertEquals("unchecked", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to be\nExpected: checked"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).isChecked() failed"), e.getMessage()); } @PlaywrightTest @@ -682,7 +681,7 @@ void notIsCheckedFail(Page page) { assertEquals("checked", e.getExpected().getStringRepresentation()); assertEquals("checked", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected not to be\nExpected: checked"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(locator).not().isChecked() failed"), e.getMessage()); } @PlaywrightTest @@ -698,7 +697,7 @@ void isCheckedFalseFail(Page page) { Locator locator = page.locator("input"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).isChecked(new LocatorAssertions.IsCheckedOptions().setChecked(false).setTimeout(1000))); - assertTrue(error.getMessage().contains("Locator expected to be\nExpected: unchecked"), error.getMessage()); + assertTrue(error.getMessage().contains("assertThat(locator).isChecked({ checked: false }) failed"), error.getMessage()); } @PlaywrightTest @@ -715,9 +714,8 @@ void isDisabledFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).isDisabled(new LocatorAssertions.IsDisabledOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected to be disabled"), e.getMessage()); + assertEquals("disabled", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).isDisabled() failed"), e.getMessage()); } @PlaywrightTest @@ -727,9 +725,8 @@ void notIsDisabledFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).not().isDisabled(new LocatorAssertions.IsDisabledOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected not to be disabled"), e.getMessage()); + assertEquals("disabled", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).not().isDisabled() failed"), e.getMessage()); } @PlaywrightTest @@ -746,9 +743,8 @@ void isEditableFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).isEditable(new LocatorAssertions.IsEditableOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected to be editable"), e.getMessage()); + assertEquals("editable", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).isEditable() failed"), e.getMessage()); } @PlaywrightTest @@ -757,7 +753,7 @@ void isEditableFalseFail(Page page) { Locator locator = page.locator("input"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).isEditable(new LocatorAssertions.IsEditableOptions().setEditable(false).setTimeout(1000))); - assertTrue(error.getMessage().contains("Locator expected to be readonly"), error.getMessage()); + assertTrue(error.getMessage().contains("assertThat(locator).isEditable({ editable: false }) failed"), error.getMessage()); } @PlaywrightTest @@ -767,9 +763,8 @@ void notIsEditableFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).not().isEditable(new LocatorAssertions.IsEditableOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected not to be editable"), e.getMessage()); + assertEquals("editable", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).not().isEditable() failed"), e.getMessage()); } @PlaywrightTest @@ -822,9 +817,8 @@ void isEmptyFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).isEmpty(new LocatorAssertions.IsEmptyOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected to be empty"), e.getMessage()); + assertEquals("empty", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).isEmpty() failed"), e.getMessage()); } @PlaywrightTest @@ -834,9 +828,8 @@ void notIsEmptyFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).not().isEmpty(new LocatorAssertions.IsEmptyOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected not to be empty"), e.getMessage()); + assertEquals("empty", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).not().isEmpty() failed"), e.getMessage()); } @PlaywrightTest @@ -853,9 +846,8 @@ void isEnabledFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).isEnabled(new LocatorAssertions.IsEnabledOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected to be enabled"), e.getMessage()); + assertEquals("enabled", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).isEnabled() failed"), e.getMessage()); } @PlaywrightTest @@ -865,9 +857,8 @@ void notIsEnabledFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).not().isEnabled(new LocatorAssertions.IsEnabledOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected not to be enabled"), e.getMessage()); + assertEquals("enabled", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).not().isEnabled() failed"), e.getMessage()); } @PlaywrightTest @@ -890,7 +881,7 @@ void isEnabledFalseFail(Page page) { Locator locator = page.locator("button"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).isEnabled(new LocatorAssertions.IsEnabledOptions().setEnabled(false).setTimeout(1000))); - assertTrue(error.getMessage().contains("Locator expected to be disabled"), error.getMessage()); + assertTrue(error.getMessage().contains("assertThat(locator).isEnabled({ enabled: false }) failed"), error.getMessage()); } @PlaywrightTest @@ -935,9 +926,8 @@ void isFocusedFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).isFocused(new LocatorAssertions.IsFocusedOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected to be focused"), e.getMessage()); + assertEquals("focused", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).isFocused() failed"), e.getMessage()); } @PlaywrightTest @@ -948,9 +938,8 @@ void notIsFocusedFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).not().isFocused(new LocatorAssertions.IsFocusedOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected not to be focused"), e.getMessage()); + assertEquals("focused", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).not().isFocused() failed"), e.getMessage()); } @PlaywrightTest @@ -967,9 +956,8 @@ void isHiddenFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).isHidden(new LocatorAssertions.IsHiddenOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected to be hidden"), e.getMessage()); + assertEquals("hidden", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).isHidden() failed"), e.getMessage()); } @PlaywrightTest @@ -979,9 +967,8 @@ void notIsHiddenFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).not().isHidden(new LocatorAssertions.IsHiddenOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected not to be hidden"), e.getMessage()); + assertEquals("hidden", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).not().isHidden() failed"), e.getMessage()); } @PlaywrightTest @@ -998,9 +985,8 @@ void isVisibleFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).isVisible(new LocatorAssertions.IsVisibleOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected to be visible"), e.getMessage()); + assertEquals("visible", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).isVisible() failed"), e.getMessage()); } @PlaywrightTest @@ -1009,7 +995,7 @@ void isVisibleFalseFail(Page page) { Locator locator = page.locator("input"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).isVisible(new LocatorAssertions.IsVisibleOptions().setVisible(false).setTimeout(1000))); - assertTrue(error.getMessage().contains("Locator expected to be hidden"), error.getMessage()); + assertTrue(error.getMessage().contains("assertThat(locator).isVisible({ visible: false }) failed"), error.getMessage()); } @PlaywrightTest @@ -1019,9 +1005,8 @@ void notIsVisibleFail(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> { assertThat(locator).not().isVisible(new LocatorAssertions.IsVisibleOptions().setTimeout(1000)); }); - assertNull(e.getExpected()); - assertNull(e.getActual()); - assertTrue(e.getMessage().contains("Locator expected not to be visible"), e.getMessage()); + assertEquals("visible", e.getExpected().getValue()); + assertTrue(e.getMessage().contains("assertThat(locator).not().isVisible() failed"), e.getMessage()); } @PlaywrightTest @@ -1072,37 +1057,6 @@ void locatorCountShouldWorkWithDeletedMapInMainWorld(Page page) { assertThat(page.locator("#searchResultTableDiv .x-grid3-row")).hasCount(0); } - @PlaywrightTest - void defaultTimeoutHasTextFail(Page page) { - page.setContent("
"); - Locator locator = page.locator("div"); - PlaywrightAssertions.setDefaultAssertionTimeout(1000); - AssertionFailedError exception = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasText("foo")); - assertTrue(exception.getMessage().contains("Assert \"hasText\" locator(\"div\") with timeout 1000ms"), exception.getMessage()); - // Restore default. - PlaywrightAssertions.setDefaultAssertionTimeout(5_000); - } - - @PlaywrightTest - void defaultTimeoutHasTextPass(Page page) { - page.setContent("
foo
"); - Locator locator = page.locator("div"); - PlaywrightAssertions.setDefaultAssertionTimeout(1000); - assertThat(locator).hasText("foo"); - // Restore default. - PlaywrightAssertions.setDefaultAssertionTimeout(5_000); - } - - @PlaywrightTest - void defaultTimeoutZeroHasTextPass(Page page) { - page.setContent("
foo
"); - Locator locator = page.locator("div"); - PlaywrightAssertions.setDefaultAssertionTimeout(0); - assertThat(locator).hasText("foo"); - // Restore default. - PlaywrightAssertions.setDefaultAssertionTimeout(5_000); - } - @PlaywrightTest void containsClassPass(Page page) { page.setContent("
"); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestLocatorConvenience.java b/playwright/src/test/java/com/microsoft/playwright/TestLocatorConvenience.java index 15ccf26cd..dd77ae5c9 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestLocatorConvenience.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestLocatorConvenience.java @@ -16,6 +16,8 @@ package com.microsoft.playwright; +import com.microsoft.playwright.fixtures.BrowserName; +import com.microsoft.playwright.fixtures.Env; import com.microsoft.playwright.fixtures.PlaywrightTest; import static java.util.Arrays.asList; @@ -30,10 +32,10 @@ void shouldHaveANicePreview(Page page, Server server) { Locator check = page.locator("#check"); JSHandle text = inner.evaluateHandle("e => e.firstChild"); page.evaluate("() => 1"); // Give them a chance to calculate the preview. - assertEquals("Locator@#outer", outer.toString()); - assertEquals("Locator@#outer >> #inner", inner.toString()); + assertEquals("locator(\"#outer\")", outer.toString()); + assertEquals("locator(\"#outer\").locator(\"#inner\")", inner.toString()); assertEquals("JSHandle@#text=Text,↵more text", text.toString()); - assertEquals("Locator@#check", check.toString()); + assertEquals("locator(\"#check\")", check.toString()); } @PlaywrightTest @@ -226,7 +228,7 @@ void descriptionShouldReturnLastDescriptionForMultipleCalls(Page page) { void toStringShouldReturnFormattedLocator(Page page) { page.setContent("
Hello
"); Locator locator = page.locator("div"); - assertTrue(locator.toString().startsWith("Locator@")); + assertEquals("locator(\"div\")", locator.toString()); } @PlaywrightTest @@ -235,4 +237,19 @@ void toStringShouldPreferDescription(Page page) { Locator locator = page.locator("div").describe("my div"); assertEquals("my div", locator.toString()); } + + @PlaywrightTest + void toStringShouldNotThrowWhenTheConnectionIsClosed(BrowserName browserName) { + Locator locator; + Locator described; + try (Playwright playwright = Playwright.create(); + Browser browser = browserName.browserType(playwright).launch(Env.launchOptions()); + Page page = browser.newPage()) { + locator = page.locator("div"); + described = page.locator("div").describe("my div"); + assertEquals("locator(\"div\")", locator.toString()); + } + assertEquals("Locator@div", locator.toString()); + assertEquals("my div", described.toString()); + } } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageAriaSnapshot.java b/playwright/src/test/java/com/microsoft/playwright/TestPageAriaSnapshot.java index a41cce6a0..a5e0d06dc 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageAriaSnapshot.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageAriaSnapshot.java @@ -344,7 +344,7 @@ void pageMatchesAriaSnapshot(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("- heading \"world\"", new com.microsoft.playwright.assertions.PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - org.junit.jupiter.api.Assertions.assertTrue(e.getMessage().contains("Page expected to match Aria snapshot"), e.getMessage()); + org.junit.jupiter.api.Assertions.assertTrue(e.getMessage().contains("assertThat(page).matchesAriaSnapshot(expected) failed"), e.getMessage()); } } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestPageAssertions.java b/playwright/src/test/java/com/microsoft/playwright/TestPageAssertions.java index 786e44196..0658929cf 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestPageAssertions.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestPageAssertions.java @@ -40,7 +40,7 @@ void hasURLTextFail(Page page) { }); assertEquals("foo", e.getExpected().getValue()); assertEquals("data:text/html,
B
", e.getActual().getValue()); - assertTrue(e.getMessage().contains("Page URL expected to be"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(page).hasURL(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -78,7 +78,7 @@ void hasURLRegexFail(Page page, Server server) { }); assertEquals(".*foo.*", e.getExpected().getStringRepresentation()); assertEquals(server.EMPTY_PAGE, e.getActual().getValue()); - assertTrue(e.getMessage().contains("Page URL expected to match regex"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(page).hasURL(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -107,7 +107,7 @@ void hasTitleTextFail(Page page, Server server) { }); assertEquals("foo", e.getExpected().getValue()); assertEquals("Woof-Woof", e.getActual().getValue()); - assertTrue(e.getMessage().contains("Page title expected to be\nExpected: foo\nReceived: Woof-Woof"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(page).hasTitle(expected) failed"), e.getMessage()); } @PlaywrightTest @@ -124,7 +124,7 @@ void hasTitleRegexFail(Page page, Server server) { }); assertEquals("^foo[AB]", e.getExpected().getStringRepresentation()); assertEquals("Woof-Woof", e.getActual().getValue()); - assertTrue(e.getMessage().contains("Page title expected to match regex\nExpected: ^foo[AB]\nReceived: Woof-Woof"), e.getMessage()); + assertTrue(e.getMessage().contains("assertThat(page).hasTitle(expected) failed"), e.getMessage()); } @PlaywrightTest diff --git a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectBoolean.java b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectBoolean.java index 5614609f4..f55859850 100644 --- a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectBoolean.java +++ b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectBoolean.java @@ -87,19 +87,18 @@ void withIndeterminateTrueAndChecked(Page page) { @PlaywrightTest @DisplayName("EX-006 fail") - @Fixme("java: the message is the Java assertion format, 'Locator expected to be\\nExpected: checked\\nReceived: unchecked\\n\\nCall log:\\n - Assert \"isChecked\" locator(\"input\") with timeout 1000ms ...', not the 'expect(locator).toBeChecked() failed' message of the Node.js client") void fail(Page page) { page.setContent(""); Locator locator = page.locator("input"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).isChecked(new LocatorAssertions.IsCheckedOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toBeChecked() failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).isChecked() failed\n" + "\n" + - "Locator: locator('input')\n" + + "Locator: locator(\"input\")\n" + "Expected: checked\n" + "Received: unchecked\n" + "Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toBeChecked\" locator('input') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"isChecked\" locator(\"input\") with timeout 1000ms")); } @PlaywrightTest @@ -120,75 +119,71 @@ void withNotAndCheckedFalse(Page page) { @PlaywrightTest @DisplayName("EX-009 fail with not") - @Fixme("java: the message is the Java assertion format, 'Locator expected to be\\nExpected: checked\\nReceived: unchecked\\n\\nCall log:\\n - Assert \"isChecked\" locator(\"input\") with timeout 1000ms ...', not the 'expect(locator).not.toBeChecked() failed' message of the Node.js client") void failWithNot(Page page) { page.setContent(""); Locator locator = page.locator("input"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).not().isChecked(new LocatorAssertions.IsCheckedOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).not.toBeChecked() failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).not().isChecked() failed\n" + "\n" + - "Locator: locator('input')\n" + + "Locator: locator(\"input\")\n" + "Expected: not checked\n" + "Received: checked\n" + "Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"not toBeChecked\" locator('input') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"not isChecked\" locator(\"input\") with timeout 1000ms")); assertTrue(stripAnsi(error.getMessage()).contains("locator resolved to ")); } @PlaywrightTest @DisplayName("EX-010 fail with checked:false") - @Fixme("java: the message is the Java assertion format, 'Locator expected to be\\nExpected: checked\\nReceived: unchecked\\n\\nCall log:\\n - Assert \"isChecked\" locator(\"input\") with timeout 1000ms ...', not the 'expect(locator).toBeChecked({ checked: false }) failed' message of the Node.js client") void failWithCheckedFalse(Page page) { page.setContent(""); Locator locator = page.locator("input"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).isChecked(new LocatorAssertions.IsCheckedOptions().setChecked(false).setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toBeChecked({ checked: false }) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).isChecked({ checked: false }) failed\n" + "\n" + - "Locator: locator('input')\n" + + "Locator: locator(\"input\")\n" + "Expected: unchecked\n" + "Received: checked\n" + "Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toBeChecked\" locator('input') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"isChecked\" locator(\"input\") with timeout 1000ms")); assertTrue(stripAnsi(error.getMessage()).contains("locator resolved to ")); } @PlaywrightTest @DisplayName("EX-011 fail with indeterminate: true") - @Fixme("java: the message is the Java assertion format, 'Locator expected to be\\nExpected: checked\\nReceived: unchecked\\n\\nCall log:\\n - Assert \"isChecked\" locator(\"input\") with timeout 1000ms ...', not the 'expect(locator).toBeChecked({ indeterminate: true }) failed' message of the Node.js client") void failWithIndeterminateTrue(Page page) { page.setContent(""); Locator locator = page.locator("input"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).isChecked(new LocatorAssertions.IsCheckedOptions().setIndeterminate(true).setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toBeChecked({ indeterminate: true }) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).isChecked({ indeterminate: true }) failed\n" + "\n" + - "Locator: locator('input')\n" + + "Locator: locator(\"input\")\n" + "Expected: indeterminate\n" + "Received: unchecked\n" + "Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toBeChecked\" locator('input') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"isChecked\" locator(\"input\") with timeout 1000ms")); } @PlaywrightTest @DisplayName("EX-012 fail missing") - @Fixme("java: the message is the Java assertion format, 'Locator expected to be\\nExpected: checked\\nReceived: unchecked\\n\\nCall log:\\n - Assert \"isChecked\" locator(\"input\") with timeout 1000ms ...', not the 'expect(locator).not.toBeChecked() failed' message of the Node.js client") void failMissing(Page page) { page.setContent("
no inputs here
"); Locator locator2 = page.locator("input2"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator2).not().isChecked(new LocatorAssertions.IsCheckedOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).not.toBeChecked() failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).not().isChecked() failed\n" + "\n" + - "Locator: locator('input2')\n" + + "Locator: locator(\"input2\")\n" + "Expected: not checked\n" + "Timeout: 1000ms\n" + "Error: element(s) not found\n" + "\n" + "Call log:\n" + - " - Expect \"not toBeChecked\" locator('input2') with timeout 1000ms\n" + - " - waiting for locator('input2')\n")); + " - Assert \"not isChecked\" locator(\"input2\") with timeout 1000ms\n" + + " - waiting for locator(\"input2\")\n")); } @PlaywrightTest @@ -605,21 +600,20 @@ void failWithNot(Page page) { @PlaywrightTest @DisplayName("EX-059 fail with not when nothing matching") - @Fixme("java: the message is 'Locator expected not to be hidden\\nError: element(s) not found\\nCall log:\\n - Assert \"isHidden\" locator(\"button\") with timeout 1000ms ...', not the 'expect(locator).not.toBeHidden() failed' message of the Node.js client") void failWithNotWhenNothingMatching(Page page) { page.setContent("
"); Locator locator = page.locator("button"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).not().isHidden(new LocatorAssertions.IsHiddenOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).not.toBeHidden() failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).not().isHidden() failed\n" + "\n" + - "Locator: locator('button')\n" + + "Locator: locator(\"button\")\n" + "Expected: not hidden\n" + "Timeout: 1000ms\n" + "Error: element(s) not found\n" + "\n" + "Call log:\n" + - " - Expect \"not toBeHidden\" locator('button') with timeout 1000ms\n")); + " - Assert \"not isHidden\" locator(\"button\") with timeout 1000ms\n")); } @PlaywrightTest @@ -717,11 +711,10 @@ void beforeEach(TestServer server) { @PlaywrightTest @DisplayName("EX-070 text content type") - @Fixme("java: the message is 'Response status expected to be within [200..299] range, was 404\\nCall log:\\n→ GET ...', without the 'expect(response).toBeOK() failed' line of the Node.js client") void textContentType(Page page, TestServer server) { APIResponse res = page.request().get(server.PREFIX + "/text-content-type"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(res).isOK()); - assertTrue(stripAnsi(error.getMessage()).contains("expect(response).toBeOK() failed")); + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(response).isOK() failed")); assertTrue(error.getMessage().contains("→ GET " + server.PREFIX + "/text-content-type")); assertTrue(error.getMessage().contains("← 404 Not Found")); assertTrue(stripAnsi(error.getMessage()).contains("Response text:\nText error")); diff --git a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectMisc.java b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectMisc.java index bfafaabd2..4b5412799 100644 --- a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectMisc.java +++ b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectMisc.java @@ -99,36 +99,34 @@ void eventuallyPassNotNonZero(Page page) { @PlaywrightTest @DisplayName("EX-186 fail zero") - @Fixme("java: the assertion error has another format: 'Locator expected to have count\nExpected: 0\nReceived: 1\n\nCall log:\n- Assert \"hasCount\" locator(\"span\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failZero(Page page) { page.setContent("
"); Locator locator = page.locator("span"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasCount(0, new LocatorAssertions.HasCountOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveCount(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasCount(expected) failed\n" + "\n" + - "Locator: locator('span')\n" + + "Locator: locator(\"span\")\n" + "Expected: 0\n" + "Received: 1\n" + "Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveCount\" locator('span') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasCount\" locator(\"span\") with timeout 1000ms")); } @PlaywrightTest @DisplayName("EX-187 fail zero 2") - @Fixme("java: the assertion error has another format: 'Locator expected not to have count\nExpected: 1\nReceived: 1\n\nCall log:\n- Assert \"hasCount\" locator(\"span\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failZero2(Page page) { page.setContent("
"); Locator locator = page.locator("span"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).not().hasCount(1, new LocatorAssertions.HasCountOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).not.toHaveCount(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).not().hasCount(expected) failed\n" + "\n" + - "Locator: locator('span')\n" + + "Locator: locator(\"span\")\n" + "Expected: not 1\n" + "Received: 1\n" + "Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"not toHaveCount\" locator('span') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"not hasCount\" locator(\"span\") with timeout 1000ms")); } } @@ -145,14 +143,13 @@ void pass(Page page) { @PlaywrightTest @DisplayName("EX-189 fail") - @Fixme("java: the assertion error has another format: 'Locator expected to have JavaScript property 'foo'\nExpected: {a=1, b=string, c=Wed Jul 28 13:26:32 PDT 2021}\nReceived: {a=1, b=string, c=Wed Jul 28 13:26:32 PDT 2021}\n\nCall log:\n- Assert \"hasJSProperty\" locator(\"div\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
"); page.evalOnSelector("div", "e => e.foo = { a: 1, b: 'string', c: new Date(1627503992000) }"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasJSProperty("foo", mapOf("a", 1, "b", "string", "c", new Date(1627503992001L)), new LocatorAssertions.HasJSPropertyOptions().setTimeout(1000))); - assertTrue(error.getMessage().contains("- \"c\"")); + assertTrue(error.getMessage().contains("\"c\": 2021-07-28T20:26:32.001Z}")); } @PlaywrightTest @@ -166,20 +163,19 @@ void passString(Page page) { @PlaywrightTest @DisplayName("EX-191 fail string") - @Fixme("java: the assertion error has another format: 'Locator expected to have JavaScript property 'foo'\nExpected: error\nReceived: string\n\nCall log:\n- Assert \"hasJSProperty\" locator(\"div\") with timeout 200ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failString(Page page) { page.setContent("
"); page.evalOnSelector("div", "e => e.foo = 'string'"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasJSProperty("foo", "error", new LocatorAssertions.HasJSPropertyOptions().setTimeout(200))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveJSProperty(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasJSProperty(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: \"error\"\n" + "Received: \"string\"\n" + "Timeout: 200ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveJSProperty\" locator('div') with timeout 200ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasJSProperty\" locator(\"div\") with timeout 200ms")); } @PlaywrightTest @@ -193,20 +189,19 @@ void passNumber(Page page) { @PlaywrightTest @DisplayName("EX-193 fail number") - @Fixme("java: the assertion error has another format: 'Locator expected to have JavaScript property 'foo'\nExpected: 1\nReceived: 2021\n\nCall log:\n- Assert \"hasJSProperty\" locator(\"div\") with timeout 200ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failNumber(Page page) { page.setContent("
"); page.evalOnSelector("div", "e => e.foo = 2021"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasJSProperty("foo", 1, new LocatorAssertions.HasJSPropertyOptions().setTimeout(200))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveJSProperty(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasJSProperty(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: 1\n" + "Received: 2021\n" + "Timeout: 200ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveJSProperty\" locator('div') with timeout 200ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasJSProperty\" locator(\"div\") with timeout 200ms")); } @PlaywrightTest @@ -220,20 +215,19 @@ void passBoolean(Page page) { @PlaywrightTest @DisplayName("EX-195 fail boolean") - @Fixme("java: the assertion error has another format: 'Locator expected to have JavaScript property 'foo'\nExpected: true\nReceived: false\n\nCall log:\n- Assert \"hasJSProperty\" locator(\"div\") with timeout 200ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failBoolean(Page page) { page.setContent("
"); page.evalOnSelector("div", "e => e.foo = false"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasJSProperty("foo", true, new LocatorAssertions.HasJSPropertyOptions().setTimeout(200))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveJSProperty(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasJSProperty(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: true\n" + "Received: false\n" + "Timeout: 200ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveJSProperty\" locator('div') with timeout 200ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasJSProperty\" locator(\"div\") with timeout 200ms")); } @PlaywrightTest @@ -247,20 +241,19 @@ void passBoolean2(Page page) { @PlaywrightTest @DisplayName("EX-197 fail boolean 2") - @Fixme("java: the assertion error has another format: 'Locator expected to have JavaScript property 'foo'\nExpected: true\nReceived: false\n\nCall log:\n- Assert \"hasJSProperty\" locator(\"div\") with timeout 200ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failBoolean2(Page page) { page.setContent("
"); page.evalOnSelector("div", "e => e.foo = false"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasJSProperty("foo", true, new LocatorAssertions.HasJSPropertyOptions().setTimeout(200))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveJSProperty(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasJSProperty(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: true\n" + "Received: false\n" + "Timeout: 200ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveJSProperty\" locator('div') with timeout 200ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasJSProperty\" locator(\"div\") with timeout 200ms")); } @PlaywrightTest @@ -296,7 +289,6 @@ void passNested(Page page) { @PlaywrightTest @DisplayName("EX-201 fail nested") - @Fixme("java: the assertion error has another format: 'Locator expected to have JavaScript property 'foo.bar'\nExpected: {a=1, b=string, c=Wed Jul 28 13:26:32 PDT 2021}\nReceived: null\n\nCall log:\n- Assert \"hasJSProperty\" locator(\"div\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failNested(Page page) { page.setContent("
"); page.evalOnSelector("div", "e => e.foo = { nested: { a: 1, b: 'string', c: new Date(1627503992000) } }"); @@ -331,21 +323,20 @@ void passWithSVGs(Page page) { @PlaywrightTest @DisplayName("EX-204 fail") - @Fixme("java: the assertion error has another format: 'Locator expected to have class\nExpected: foo bar baz\nReceived: bar baz\n\nCall log:\n- Assert \"hasClass\" locator(\"div\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasClass("foo bar baz", new LocatorAssertions.HasClassOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveClass(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasClass(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: \"foo bar baz\"\n" + "Received: \"bar baz\"\n" + "Timeout: 1000ms\n" + "\n" + "Call log:\n")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveClass\" locator('div') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasClass\" locator(\"div\") with timeout 1000ms")); } // EX-205 pass with array: not ported, see upstream_not_ported.txt. @@ -366,36 +357,34 @@ void passWithSVGs(Page page) { @PlaywrightTest @DisplayName("EX-209 fail") - @Fixme("java: the assertion error has another format: 'Locator expected to contain class\nExpected: does-not-exist\nReceived: bar baz\n\nCall log:\n- Assert \"containsClass\" locator(\"div\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).containsClass("does-not-exist", new LocatorAssertions.ContainsClassOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toContainClass(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).containsClass(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: \"does-not-exist\"\n" + "Received: \"bar baz\"\n" + "Timeout: 1000ms\n" + "\n" + "Call log:\n")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toContainClass\" locator('div') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"containsClass\" locator(\"div\") with timeout 1000ms")); } // EX-210 pass with array: not ported, see upstream_not_ported.txt. @PlaywrightTest @DisplayName("EX-211 fail with array") - @Fixme("java: the assertion error has another format: 'Locator expected to contain classes\nExpected: [foo, bar, baz]\nReceived: [foo, bar, bar]\n\nCall log:\n- Assert \"containsClass\" locator(\"div\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failWithArray(Page page) { page.setContent("
"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).containsClass(Arrays.asList("foo", "bar", "baz"), new LocatorAssertions.ContainsClassOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toContainClass(expected) failed")); - assertTrue(stripAnsi(error.getMessage()).contains("Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toContainClass\" locator('div') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).containsClass(expected) failed")); + assertTrue(stripAnsi(error.getMessage()).contains("Timeout: 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"containsClass\" locator(\"div\") with timeout 1000ms")); } } @@ -410,19 +399,18 @@ void pass(Page page) { @PlaywrightTest @DisplayName("EX-213 fail") - @Fixme("java: the assertion error has another format: 'Page title expected to be\nExpected: Hello\nReceived: Bye\n\nCall log:\n- Assert \"hasTitle\" with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("Bye"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).hasTitle("Hello", new PageAssertions.HasTitleOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(page).toHaveTitle(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(page).hasTitle(expected) failed\n" + "\n" + "Expected: \"Hello\"\n" + "Received: \"Bye\"\n" + "Timeout: 1000ms\n" + "\n" + "Call log:\n")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveTitle\" with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasTitle\" with timeout 1000ms")); } } @@ -437,12 +425,11 @@ void pass(Page page) { @PlaywrightTest @DisplayName("EX-215 fail string") - @Fixme("java: the assertion error has another format: 'Page URL expected to be\nExpected: wrong\nReceived: data:text/html,
A
\n\nCall log:\n- Assert \"hasURL\" with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failString(Page page) { page.navigate("data:text/html,
A
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).hasURL("wrong", new PageAssertions.HasURLOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(page).toHaveURL(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(page).hasURL(expected) failed\n" + "\n" + "Expected: \"wrong\"\n" + "Received: \"data:text/html,
A
\"\n" + @@ -483,35 +470,34 @@ void pass(Page page) { @PlaywrightTest @DisplayName("EX-224 should not match missing attribute") - @Fixme("java: the assertion error has another format: 'Locator expected to have attribute 'disabled'\nExpected:\nReceived: null\n\nCall log:\n- Assert \"hasAttribute\" locator(\"#node\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void shouldNotMatchMissingAttribute(Page page) { page.setContent("
Text content
"); Locator locator = page.locator("#node"); { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasAttribute("disabled", "", new LocatorAssertions.HasAttributeOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveAttribute(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasAttribute(expected) failed\n" + "\n" + - "Locator: locator('#node')\n" + + "Locator: locator(\"#node\")\n" + "Expected: \"\"\n" + "Received: serializes to the same string\n" + "Timeout: 1000ms\n" + "\n" + "Call log:\n")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveAttribute\" locator('#node') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasAttribute\" locator(\"#node\") with timeout 1000ms")); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasAttribute("disabled", Pattern.compile(".*"), new LocatorAssertions.HasAttributeOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveAttribute(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasAttribute(expected) failed\n" + "\n" + - "Locator: locator('#node')\n" + + "Locator: locator(\"#node\")\n" + "Expected pattern: /.*/\n" + "Received string: \"\"\n" + "Timeout: 1000ms\n" + "\n" + "Call log:\n")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"toHaveAttribute\" locator('#node') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"hasAttribute\" locator(\"#node\") with timeout 1000ms")); } assertThat(locator).not().hasAttribute("disabled", ""); assertThat(locator).not().hasAttribute("disabled", Pattern.compile(".*")); @@ -519,7 +505,6 @@ void shouldNotMatchMissingAttribute(Page page) { @PlaywrightTest @DisplayName("EX-225 should match boolean attribute") - @Fixme("java: the assertion error has another format: 'Locator expected not to have attribute 'checked'\nExpected:\nReceived:\n\nCall log:\n- Assert \"hasAttribute\" locator(\"#node\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void shouldMatchBooleanAttribute(Page page) { page.setContent("
Text content
"); Locator locator = page.locator("#node"); @@ -528,26 +513,26 @@ void shouldMatchBooleanAttribute(Page page) { { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).not().hasAttribute("checked", "", new LocatorAssertions.HasAttributeOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).not.toHaveAttribute(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).not().hasAttribute(expected) failed\n" + "\n" + - "Locator: locator('#node')\n" + + "Locator: locator(\"#node\")\n" + "Expected: not \"\"\n" + "Received: \"\"\n" + "Timeout: 1000ms\n" + "\n" + "Call log:\n")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"not toHaveAttribute\" locator('#node') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"not hasAttribute\" locator(\"#node\") with timeout 1000ms")); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).not().hasAttribute("checked", Pattern.compile(".*"), new LocatorAssertions.HasAttributeOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).not.toHaveAttribute(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).not().hasAttribute(expected) failed\n" + "\n" + - "Locator: locator('#node')\n" + + "Locator: locator(\"#node\")\n" + "Expected pattern: not /.*/\n" + "Received string: \"\"\n" + "Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"not toHaveAttribute\" locator('#node') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"not hasAttribute\" locator(\"#node\") with timeout 1000ms")); } } @@ -663,12 +648,11 @@ void shouldReportIntersectionEvenIfFullyCoveredByOtherElement(Page page) { @PlaywrightTest @DisplayName("EX-237 toHaveCount should not produce logs twice") - @Fixme("java: the assertion error has another format: 'Locator expected to have count\nExpected: 2\nReceived: 1\n\nCall log:\n- Assert \"hasCount\" locator(\"option\") with timeout 2000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void toHaveCountShouldNotProduceLogsTwice(Page page) { page.setContent(""); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("option")).hasCount(2, new LocatorAssertions.HasCountOptions().setTimeout(2000))); - String waitingForMessage = "waiting for locator('option')"; + String waitingForMessage = "waiting for locator(\"option\")"; assertTrue(error.getMessage().contains(waitingForMessage)); assertTrue(error.getMessage().contains("locator resolved to 1 element")); assertTrue(error.getMessage().contains("unexpected value \"1\"")); @@ -677,12 +661,11 @@ void toHaveCountShouldNotProduceLogsTwice(Page page) { @PlaywrightTest @DisplayName("EX-238 toHaveText should not produce logs twice") - @Fixme("java: the assertion error has another format: 'Locator expected to have text\nExpected: world\nReceived: hello\n\nCall log:\n- Assert \"hasText\" locator(\"div\") with timeout 2000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void toHaveTextShouldNotProduceLogsTwice(Page page) { page.setContent("
hello
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("div")).hasText("world", new LocatorAssertions.HasTextOptions().setTimeout(2000))); - String waitingForMessage = "waiting for locator('div')"; + String waitingForMessage = "waiting for locator(\"div\")"; assertTrue(error.getMessage().contains(waitingForMessage)); assertTrue(error.getMessage().contains("locator resolved to
hello
")); assertTrue(error.getMessage().contains("unexpected value \"hello\"")); @@ -691,12 +674,11 @@ void toHaveTextShouldNotProduceLogsTwice(Page page) { @PlaywrightTest @DisplayName("EX-239 toHaveText that does not match should not produce logs twice") - @Fixme("java: the assertion error has another format: 'Locator expected to have text\nError: element(s) not found\nExpected: world\nReceived: - text: hello\n\nCall log:\n- Assert \"hasText\" locator(\"span\") with timeout 2000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void toHaveTextThatDoesNotMatchShouldNotProduceLogsTwice(Page page) { page.setContent("
hello
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("span")).hasText("world", new LocatorAssertions.HasTextOptions().setTimeout(2000))); - String waitingForMessage = "waiting for locator('span')"; + String waitingForMessage = "waiting for locator(\"span\")"; assertTrue(error.getMessage().contains(waitingForMessage)); assertFalse(error.getMessage().contains("locator resolved to")); assertFalse(error.getMessage().replace(waitingForMessage, "").contains(waitingForMessage)); @@ -704,30 +686,28 @@ void toHaveTextThatDoesNotMatchShouldNotProduceLogsTwice(Page page) { @PlaywrightTest @DisplayName("EX-240 strict mode violation error format") - @Fixme("java: the assertion error has another format: 'Locator expected to have text\nError: strict mode violation: locator(\"div\") resolved to 2 elements:\n1)
a
aka getByText(\"a\")\n2)
b
aka getByText(\"b\")\nExpected: foo\n\nCall log:\n- Assert \"hasText\" locator(\"div\") with timeout 5000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void strictModeViolationErrorFormat(Page page) { page.setContent("
a
b
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("div")).hasText("foo")); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveText(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasText(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: \"foo\"\n" + - "Error: strict mode violation: locator('div') resolved to 2 elements:\n" + - " 1)
a
aka getByText('a')\n" + - " 2)
b
aka getByText('b')\n" + + "Error: strict mode violation: locator(\"div\") resolved to 2 elements:\n" + + " 1)
a
aka getByText(\"a\")\n" + + " 2)
b
aka getByText(\"b\")\n" + "\n" + "Call log:\n")); } @PlaywrightTest @DisplayName("EX-241 invalid selector error format") - @Fixme("java: the assertion error has another format: 'Locator expected to be visible\nError: Unexpected token \"#\" while parsing css selector \"##\". Did you mean to CSS.escape it?\n\nCall log:\n- Assert \"isVisible\" ## with timeout 5000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void invalidSelectorErrorFormat(Page page) { page.setContent("
a
b
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("##")).isVisible()); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toBeVisible() failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).isVisible() failed\n" + "\n" + "Locator: ##\n" + "Expected: visible\n" + diff --git a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectTimeout.java b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectTimeout.java index 76c9f7ad6..ed9753b3c 100644 --- a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectTimeout.java +++ b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectTimeout.java @@ -35,14 +35,13 @@ public class TestExpectTimeout { @PlaywrightTest @DisplayName("EX-244 should print element not found") - @Fixme("java: the assertion error has another format: no 'expect(locator).toHaveText(expected) failed' header, no 'Locator:' and 'Expected:' lines") void shouldPrintElementNotFound(Page page) { page.setContent("
Text content
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("no-such-thing")).hasText("hey", new LocatorAssertions.HasTextOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveText(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasText(expected) failed\n" + "\n" + - "Locator: locator('no-such-thing')\n" + + "Locator: locator(\"no-such-thing\")\n" + "Expected: \"hey\"\n" + "Timeout: 1000ms\n" + "Error: element(s) not found\n" + @@ -52,14 +51,13 @@ void shouldPrintElementNotFound(Page page) { @PlaywrightTest @DisplayName("EX-245 should print timed out error message when value does not match") - @Fixme("java: the assertion error has another format: no 'expect(locator).toHaveText(expected) failed' header, no 'Locator:' and 'Expected:' lines") void shouldPrintTimedOutErrorMessageWhenValueDoesNotMatch(Page page) { page.setContent("
Text content
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("div")).hasText("hey", new LocatorAssertions.HasTextOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveText(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasText(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: \"hey\"\n" + "Received: \"Text content\"\n" + "Timeout: 1000ms\n" + @@ -69,14 +67,13 @@ void shouldPrintTimedOutErrorMessageWhenValueDoesNotMatch(Page page) { @PlaywrightTest @DisplayName("EX-246 should print timed out error message with impossible timeout") - @Fixme("java: the assertion error has another format: no 'expect(locator).toHaveText(expected) failed' header, no 'Locator:' and 'Expected:' lines") void shouldPrintTimedOutErrorMessageWithImpossibleTimeout(Page page) { page.setContent("
Text content
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("no-such-thing")).hasText("hey", new LocatorAssertions.HasTextOptions().setTimeout(1))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveText(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasText(expected) failed\n" + "\n" + - "Locator: locator('no-such-thing')\n" + + "Locator: locator(\"no-such-thing\")\n" + "Expected: \"hey\"\n" + "Timeout: 1ms\n" + "Error: element(s) not found\n" + @@ -86,14 +83,13 @@ void shouldPrintTimedOutErrorMessageWithImpossibleTimeout(Page page) { @PlaywrightTest @DisplayName("EX-247 should print timed out error message when value does not match with impossible timeout") - @Fixme("java: the assertion error has another format: no 'expect(locator).toHaveText(expected) failed' header, no 'Locator:' and 'Expected:' lines") void shouldPrintTimedOutErrorMessageWhenValueDoesNotMatchWithImpossibleTimeout(Page page) { page.setContent("
Text content
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("div")).hasText("hey", new LocatorAssertions.HasTextOptions().setTimeout(1))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveText(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasText(expected) failed\n" + "\n" + - "Locator: locator('div')\n" + + "Locator: locator(\"div\")\n" + "Expected: \"hey\"\n" + "Received: \"Text content\"\n" + "Timeout: 1ms\n" + @@ -113,21 +109,20 @@ void shouldHaveTimeoutErrorName(Page page) { @PlaywrightTest @DisplayName("EX-251 should timeout during first locator handler check") - @Fixme("java: the assertion error has another format: no 'expect(locator).toHaveText(expected) failed' header, no 'Locator:' and 'Expected:' lines") void shouldTimeoutDuringFirstLocatorHandlerCheck(Page page, TestServer server) { page.addLocatorHandler(page.locator("div"), locator -> {}); page.setContent("
hello
bye"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("span")).hasText("bye", new LocatorAssertions.HasTextOptions().setTimeout(3000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveText(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasText(expected) failed\n" + "\n" + - "Locator: locator('span')\n" + + "Locator: locator(\"span\")\n" + "Expected: \"bye\"\n" + "Received: \"\"\n" + "Timeout: 3000ms\n" + "\n" + "Call log:\n")); - assertTrue(error.getMessage().contains("locator handler has finished, waiting for locator('div') to be hidden")); + assertTrue(error.getMessage().contains("locator handler has finished, waiting for locator(\"div\") to be hidden")); assertTrue(error.getMessage().contains("locator resolved to visible
hello
")); } diff --git a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectToHaveText.java b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectToHaveText.java index 03c11da8a..25a85878e 100644 --- a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectToHaveText.java +++ b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectToHaveText.java @@ -55,15 +55,14 @@ void pass(Page page) { @PlaywrightTest @DisplayName("EX-275 fail") - @Fixme("java: the assertion error has another format: 'Locator expected to have text matching regex\nExpected: Text 2\nReceived: Text content\n\nCall log:\n- Assert \"hasText\" locator(\"#node\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
Text content
"); Locator locator = page.locator("#node"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasText(Pattern.compile("Text 2"), new LocatorAssertions.HasTextOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toHaveText(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).hasText(expected) failed\n" + "\n" + - "Locator: locator('#node')\n" + + "Locator: locator(\"#node\")\n" + "Expected pattern: /Text 2/\n" + "Received string: \"Text content\"\n" + "Timeout: 1000ms\n" + @@ -87,7 +86,6 @@ void pass(Page page) { @PlaywrightTest @DisplayName("EX-277 fail") - @Fixme("java: the assertion error has another format: 'Locator expected to contain regex\nExpected: ex2\nReceived: Text content\n\nCall log:\n- Assert \"containsText\" locator(\"#node\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
Text content
"); Locator locator = page.locator("#node"); @@ -110,15 +108,14 @@ void pass(Page page) { @PlaywrightTest @DisplayName("EX-279 fail") - @Fixme("java: the assertion error has another format: 'Locator expected to contain text\nExpected: foo\nReceived: Text content\n\nCall log:\n- Assert \"containsText\" locator(\"#node\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
Text content
"); Locator locator = page.locator("#node"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).containsText("foo", new LocatorAssertions.ContainsTextOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).toContainText(expected) failed\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).containsText(expected) failed\n" + "\n" + - "Locator: locator('#node')\n" + + "Locator: locator(\"#node\")\n" + "Expected substring: \"foo\"\n" + "Received string: \"Text content\"\n" + "Timeout: 1000ms\n" + @@ -162,7 +159,6 @@ void passContain(Page page) { @PlaywrightTest @DisplayName("EX-282 fail") - @Fixme("java: the assertion error has another format: 'Locator expected to have text\nExpected: Text\nReceived: Text content\n\nCall log:\n- Assert \"hasText\" locator(\"#node\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
Text content
"); Locator locator = page.locator("#node"); @@ -217,7 +213,6 @@ void inShadowDom(Page page) { @PlaywrightTest @DisplayName("EX-286 fail with impossible timeout") - @Fixme("java: the assertion error has another format: 'Locator expected to have text\nExpected: Text\nReceived: Text content\n\nCall log:\n- Assert \"hasText\" locator(\"#node\") with timeout 1ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failWithImpossibleTimeout(Page page) { page.setContent("
Text content
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, @@ -228,7 +223,6 @@ void failWithImpossibleTimeout(Page page) { @PlaywrightTest @DisplayName("EX-287 do not show \"element(s) not found\" when the real failure is a string mismatch") - @Fixme("java: the assertion error has another format: 'Locator expected to have text\nExpected: Something\nReceived: Final value\n\nCall log:\n- Assert \"hasText\" locator(\"#field\") with timeout 3000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void doNotShowElementSNotFoundWhenTheRealFailureIsAStringMismatch(Page page) { assumeFalse("frozen".equals(System.getenv("PW_CLOCK")), "The element is attached after 1 second."); page.setContent("\n" + @@ -264,7 +258,6 @@ void pass(Page page) { @PlaywrightTest @DisplayName("EX-289 fail") - @Fixme("java: the assertion error has another format: 'Locator expected not to have text\nExpected: Text content\nReceived: Text content\n\nCall log:\n- Assert \"hasText\" locator(\"#node\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
Text content
"); Locator locator = page.locator("#node"); @@ -276,14 +269,13 @@ void fail(Page page) { @PlaywrightTest @DisplayName("EX-290 should work when selector does not match") - @Fixme("java: the assertion error has another format: 'Locator expected not to have text\nError: element(s) not found\nExpected: hello\nReceived: - text: hello\n\nCall log:\n- Assert \"hasText\" locator(\"span\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void shouldWorkWhenSelectorDoesNotMatch(Page page) { page.setContent("
hello
"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.locator("span")).not().hasText("hello", new LocatorAssertions.HasTextOptions().setTimeout(1000))); assertTrue(stripAnsi(error.getMessage()).contains("Expected: not \"hello\"")); assertTrue(stripAnsi(error.getMessage()).contains("Error: element(s) not found")); - assertTrue(stripAnsi(error.getMessage()).contains("waiting for locator('span')")); + assertTrue(stripAnsi(error.getMessage()).contains("waiting for locator(\"span\")")); } } @@ -334,15 +326,14 @@ void passOnEmpty(Page page) { @PlaywrightTest @DisplayName("EX-296 fail on not+empty") - @Fixme("java: the assertion error has another format: 'Locator expected not to have text\nExpected: []\nReceived: []\n\nCall log:\n- Assert \"hasText\" locator(\"p\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void failOnNotEmpty(Page page) { page.setContent("
"); Locator locator = page.locator("p"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).not().hasText(new String[]{}, new LocatorAssertions.HasTextOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(locator).not.toHaveText(expected)")); + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(locator).not().hasText(expected)")); assertTrue(stripAnsi(error.getMessage()).contains("Timeout: 1000ms")); - assertTrue(stripAnsi(error.getMessage()).contains("- Expect \"not toHaveText\" locator('p') with timeout 1000ms")); + assertTrue(stripAnsi(error.getMessage()).contains("- Assert \"not hasText\" locator(\"p\") with timeout 1000ms")); } @PlaywrightTest @@ -379,13 +370,12 @@ class ToContainTextWithArray { @PlaywrightTest @DisplayName("EX-301 fail") - @Fixme("java: the assertion error has another format: 'Locator expected to contain text\nExpected: [Text 2]\nReceived: [Text 1, Text 3]\n\nCall log:\n- Assert \"containsText\" locator(\"div\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void fail(Page page) { page.setContent("
Text 1
Text 3
"); Locator locator = page.locator("div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).containsText(new String[]{"Text 2"}, new LocatorAssertions.ContainsTextOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("- \"Text 2\"")); + assertTrue(stripAnsi(error.getMessage()).contains("Expected: [\"Text 2\"]")); } } } diff --git a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectToHaveValue.java b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectToHaveValue.java index 02e34262a..8b312f2a8 100644 --- a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectToHaveValue.java +++ b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestExpectToHaveValue.java @@ -106,7 +106,6 @@ void followsLabels(Page page) { @PlaywrightTest @DisplayName("EX-308 exact match with text failure") - @Fixme("java: the assertion error has another format: 'Locator expected to have values\nExpected: [R, G]\nReceived: [RR, GG]\n\nCall log:\n- Assert \"hasValues\" locator(\"select\") with timeout 1000ms ...' (no 'expect(locator)... failed' header, no 'Locator:' line, unquoted values, double quoted locators)") void exactMatchWithTextFailure(Page page) { page.setContent("\n" + " \n" + @@ -152,7 +150,7 @@ void failsWhenItemsNotSelected(Page page) { locator.selectOption(new String[]{"B"}); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(locator).hasValues(new Pattern[]{Pattern.compile("R"), Pattern.compile("G")}, new LocatorAssertions.HasValuesOptions().setTimeout(1000))); - assertTrue(stripAnsi(error.getMessage()).contains("+ \"B\"")); + assertTrue(stripAnsi(error.getMessage()).contains("Received: [\"B\"]")); } @PlaywrightTest diff --git a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestLocatorAnyFrame.java b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestLocatorAnyFrame.java index e6764b9b6..34048daf6 100644 --- a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestLocatorAnyFrame.java +++ b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestLocatorAnyFrame.java @@ -200,7 +200,6 @@ void shouldWaitForAFrameToAppearWithToHaveCount(Page page, TestServer server) { @PlaywrightTest @DisplayName("LO-010 should fail toHaveCount when elements match in multiple frames") - @Fixme("java: the assertion message has no 'Locator: frameLocator().locator(...)' line, only the call log: Assert \"hasCount\" frameLocator().locator(\"span\")") void shouldFailToHaveCountWhenElementsMatchInMultipleFrames(Page page, TestServer server) { routePage(page, "empty.html", ""); routePage(page, "a.html", "one"); @@ -209,7 +208,7 @@ void shouldFailToHaveCountWhenElementsMatchInMultipleFrames(Page page, TestServe waitForAllFrames(page, 3, "span"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.frameLocator().locator("span")).hasCount(2, new LocatorAssertions.HasCountOptions().setTimeout(3000))); assertTrue(error.getMessage().contains("frameLocator() matched elements in multiple frames")); - assertTrue(error.getMessage().contains("Locator: frameLocator().locator('span')")); + assertTrue(error.getMessage().contains("Locator: frameLocator().locator(\"span\")")); } @PlaywrightTest @@ -232,7 +231,6 @@ void shouldSupportToHaveTextWithAnArray(Page page, TestServer server) { @PlaywrightTest @DisplayName("LO-013 should fail toHaveText when elements match in multiple frames") - @Fixme("java: the assertion message has no 'Locator: frameLocator().locator(...)' line, only the call log: Assert \"hasCount\" frameLocator().locator(\"span\")") void shouldFailToHaveTextWhenElementsMatchInMultipleFrames(Page page, TestServer server) { routePage(page, "empty.html", ""); routePage(page, "a.html", "
one
"); @@ -241,7 +239,7 @@ void shouldFailToHaveTextWhenElementsMatchInMultipleFrames(Page page, TestServer waitForAllFrames(page, 3, "div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.frameLocator().locator("div")).hasText("one", new LocatorAssertions.HasTextOptions().setTimeout(3000))); assertTrue(error.getMessage().contains("frameLocator() matched elements in multiple frames")); - assertTrue(error.getMessage().contains("Locator: frameLocator().locator('div')")); + assertTrue(error.getMessage().contains("Locator: frameLocator().locator(\"div\")")); } @PlaywrightTest @@ -258,7 +256,6 @@ void shouldFailToHaveTextWithAnArrayWhenElementsMatchInMultipleFrames(Page page, @PlaywrightTest @DisplayName("LO-015 should fail toHaveText upon strict mode violation inside a single frame") - @Fixme("java: the assertion message has no 'Locator: frameLocator().locator(...)' line, only the call log: Assert \"hasCount\" frameLocator().locator(\"span\")") void shouldFailToHaveTextUponStrictModeViolationInsideASingleFrame(Page page, TestServer server) { routePage(page, "empty.html", ""); routePage(page, "a.html", "
one
two
"); @@ -266,7 +263,7 @@ void shouldFailToHaveTextUponStrictModeViolationInsideASingleFrame(Page page, Te waitForAllFrames(page, 2, "div"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page.frameLocator().locator("div")).hasText("one", new LocatorAssertions.HasTextOptions().setTimeout(3000))); assertTrue(error.getMessage().contains("strict mode violation")); - assertTrue(error.getMessage().contains("Locator: frameLocator().locator('div')")); + assertTrue(error.getMessage().contains("Locator: frameLocator().locator(\"div\")")); } @PlaywrightTest @@ -577,7 +574,7 @@ void shouldSupportContentFrame(Page page, TestServer server) { @PlaywrightTest @DisplayName("LO-041 should render frameLocator() in the locator description") - @Fixme("java: Locator.toString() is 'Locator@internal:control=any-frame >> #x >> internal:control=enter-frame >> button', not \"frameLocator().locator('#x').contentFrame().locator('button')\"") + @Fixme("java: Locator.toString() prints Java code, frameLocator().locator(\"#x\").contentFrame().locator(\"button\"), not the JavaScript frameLocator().locator('#x').contentFrame().locator('button')") void shouldRenderFrameLocatorInTheLocatorDescription(Page page) { assertEquals("frameLocator().locator('#x').contentFrame().locator('button')", String.valueOf(page.frameLocator().frameLocator("#x").locator("button"))); assertEquals("frameLocator().locator('section').locator('iframe').contentFrame().getByText('foo')", String.valueOf(page.frameLocator().locator("section").frameLocator("iframe").getByText("foo"))); diff --git a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestLocatorConvenience.java b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestLocatorConvenience.java index cc31a32aa..d3b184cdc 100644 --- a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestLocatorConvenience.java +++ b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestLocatorConvenience.java @@ -39,7 +39,7 @@ public class TestLocatorConvenience { @PlaywrightTest @DisplayName("LO-057 should have a nice preview") - @Fixme("java: Locator.toString() is 'Locator@#outer', not \"locator('#outer')\"; the JSHandle preview is 'JSHandle@node'") + @Fixme("java: Locator.toString() prints Java code, locator(\"#outer\"), not the JavaScript locator('#outer'); the JSHandle preview is 'JSHandle@node'") void shouldHaveANicePreview(Page page, TestServer server) { page.navigate(server.PREFIX + "/dom.html"); Locator outer = page.locator("#outer"); @@ -290,7 +290,7 @@ void descriptionShouldReturnDescriptionForLocatorWithMultipleDescribeCalls(Page @PlaywrightTest @DisplayName("LO-077 toString() returns formatted locator") - @Fixme("java: Locator.toString() is 'Locator@internal:role=button[name=\"Submit\"i]', not \"getByRole('button', { name: 'Submit' })\"") + @Fixme("java: Locator.toString() prints Java code, getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(\"Submit\")), not the JavaScript getByRole('button', { name: 'Submit' })") void toStringReturnsFormattedLocator(Page page) { Locator locator = page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")); assertEquals("getByRole('button', { name: 'Submit' })", locator.toString()); diff --git a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestToMatchAriaSnapshot.java b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestToMatchAriaSnapshot.java index 7a3c78437..d59311faf 100644 --- a/playwright/src/test/java/com/microsoft/playwright/tests/page/TestToMatchAriaSnapshot.java +++ b/playwright/src/test/java/com/microsoft/playwright/tests/page/TestToMatchAriaSnapshot.java @@ -74,13 +74,12 @@ void shouldMatchPageWithNot(Page page) { @PlaywrightTest @DisplayName("AR-110 should fail page with timeout") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"Timeout: Nms\" line") void shouldFailPageWithTimeout(Page page) { page.setContent("

title

"); AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading \"wrong\"\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(2000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); assertTrue(stripAnsi(e.getMessage()).contains("Timeout: 2000ms")); assertFalse(stripAnsi(e.getMessage()).contains("Locator")); } @@ -217,7 +216,6 @@ void detailsVisibility(Page page) { @PlaywrightTest @DisplayName("AR-119 checked attribute") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"Timeout: Nms\" line") void checkedAttribute(Page page) { page.setContent("\n" + " \n" + @@ -239,7 +237,7 @@ void checkedAttribute(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - checkbox [checked=false]\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); assertTrue(stripAnsi(e.getMessage()).contains("Timeout: 1000ms")); } @@ -247,7 +245,7 @@ void checkedAttribute(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - checkbox [checked=mixed]\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); assertTrue(stripAnsi(e.getMessage()).contains("Timeout: 1000ms")); } @@ -261,7 +259,6 @@ void checkedAttribute(Page page) { @PlaywrightTest @DisplayName("AR-120 disabled attribute") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"Timeout: Nms\" line") void disabledAttribute(Page page) { page.setContent("\n" + " \n" + @@ -283,7 +280,7 @@ void disabledAttribute(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - button [disabled=false]\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); assertTrue(stripAnsi(e.getMessage()).contains("Timeout: 1000ms")); } @@ -297,7 +294,6 @@ void disabledAttribute(Page page) { @PlaywrightTest @DisplayName("AR-121 expanded attribute") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"Timeout: Nms\" line") void expandedAttribute(Page page) { page.setContent("\n" + " \n" + @@ -319,7 +315,7 @@ void expandedAttribute(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - button [expanded=false]\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); assertTrue(stripAnsi(e.getMessage()).contains("Timeout: 1000ms")); } @@ -333,7 +329,6 @@ void expandedAttribute(Page page) { @PlaywrightTest @DisplayName("AR-122 level attribute") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"Timeout: Nms\" line") void levelAttribute(Page page) { page.setContent("\n" + "

Section Title

\n" + @@ -351,7 +346,7 @@ void levelAttribute(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading [level=3]\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); assertTrue(stripAnsi(e.getMessage()).contains("Timeout: 1000ms")); } @@ -365,7 +360,6 @@ void levelAttribute(Page page) { @PlaywrightTest @DisplayName("AR-123 pressed attribute") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"Timeout: Nms\" line") void pressedAttribute(Page page) { page.setContent("\n" + " \n" + @@ -387,7 +381,7 @@ void pressedAttribute(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - button [pressed=false]\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); assertTrue(stripAnsi(e.getMessage()).contains("Timeout: 1000ms")); } @@ -404,7 +398,7 @@ void pressedAttribute(Page page) { AssertionFailedError e = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - button [pressed=true]\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); assertTrue(stripAnsi(e.getMessage()).contains("Timeout: 1000ms")); } @@ -418,7 +412,6 @@ void pressedAttribute(Page page) { @PlaywrightTest @DisplayName("AR-124 selected attribute") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"- Expected - N / + Received + M\" diff, only the Expected: and Received: dumps") void selectedAttribute(Page page) { page.setContent("\n" + " \n" + @@ -458,20 +451,17 @@ void selectedAttribute(Page page) { " - row [selected=false]\n" + " - row [selected=false]\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + - "Timeout: 1000ms\n" + - "- Expected - 2\n" + - "+ Received + 6\n" + - "\n" + - "- - row [selected=false]\n" + - "- - row [selected=false]\n" + - "+ - table:\n" + - "+ - rowgroup:\n" + - "+ - row \"Row\" [selected]:\n" + - "+ - cell \"Row\"\n" + - "+ - row \"Row 2\":\n" + - "+ - cell \"Row 2\"\n" + + "Expected: \"- row [selected=false]\n" + + "- row [selected=false]\"\n" + + "Received: \"- table:\n" + + " - rowgroup:\n" + + " - row \\\"Row\\\" [selected]:\n" + + " - cell \\\"Row\\\"\n" + + " - row \\\"Row 2\\\":\n" + + " - cell \\\"Row 2\\\"\"\n" + + "Timeout: 1000ms\n" + "\n" + "Call log:\n")); } @@ -549,7 +539,6 @@ void integrationTest2(Page page) { @PlaywrightTest @DisplayName("AR-127 expected formatter") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"- Expected - N / + Received + M\" diff, only the Expected: and Received: dumps") void expectedFormatter(Page page) { page.setContent("\n" + "
\n" + @@ -566,18 +555,15 @@ void expectedFormatter(Page page) { // Note that error message should not contain any regular expressions, // unlike the baseline generated by --update-snapshots. - assertTrue(stripAnsi(error.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed\n" + - "\n" + - "Timeout: 1ms\n" + - "- Expected - 2\n" + - "+ Received + 4\n" + + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + - "- - heading \"todos\"\n" + - "- - textbox \"Wrong text\"\n" + - "+ - banner:\n" + - "+ - heading \"todos\" [level=1]\n" + - "+ - textbox \"What needs to be done?\"\n" + - "+ - button \"Time 15:30\"\n" + + "Expected: \"- heading \\\"todos\\\"\n" + + "- textbox \\\"Wrong text\\\"\"\n" + + "Received: \"- banner:\n" + + " - heading \\\"todos\\\" [level=1]\n" + + " - textbox \\\"What needs to be done?\\\"\n" + + " - button \\\"Time 15:30\\\"\"\n" + + "Timeout: 1ms\n" + "\n" + "Call log:\n")); } @@ -674,7 +660,6 @@ void shouldUnpackEscapedNames(Page page) { @PlaywrightTest @DisplayName("AR-129 should report error in YAML") - @Fixme("java: the error message is \"Page expected to match Aria snapshot\\nError: Aria snapshot must be a YAML sequence, elements starting with \\\" -\\\"\\nExpected: ...\" with the call log \"Assert \\\"matchesAriaSnapshot\\\" with timeout 5000ms\", not \"expect(page).toMatchAriaSnapshot(expected) failed\\n\\nExpected: ...\" with the call log \"Expect \\\"toMatchAriaSnapshot\\\" with timeout 10000ms\"") void shouldReportErrorInYAML(Page page) { page.setContent("

title

"); @@ -682,20 +667,20 @@ void shouldReportErrorInYAML(Page page) { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " heading \"title\"\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"heading \\\"title\\\"\"\n" + "Error: Aria snapshot must be a YAML sequence, elements starting with \" -\"\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading: a:\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading: a:\"\n" + "Error: Nested mappings are not allowed in compact mappings at line 1, column 12:\n" + @@ -704,13 +689,12 @@ void shouldReportErrorInYAML(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } } @PlaywrightTest @DisplayName("AR-130 should report error in YAML keys") - @Fixme("java: the error message is \"Page expected to match Aria snapshot\\nError: Unterminated string:\\n...\" with the call log \"Assert \\\"matchesAriaSnapshot\\\" with timeout 5000ms\", not \"expect(page).toMatchAriaSnapshot(expected) failed\\n\\nExpected: ...\" with the call log \"Expect \\\"toMatchAriaSnapshot\\\" with timeout 10000ms\"") void shouldReportErrorInYAMLKeys(Page page) { page.setContent("

title

"); @@ -718,7 +702,7 @@ void shouldReportErrorInYAMLKeys(Page page) { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading \"title\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading \\\"title\"\n" + "Error: Unterminated string:\n" + @@ -727,14 +711,14 @@ void shouldReportErrorInYAMLKeys(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading /title\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading /title\"\n" + "Error: Unterminated regex:\n" + @@ -743,14 +727,14 @@ void shouldReportErrorInYAMLKeys(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading [level=a]\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading [level=a]\"\n" + "Error: Value of \"level\" attribute must be a number:\n" + @@ -759,14 +743,14 @@ void shouldReportErrorInYAMLKeys(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading [expanded=FALSE]\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading [expanded=FALSE]\"\n" + "Error: Value of \"expanded\" attribute must be a boolean:\n" + @@ -775,14 +759,14 @@ void shouldReportErrorInYAMLKeys(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading [checked=foo]\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading [checked=foo]\"\n" + "Error: Value of \"checked\" attribute must be a boolean or \"mixed\":\n" + @@ -791,14 +775,14 @@ void shouldReportErrorInYAMLKeys(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading [level=]\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading [level=]\"\n" + "Error: Value of \"level\" attribute must be a number:\n" + @@ -807,14 +791,14 @@ void shouldReportErrorInYAMLKeys(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading [bogus]\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading [bogus]\"\n" + "Error: Unsupported attribute [bogus]:\n" + @@ -823,14 +807,14 @@ void shouldReportErrorInYAMLKeys(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } { AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - heading invalid\n" + " ")); - assertEquals("expect(page).toMatchAriaSnapshot(expected) failed\n" + + assertEquals("assertThat(page).matchesAriaSnapshot(expected) failed\n" + "\n" + "Expected: \"- heading invalid\"\n" + "Error: Unexpected input:\n" + @@ -839,7 +823,7 @@ void shouldReportErrorInYAMLKeys(Page page) { " ^\n" + "\n" + "Call log:\n" + - " - Expect \"toMatchAriaSnapshot\" with timeout 10000ms\n", stripAnsi(error.getMessage())); + " - Assert \"matchesAriaSnapshot\" with timeout 5000ms\n", stripAnsi(error.getMessage())); } } @@ -878,7 +862,6 @@ void shouldParseAttributes(Page page) { @PlaywrightTest @DisplayName("AR-133 should not unshift actual template text") - @Fixme("java: the assertion error message has no \"- Expected - N / + Received + M\" diff with the \"+ - heading ...\" lines, only the Expected: and Received: dumps") void shouldNotUnshiftActualTemplateText(Page page) { page.setContent("\n" + "

title

\n" + @@ -895,7 +878,6 @@ void shouldNotUnshiftActualTemplateText(Page page) { @PlaywrightTest @DisplayName("AR-134 should not match what is not matched") - @Fixme("java: the assertion error message has no \"- Expected - N / + Received + M\" diff with the \"+ - heading ...\" lines, only the Expected: and Received: dumps") void shouldNotMatchWhatIsNotMatched(Page page) { page.setContent("

Text

"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + @@ -903,9 +885,9 @@ void shouldNotMatchWhatIsNotMatched(Page page) { " - button \"bogus\"\n" + " ")); assertTrue(stripAnsi(error.getMessage()).contains("\n" + - "- - paragraph:\n" + - "- - button \"bogus\"\n" + - "+ - paragraph: Text")); + "Expected: \"- paragraph:\n" + + " - button \\\"bogus\\\"\"\n" + + "Received: \"- paragraph: Text\"")); } @PlaywrightTest @@ -923,7 +905,6 @@ void shouldMatchUrl(Page page) { @PlaywrightTest @DisplayName("AR-136 should detect unexpected children: equal") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"- Expected - N / + Received + M\" diff, only the Expected: and Received: dumps") void shouldDetectUnexpectedChildrenEqual(Page page) { page.setContent("\n" + "
    \n" + @@ -946,13 +927,12 @@ void shouldDetectUnexpectedChildrenEqual(Page page) { " - listitem: \"Three\"\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); - assertTrue(stripAnsi(e.getMessage()).contains("+ - listitem: Two")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("\n - listitem: Two")); } @PlaywrightTest @DisplayName("AR-137 should detect unexpected children: deep-equal") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"- Expected - N / + Received + M\" diff, only the Expected: and Received: dumps") void shouldDetectUnexpectedChildrenDeepequal(Page page) { page.setContent("\n" + "
      \n" + @@ -988,13 +968,12 @@ void shouldDetectUnexpectedChildrenDeepequal(Page page) { " - listitem: 1.1\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); - assertTrue(stripAnsi(e.getMessage()).contains("+ - listitem: \"1.2\"")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("\n - listitem: \\\"1.2\\\"")); } @PlaywrightTest @DisplayName("AR-138 should allow restoring contain mode inside deep-equal") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"- Expected - N / + Received + M\" diff, only the Expected: and Received: dumps") void shouldAllowRestoringContainModeInsideDeepequal(Page page) { page.setContent("\n" + "
        \n" + @@ -1015,8 +994,8 @@ void shouldAllowRestoringContainModeInsideDeepequal(Page page) { " - listitem: 1.1\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); - assertTrue(stripAnsi(e.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); - assertTrue(stripAnsi(e.getMessage()).contains("+ - listitem: \"1.2\"")); + assertTrue(stripAnsi(e.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(e.getMessage()).contains("\n - listitem: \\\"1.2\\\"")); assertThat(page).matchesAriaSnapshot("\n" + " - list:\n" + @@ -1030,7 +1009,6 @@ void shouldAllowRestoringContainModeInsideDeepequal(Page page) { @PlaywrightTest @DisplayName("AR-139 top-level deep-equal") - @Fixme("java: the assertion error message has no \"- Expected - N / + Received + M\" diff with the \"+ - heading ...\" lines, only the Expected: and Received: dumps") void toplevelDeepequal(Page page) { page.setContent("\n" + "
          \n" + @@ -1049,13 +1027,13 @@ void toplevelDeepequal(Page page) { " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1000))); assertTrue(stripAnsi(error.getMessage()).contains(("\n" + - "- - /children: deep-equal\n" + - "- - list\n" + - "+ - list:\n" + - "+ - listitem:\n" + - "+ - list:\n" + - "+ - listitem: \"1.1\"\n" + - "+ - listitem: \"1.2\"\n" + + "Expected: \"- /children: deep-equal\n" + + "- list\"\n" + + "Received: \"- list:\n" + + " - listitem:\n" + + " - list:\n" + + " - listitem: \\\"1.1\\\"\n" + + " - listitem: \\\"1.2\\\"\"\n" + " ").trim())); } @@ -1078,16 +1056,15 @@ void generatedSnapshotIncludesTextChildrenWhenNameIsLongerThanText(Page page) { @PlaywrightTest @DisplayName("AR-141 treat bad regex as a string") - @Fixme("java: the assertion error message starts with \"Page expected to match Aria snapshot\" (for a locator \"Locator expected to match Aria snapshot\"), not \"expect(page).toMatchAriaSnapshot(expected) failed\" and has no \"- Expected - N / + Received + M\" diff, only the Expected: and Received: dumps") void treatBadRegexAsAString(Page page) { page.setContent("Log in"); AssertionFailedError error = assertThrows(AssertionFailedError.class, () -> assertThat(page).matchesAriaSnapshot("\n" + " - link \"Log in\":\n" + " - /url: /[a/\n" + " ", new PageAssertions.MatchesAriaSnapshotOptions().setTimeout(1))); - assertTrue(stripAnsi(error.getMessage()).contains("expect(page).toMatchAriaSnapshot(expected) failed")); - assertTrue(stripAnsi(error.getMessage()).contains("- - /url: /[a/")); - assertTrue(stripAnsi(error.getMessage()).contains("+ - /url: /foo")); + assertTrue(stripAnsi(error.getMessage()).contains("assertThat(page).matchesAriaSnapshot(expected) failed")); + assertTrue(stripAnsi(error.getMessage()).contains("\n - /url: /[a/\"")); + assertTrue(stripAnsi(error.getMessage()).contains("\n - /url: /foo\"")); } @PlaywrightTest