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("