From d38bae17d31d786554484fcc18552fb1c4cd1679 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Mon, 12 Feb 2024 15:33:51 -0800 Subject: [PATCH] chore: send testIdAttributeName to server (#1490) --- .../playwright/impl/SelectorsImpl.java | 14 ++++---- .../playwright/impl/SharedSelectors.java | 12 +++++-- .../playwright/TestSelectorsGetBy.java | 32 +++++++++++++++++++ 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/SelectorsImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/SelectorsImpl.java index e1e4a063..77977bba 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/SelectorsImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/SelectorsImpl.java @@ -17,22 +17,16 @@ package com.microsoft.playwright.impl; import com.google.gson.JsonObject; -import com.microsoft.playwright.PlaywrightException; import com.microsoft.playwright.Selectors; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - import static com.microsoft.playwright.impl.Serialization.gson; -import static java.nio.charset.StandardCharsets.UTF_8; class SelectorsImpl extends ChannelOwner { SelectorsImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) { super(parent, type, guid, initializer); } - void registerImpl(String name, String script, Selectors.RegisterOptions options) { + void register(String name, String script, Selectors.RegisterOptions options) { if (options == null) { options = new Selectors.RegisterOptions(); } @@ -41,4 +35,10 @@ class SelectorsImpl extends ChannelOwner { params.addProperty("source", script); sendMessage("register", params); } + + void setTestIdAttributeName(String name) { + JsonObject params = new JsonObject(); + params.addProperty("testIdAttributeName", name); + sendMessageAsync("setTestIdAttributeName", params); + } } diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/SharedSelectors.java b/playwright/src/main/java/com/microsoft/playwright/impl/SharedSelectors.java index 0c012a9b..b654ed25 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/SharedSelectors.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/SharedSelectors.java @@ -69,10 +69,18 @@ public class SharedSelectors extends LoggingSupport implements Selectors { throw new PlaywrightException("Test id attribute cannot be null"); } testIdAttributeName = attributeName; + channels.forEach(channel -> channel.setTestIdAttributeName(testIdAttributeName)); } void addChannel(SelectorsImpl channel) { - registrations.forEach(r -> channel.registerImpl(r.name, r.script, r.options)); + registrations.forEach(r -> { + try { + channel.register(r.name, r.script, r.options); + } catch (PlaywrightException e) { + // This should not fail except for connection closure, but just in case we catch. + } + channel.setTestIdAttributeName(testIdAttributeName); + }); channels.add(channel); } @@ -81,7 +89,7 @@ public class SharedSelectors extends LoggingSupport implements Selectors { } private void registerImpl(String name, String script, RegisterOptions options) { - channels.forEach(impl -> impl.registerImpl(name, script, options)); + channels.forEach(impl -> impl.register(name, script, options)); registrations.add(new Registration(name, script, options)); } } diff --git a/playwright/src/test/java/com/microsoft/playwright/TestSelectorsGetBy.java b/playwright/src/test/java/com/microsoft/playwright/TestSelectorsGetBy.java index 2d1ea058..f2fcd5bf 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestSelectorsGetBy.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestSelectorsGetBy.java @@ -2,6 +2,7 @@ package com.microsoft.playwright; import com.microsoft.playwright.assertions.LocatorAssertions; import com.microsoft.playwright.options.AriaRole; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -12,6 +13,11 @@ import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.*; public class TestSelectorsGetBy extends TestBase { + + @AfterEach + void resetTestId() { + playwright.selectors().setTestIdAttribute("data-testid"); + } @Test void getByTestIdShouldWork() { page.setContent("
Hello world
"); @@ -29,6 +35,32 @@ public class TestSelectorsGetBy extends TestBase { assertThat(page.locator("div").getByTestId("Hello")).hasText("Hello world"); } + @Test + void shouldUseDataTestidInStrictErrors() { + playwright.selectors().setTestIdAttribute("data-custom-id"); + page.setContent("" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
\n" + + "
"); + PlaywrightException e = assertThrows(PlaywrightException.class, () -> page.locator(".foo").hover()); + assertTrue(e.getMessage().contains("strict mode violation"), e.getMessage()); + assertTrue(e.getMessage().contains("
Hello world
");