From bd6ed7bc8873834e50fd66ead9a111becd80814d Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Tue, 26 Oct 2021 16:41:47 -0700 Subject: [PATCH] feat: locator assertions part 2 (#663) --- .../impl/LocatorAssertionsImpl.java | 110 +++- .../playwright/TestLocatorAssertions.java | 580 +++++++++++++++++- .../playwright/impl/LocatorImpl.java | 4 - 3 files changed, 641 insertions(+), 53 deletions(-) diff --git a/assertions/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java b/assertions/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java index fa933eb0..4baae1bb 100644 --- a/assertions/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java +++ b/assertions/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.regex.Pattern; import static com.microsoft.playwright.impl.AssertionUtils.formatValue; +import static com.microsoft.playwright.impl.Serialization.serializeArgument; import static com.microsoft.playwright.impl.Utils.convertViaJson; import static java.util.Arrays.asList; @@ -34,8 +35,10 @@ public class LocatorAssertionsImpl implements LocatorAssertions { static class HasTextCommonOptions { public Object expressionArg; + public SerializedArgument expectedValue; public Double timeout; public Boolean useInnerText; + public Integer expectedNumber; } public LocatorAssertionsImpl(Locator locator) { @@ -116,48 +119,92 @@ public class LocatorAssertionsImpl implements LocatorAssertions { } @Override - public void hasClass(String expected, HasClassOptions options) { - + 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", convertViaJson(options, HasTextCommonOptions.class)); } @Override - public void hasClass(Pattern expected, HasClassOptions options) { - + public void hasClass(Pattern pattern, HasClassOptions options) { + ExpectedTextValue expected = new ExpectedTextValue(); + expected.regexSource = pattern.pattern(); + expectImpl("to.have.class", expected, pattern, "Locator expected to have class", convertViaJson(options, HasTextCommonOptions.class)); } @Override - public void hasClass(String[] expected, HasClassOptions options) { - + 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", convertViaJson(options, HasTextCommonOptions.class)); } @Override - public void hasClass(Pattern[] expected, HasClassOptions options) { - + public void hasClass(Pattern[] patterns, HasClassOptions options) { + List list = new ArrayList<>(); + for (Pattern pattern : patterns) { + ExpectedTextValue expected = new ExpectedTextValue(); + expected.regexSource = pattern.pattern(); + list.add(expected); + } + expectImpl("to.have.class.array", list, patterns, "Locator expected to have class", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void hasCount(int count, HasCountOptions options) { - + if (options == null) { + options = new HasCountOptions(); + } + HasTextCommonOptions commonOptions = convertViaJson(options, HasTextCommonOptions.class); + commonOptions.expectedNumber = count; + List expectedText = null; + expectImpl("to.have.count", expectedText, count, "Locator expected to have count", commonOptions); } @Override public void hasCSS(String name, String value, HasCSSOptions options) { - + ExpectedTextValue expected = new ExpectedTextValue(); + expected.string = value; + hasCSS(name, expected, value, options); } @Override public void hasCSS(String name, Pattern value, HasCSSOptions options) { + ExpectedTextValue expected = new ExpectedTextValue(); + expected.regexSource = value.pattern(); + hasCSS(name, expected, value, options); + } + private void hasCSS(String name, ExpectedTextValue expectedText, Object expectedValue, HasCSSOptions options) { + if (options == null) { + options = new HasCSSOptions(); + } + HasTextCommonOptions commonOptions = convertViaJson(options, HasTextCommonOptions.class); + commonOptions.expressionArg = name; + expectImpl("to.have.css", expectedText, expectedValue, "Locator expected to have CSS", commonOptions); } @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", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void hasJSProperty(String name, Object value, HasJSPropertyOptions options) { - + if (options == null) { + options = new HasJSPropertyOptions(); + } + HasTextCommonOptions commonOptions = convertViaJson(options, HasTextCommonOptions.class); + commonOptions.expressionArg = name; + commonOptions.expectedValue = serializeArgument(value); + List list = null; + expectImpl("to.have.property", list, value, "Locator expected to have js property", commonOptions); } @Override @@ -207,64 +254,75 @@ public class LocatorAssertionsImpl implements LocatorAssertions { @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", convertViaJson(options, HasTextCommonOptions.class)); } @Override - public void hasValue(Pattern value, HasValueOptions options) { - + public void hasValue(Pattern pattern, HasValueOptions options) { + ExpectedTextValue expected = new ExpectedTextValue(); + expected.regexSource = pattern.pattern(); + expectImpl("to.have.value", expected, pattern, "Locator expected to have value", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void isChecked(IsCheckedOptions options) { - + expectTrue("to.be.checked", "Locator expected to be checked", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void isDisabled(IsDisabledOptions options) { - + expectTrue("to.be.disabled", "Locator expected to be disabled", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void isEditable(IsEditableOptions options) { - + expectTrue("to.be.editable", "Locator expected to be editable", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void isEmpty(IsEmptyOptions options) { - + expectTrue("to.be.empty", "Locator expected to be empty", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void isEnabled(IsEnabledOptions options) { - + expectTrue("to.be.enabled", "Locator expected to be enabled", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void isFocused(IsFocusedOptions options) { - + expectTrue("to.be.focused", "Locator expected to be focused", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void isHidden(IsHiddenOptions options) { - + expectTrue("to.be.hidden", "Locator expected to be hidden", convertViaJson(options, HasTextCommonOptions.class)); } @Override public void isVisible(IsVisibleOptions options) { - + expectTrue("to.be.visible", "Locator expected to be visible", convertViaJson(options, HasTextCommonOptions.class)); } private void expectImpl(String expression, ExpectedTextValue textValue, Object expected, String message, HasTextCommonOptions options) { expectImpl(expression, asList(textValue), expected, message, options); } + private void expectTrue(String expression, String message, HasTextCommonOptions options) { + List expectedText = null; + expectImpl(expression, expectedText, null, message, options); + } + private void expectImpl(String expression, List expectedText, Object expected, String message, HasTextCommonOptions options) { if (options == null) { options = new HasTextCommonOptions(); } FrameExpectOptions expectOptions = new FrameExpectOptions(); expectOptions.expressionArg = options.expressionArg; + expectOptions.expectedNumber = options.expectedNumber; + expectOptions.expectedValue = options.expectedValue; expectOptions.expectedText = expectedText; expectOptions.isNot = isNot; expectOptions.timeout = options.timeout; @@ -272,9 +330,6 @@ public class LocatorAssertionsImpl implements LocatorAssertions { expectImpl(expression, expectOptions, expected, message); } - private void expectImpl(String expression, ExpectedTextValue textValue, Object expected, String message, Double timeout) { - } - private void expectImpl(String expression, FrameExpectOptions expectOptions, Object expected, String message) { FrameExpectResult result = actual.expect(expression, expectOptions); if (result.matches == isNot) { @@ -283,6 +338,9 @@ public class LocatorAssertionsImpl implements LocatorAssertions { if (!log.isEmpty()) { log = "\nCall log:\n" + log; } + if (expected == null) { + throw new AssertionFailedError(message + log); + } throw new AssertionFailedError(message + log, formatValue(expected), formatValue(actual)); } } diff --git a/assertions/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java b/assertions/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java index c6d5a17a..ed4f4b66 100644 --- a/assertions/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java +++ b/assertions/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java @@ -17,19 +17,18 @@ package com.microsoft.playwright; import com.microsoft.playwright.assertions.LocatorAssertions; -import com.microsoft.playwright.assertions.PageAssertions; import org.junit.jupiter.api.Test; import org.opentest4j.AssertionFailedError; import java.util.regex.Pattern; +import static com.microsoft.playwright.Utils.mapOf; import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; -import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.*; public class TestLocatorAssertions extends TestBase { @Test - void shouldSupportContainsTextWRegexPass() { + void containsTextWRegexPass() { page.setContent("
Text content
"); Locator locator = page.locator("#node"); assertThat(locator).containsText(Pattern.compile("ex")); @@ -38,11 +37,11 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportContainsTextWRegexFail() { + void containsTextWRegexFail() { page.setContent("
Text content
"); Locator locator = page.locator("#node"); try { - assertThat(locator).containsText(Pattern.compile("ex2"), new LocatorAssertions.ContainsTextOptions().setTimeout(100)); + assertThat(locator).containsText(Pattern.compile("ex2"), new LocatorAssertions.ContainsTextOptions().setTimeout(1000)); fail("did not throw"); } catch (AssertionFailedError e) { assertEquals("ex2", e.getExpected().getStringRepresentation()); @@ -52,7 +51,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportHasTextWRegexPass() { + void hasTextWRegexPass() { page.setContent("
Text content
"); Locator locator = page.locator("#node"); assertThat(locator).hasText(Pattern.compile("Text")); @@ -61,11 +60,11 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportHasTextWRegexFail() { + void hasTextWRegexFail() { page.setContent("
Text content
"); Locator locator = page.locator("#node"); try { - assertThat(locator).hasText(Pattern.compile("Text 2"), new LocatorAssertions.HasTextOptions().setTimeout(100)); + assertThat(locator).hasText(Pattern.compile("Text 2"), new LocatorAssertions.HasTextOptions().setTimeout(1000)); fail("did not throw"); } catch (AssertionFailedError e) { assertEquals("Text 2", e.getExpected().getStringRepresentation()); @@ -75,7 +74,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportHasTextWTextPass() { + void hasTextWTextPass() { page.setContent("
Text \ncontent 
"); Locator locator = page.locator("#node"); // Should normalize whitespace. @@ -83,12 +82,12 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportHasTextWTextFail() { + void hasTextWTextFail() { page.setContent("
Text content
"); Locator locator = page.locator("#node"); // Should normalize whitespace. try { - assertThat(locator).hasText("Text", new LocatorAssertions.HasTextOptions().setTimeout(100)); + assertThat(locator).hasText("Text", new LocatorAssertions.HasTextOptions().setTimeout(1000)); fail("did not throw"); } catch (AssertionFailedError e) { assertEquals("Text", e.getExpected().getStringRepresentation()); @@ -98,7 +97,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWTextArrayPass() { + void hasTextWTextArrayPass() { page.setContent("
Text \n1
Text 2a
"); Locator locator = page.locator("div"); // Should normalize whitespace. @@ -106,7 +105,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWTextArrayPassEmpty() { + void hasTextWTextArrayPassEmpty() { page.setContent("
"); Locator locator = page.locator("p"); // Should normalize whitespace. @@ -114,7 +113,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWTextArrayPassNotEmpty() { + void hasTextWTextArrayPassNotEmpty() { page.setContent("

Test

"); Locator locator = page.locator("div"); // Should normalize whitespace. @@ -122,7 +121,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWTextArrayPassOnEmpty() { + void hasTextWTextArrayPassOnEmpty() { page.setContent("
"); Locator locator = page.locator("p"); // Should normalize whitespace. @@ -130,7 +129,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWTextArrayFailOnNotEmpty() { + void hasTextWTextArrayFailOnNotEmpty() { page.setContent("
"); Locator locator = page.locator("p"); // Should normalize whitespace. @@ -145,7 +144,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWTextArrayPassLazyPass() { + void hasTextWTextArrayPassLazyPass() { page.setContent("
"); Locator locator = page.locator("p"); page.evaluate("setTimeout(() => {\n" + @@ -156,7 +155,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWTextArrayFail() { + void hasTextWTextArrayFail() { page.setContent("
Text 1
Text 3
"); Locator locator = page.locator("div"); page.evaluate("setTimeout(() => {\n" + @@ -174,7 +173,7 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWRegExArrayPass() { + void hasTextWRegExArrayPass() { page.setContent("
Text \n1
Text 2a
"); Locator locator = page.locator("div"); // Should normalize whitespace. @@ -182,12 +181,12 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveTextWRegExArrayFail() { + void hasTextWRegExArrayFail() { page.setContent("
Text 1
Text 3
"); Locator locator = page.locator("div"); try { // Should normalize whitespace. - assertThat(locator).hasText(new Pattern[] {Pattern.compile( "Text 1"), Pattern.compile("Text \\d"), Pattern.compile("Extra")}, new LocatorAssertions.HasTextOptions().setTimeout(100)); + assertThat(locator).hasText(new Pattern[] {Pattern.compile( "Text 1"), Pattern.compile("Text \\d"), Pattern.compile("Extra")}, new LocatorAssertions.HasTextOptions().setTimeout(1000)); fail("did not throw"); } catch (AssertionFailedError e) { assertEquals("[Text 1, Text \\d, Extra]", e.getExpected().getStringRepresentation()); @@ -197,14 +196,14 @@ public class TestLocatorAssertions extends TestBase { } @Test - void shouldSupportToHaveAttributeTextPass() { + void hasAttributeTextPass() { page.setContent("
Text content
"); Locator locator = page.locator("#node"); assertThat(locator).hasAttribute("id", "node"); } @Test - void shouldSupportToHaveAttributeTextFail() { + void hasAttributeTextFail() { page.setContent("
Text content
"); Locator locator = page.locator("#node"); try { @@ -216,4 +215,539 @@ public class TestLocatorAssertions extends TestBase { assertTrue(e.getMessage().contains("Locator expected to have attribute"), e.getMessage()); } } + + @Test + void hasAttributeRegExpPass() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + assertThat(locator).hasAttribute("id", Pattern.compile("n..e")); + } + + @Test + void hasAttributeRegExpFail() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + try { + assertThat(locator).hasAttribute("id", ".Nod..", new LocatorAssertions.HasAttributeOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals(".Nod..", e.getExpected().getStringRepresentation()); + assertEquals("node", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have attribute"), e.getMessage()); + } + } + + @Test + void hasClassTextPass() { + page.setContent("
"); + Locator locator = page.locator("div"); + assertThat(locator).hasClass("foo bar baz"); + } + + @Test + void hasClassTextFail() { + page.setContent("
"); + Locator locator = page.locator("div"); + try { + assertThat(locator).hasClass("foo bar baz", new LocatorAssertions.HasClassOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("foo bar baz", e.getExpected().getStringRepresentation()); + assertEquals("bar baz", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have class"), e.getMessage()); + } + } + + @Test + void hasClassRegExpPass() { + page.setContent("
"); + Locator locator = page.locator("div"); + assertThat(locator).hasClass(Pattern.compile("foo.* baz")); + } + + @Test + void hasClassRegExpFail() { + page.setContent("
"); + Locator locator = page.locator("div"); + try { + assertThat(locator).hasClass("foo Z.*", new LocatorAssertions.HasClassOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("foo Z.*", e.getExpected().getStringRepresentation()); + assertEquals("bar baz", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have class"), e.getMessage()); + } + } + + @Test + void hasClassTextArrayPass() { + page.setContent("
"); + Locator locator = page.locator("div"); + assertThat(locator).hasClass(new String[] {"foo", "bar", "baz"}); + } + + @Test + void hasClassTextArrayFail() { + page.setContent("
"); + Locator locator = page.locator("div"); + try { + assertThat(locator).hasClass(new String[] {"foo", "bar", "missing"}, new LocatorAssertions.HasClassOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + 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()); + } + } + + @Test + void hasClassRegExpArrayPass() { + page.setContent("
"); + Locator locator = page.locator("div"); + assertThat(locator).hasClass(new Pattern[] {Pattern.compile("fo.*"), Pattern.compile(".ar"), Pattern.compile("baz")}); + } + + @Test + void hasClassRegExpArrayFail() { + page.setContent("
"); + Locator locator = page.locator("div"); + try { + assertThat(locator).hasClass(new Pattern[] {Pattern.compile("fo.*"), Pattern.compile(".ar"), Pattern.compile("baz"), Pattern.compile("extra")}, new LocatorAssertions.HasClassOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("[fo.*, .ar, baz, extra]", e.getExpected().getStringRepresentation()); + assertEquals("[foo, bar, baz]", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have class"), e.getMessage()); + } + } + + @Test + void hasCountPass() { + page.setContent(""); + Locator locator = page.locator("option"); + assertThat(locator).hasCount(2); + } + + @Test + void hasCountFail() { + page.setContent(""); + Locator locator = page.locator("option"); + try { + assertThat(locator).hasCount(1, new LocatorAssertions.HasCountOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("1", e.getExpected().getStringRepresentation()); + assertEquals("2", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have count"), e.getMessage()); + } + } + + @Test + void hasCountPassZero() { + page.setContent("
"); + Locator locator = page.locator("span"); + assertThat(locator).hasCount(0); + assertThat(locator).not().hasCount(1); + } + + @Test + void hasCSSPass() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + assertThat(locator).hasCSS("color", "rgb(255, 0, 0)"); + } + + @Test + void hasCSSFail() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + try { + assertThat(locator).hasCSS("color", "red", new LocatorAssertions.HasCSSOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("red", e.getExpected().getStringRepresentation()); + assertEquals("rgb(255, 0, 0)", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have CSS"), e.getMessage()); + } + } + + @Test + void hasIdPass() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + assertThat(locator).hasId("node"); + } + + @Test + void hasIdFail() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + try { + assertThat(locator).hasId("foo", new LocatorAssertions.HasIdOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("foo", e.getExpected().getStringRepresentation()); + assertEquals("node", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have ID"), e.getMessage()); + } + } + + @Test + void hasJSPropertyPass() { + page.setContent("
"); + page.evalOnSelector("div", "e => e.foo = { a: 1, b: 'string' }"); + Locator locator = page.locator("div"); + assertThat(locator).hasJSProperty("foo", mapOf("a", 1, "b", "string")); + } + + @Test + void hasJSPropertyFail() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + try { + assertThat(locator).hasId("foo", new LocatorAssertions.HasIdOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("foo", e.getExpected().getStringRepresentation()); + assertEquals("node", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have ID"), e.getMessage()); + } + } + + @Test + void hasValueTextPass() { + page.setContent(""); + Locator locator = page.locator("#node"); + locator.fill("Text content"); + assertThat(locator).hasValue("Text content"); + } + + @Test + void hasValueTextFail() { + page.setContent(""); + Locator locator = page.locator("#node"); + locator.fill("Text content"); + try { + assertThat(locator).hasValue("Text2", new LocatorAssertions.HasValueOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("Text2", e.getExpected().getStringRepresentation()); + assertEquals("Text content", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have value"), e.getMessage()); + } + } + + @Test + void hasValueRegExpPass() { + page.setContent(""); + Locator locator = page.locator("#node"); + locator.fill("Text content"); + assertThat(locator).hasValue(Pattern.compile("Text")); + } + + @Test + void hasValueRegExpPassWithNot() { + page.setContent(""); + Locator locator = page.locator("#node"); + locator.fill("Text content"); + assertThat(locator).not().hasValue(Pattern.compile("Text2")); + } + + @Test + void hasValueRegExpFail() { + page.setContent(""); + Locator locator = page.locator("#node"); + locator.fill("Text content"); + try { + assertThat(locator).hasValue(Pattern.compile("Text2"), new LocatorAssertions.HasValueOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("Text2", e.getExpected().getStringRepresentation()); + assertEquals("Text content", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have value"), e.getMessage()); + } + } + + @Test + void isCheckedPass() { + page.setContent(""); + Locator locator = page.locator("input"); + assertThat(locator).isChecked(); + } + + @Test + void isCheckedFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).isChecked(new LocatorAssertions.IsCheckedOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be checked"), e.getMessage()); + } + } + + @Test + void notIsCheckedFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).not().isChecked(new LocatorAssertions.IsCheckedOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be checked"), e.getMessage()); + } + } + + @Test + void isDisabledPass() { + page.setContent(""); + Locator locator = page.locator("button"); + assertThat(locator).isDisabled(); + } + + @Test + void isDisabledFail() { + page.setContent(""); + Locator locator = page.locator("button"); + try { + assertThat(locator).isDisabled(new LocatorAssertions.IsDisabledOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be disabled"), e.getMessage()); + } + } + + @Test + void notIsDisabledFail() { + page.setContent(""); + Locator locator = page.locator("button"); + try { + assertThat(locator).not().isDisabled(new LocatorAssertions.IsDisabledOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be disabled"), e.getMessage()); + } + } + + @Test + void isEditablePass() { + page.setContent(""); + Locator locator = page.locator("input"); + assertThat(locator).isEditable(); + } + + @Test + void isEditableFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).isEditable(new LocatorAssertions.IsEditableOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be editable"), e.getMessage()); + } + } + + @Test + void notIsEditableFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).not().isEditable(new LocatorAssertions.IsEditableOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be editable"), e.getMessage()); + } + } + + + @Test + void isEmptyPass() { + page.setContent(""); + Locator locator = page.locator("input"); + assertThat(locator).isEmpty(); + } + + @Test + void isEmptyFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).isEmpty(new LocatorAssertions.IsEmptyOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be empty"), e.getMessage()); + } + } + + @Test + void notIsEmptyFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).not().isEmpty(new LocatorAssertions.IsEmptyOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be empty"), e.getMessage()); + } + } + + @Test + void isEnabledPass() { + page.setContent(""); + Locator locator = page.locator("button"); + assertThat(locator).isEnabled(); + } + + @Test + void isEnabledFail() { + page.setContent(""); + Locator locator = page.locator("button"); + try { + assertThat(locator).isEnabled(new LocatorAssertions.IsEnabledOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be enabled"), e.getMessage()); + } + } + + @Test + void notIsEnabledFail() { + page.setContent(""); + Locator locator = page.locator("button"); + try { + assertThat(locator).not().isEnabled(new LocatorAssertions.IsEnabledOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be enabled"), e.getMessage()); + } + } + + @Test + void isFocusedPass() { + page.setContent(""); + Locator locator = page.locator("input"); + locator.focus(); + assertThat(locator).isFocused(); + } + + @Test + void isFocusedFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).isFocused(new LocatorAssertions.IsFocusedOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be focused"), e.getMessage()); + } + } + + @Test + void notIsFocusedFail() { + page.setContent(""); + Locator locator = page.locator("input"); + locator.focus(); + try { + assertThat(locator).not().isFocused(new LocatorAssertions.IsFocusedOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be focused"), e.getMessage()); + } + } + + @Test + void isHiddenPass() { + page.setContent(""); + Locator locator = page.locator("button"); + assertThat(locator).isHidden(); + } + + @Test + void isHiddenFail() { + page.setContent(""); + Locator locator = page.locator("button"); + try { + assertThat(locator).isHidden(new LocatorAssertions.IsHiddenOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be hidden"), e.getMessage()); + } + } + + @Test + void notIsHiddenFail() { + page.setContent(""); + Locator locator = page.locator("button"); + try { + assertThat(locator).not().isHidden(new LocatorAssertions.IsHiddenOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be hidden"), e.getMessage()); + } + } + + @Test + void isVisiblePass() { + page.setContent(""); + Locator locator = page.locator("input"); + assertThat(locator).isVisible(); + } + + @Test + void isVisibleFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).isVisible(new LocatorAssertions.IsVisibleOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be visible"), e.getMessage()); + } + } + + @Test + void notIsVisibleFail() { + page.setContent(""); + Locator locator = page.locator("input"); + try { + assertThat(locator).not().isVisible(new LocatorAssertions.IsVisibleOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertNull(e.getExpected()); + assertNull(e.getActual()); + assertTrue(e.getMessage().contains("Locator expected to be visible"), e.getMessage()); + } + } } 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 7136f908..caef105e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorImpl.java @@ -431,10 +431,6 @@ class LocatorImpl implements Locator { JsonObject params = gson().toJsonTree(options).getAsJsonObject(); params.addProperty("selector", selector); params.addProperty("expression", expression); - if (options.expectedValue != null) { - params.remove("expectedValue"); - params.add("expectedValue", gson().toJsonTree(serializeArgument(options.expectedValue))); - } JsonElement json = frame.sendMessage("expect", params); FrameExpectResult result = gson().fromJson(json, FrameExpectResult.class); return result;