From e85258908e67b304c2baac28fd2e771c600f1bc4 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Thu, 28 Oct 2021 15:43:57 -0700 Subject: [PATCH] fix(assertions): include property name into message (#674) --- .../impl/LocatorAssertionsImpl.java | 6 +-- .../playwright/TestLocatorAssertions.java | 44 ++++++++++++++++--- 2 files changed, 41 insertions(+), 9 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 f0273f83..6943450f 100644 --- a/assertions/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java +++ b/assertions/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java @@ -100,7 +100,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse } FrameExpectOptions commonOptions = convertViaJson(options, FrameExpectOptions.class); commonOptions.expressionArg = name; - expectImpl("to.have.attribute", expectedText, expectedValue, "Locator expected to have attribute", commonOptions); + expectImpl("to.have.attribute", expectedText, expectedValue, "Locator expected to have attribute '" + name + "'", commonOptions); } @Override @@ -170,7 +170,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse } FrameExpectOptions commonOptions = convertViaJson(options, FrameExpectOptions.class); commonOptions.expressionArg = name; - expectImpl("to.have.css", expectedText, expectedValue, "Locator expected to have CSS", commonOptions); + expectImpl("to.have.css", expectedText, expectedValue, "Locator expected to have CSS property '" + name + "'", commonOptions); } @Override @@ -189,7 +189,7 @@ public class LocatorAssertionsImpl extends AssertionsBase implements LocatorAsse commonOptions.expressionArg = name; commonOptions.expectedValue = serializeArgument(value); List list = null; - expectImpl("to.have.property", list, value, "Locator expected to have js property", commonOptions); + expectImpl("to.have.property", list, value, "Locator expected to have JavaScript property '" + name + "'", commonOptions); } @Override diff --git a/assertions/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java b/assertions/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java index 9cc8f6f9..4dd58af4 100644 --- a/assertions/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java +++ b/assertions/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java @@ -17,6 +17,7 @@ package com.microsoft.playwright; import com.microsoft.playwright.assertions.LocatorAssertions; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.opentest4j.AssertionFailedError; @@ -212,7 +213,7 @@ public class TestLocatorAssertions extends TestBase { } catch (AssertionFailedError e) { assertEquals("foo", e.getExpected().getStringRepresentation()); assertEquals("node", e.getActual().getStringRepresentation()); - assertTrue(e.getMessage().contains("Locator expected to have attribute"), e.getMessage()); + assertTrue(e.getMessage().contains("Locator expected to have attribute 'id'"), e.getMessage()); } } @@ -367,7 +368,7 @@ public class TestLocatorAssertions extends TestBase { } 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()); + assertTrue(e.getMessage().contains("Locator expected to have CSS property 'color'"), e.getMessage()); } } @@ -401,19 +402,50 @@ public class TestLocatorAssertions extends TestBase { } @Test - void hasJSPropertyFail() { + void hasJSPropertyNumberFail() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + page.evalOnSelector("div", "e => e.foo = { a: 1, b: 'string' }"); + try { + assertThat(locator).hasJSProperty("foo", 1, new LocatorAssertions.HasJSPropertyOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + assertEquals("1", e.getExpected().getStringRepresentation()); + assertEquals("{a=1, b=string}", e.getActual().getStringRepresentation()); + assertTrue(e.getMessage().contains("Locator expected to have JavaScript property 'foo'"), e.getMessage()); + } + } + + @Test + void hasJSPropertyObjectFail() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + page.evalOnSelector("div", "e => e.foo = { a: 1, b: 'string' }"); + try { + assertThat(locator).hasJSProperty("foo", mapOf("a", 2), new LocatorAssertions.HasJSPropertyOptions().setTimeout(1000)); + fail("did not throw"); + } catch (AssertionFailedError e) { + 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()); + } + } + + @Test + @Disabled("https://github.com/microsoft/playwright/pull/9865") + void hasJSPropertyStringFail() { page.setContent("
Text content
"); Locator locator = page.locator("#node"); try { - assertThat(locator).hasId("foo", new LocatorAssertions.HasIdOptions().setTimeout(1000)); + assertThat(locator).hasJSProperty("id", "foo", new LocatorAssertions.HasJSPropertyOptions().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()); + assertTrue(e.getMessage().contains("Locator expected to have JavaScript property 'id'"), e.getMessage()); + throw e; } } - @Test void hasValueTextPass() { page.setContent("");