chore: send testIdAttributeName to server (#1490)

This commit is contained in:
Yury Semikhatsky 2024-02-12 15:33:51 -08:00 committed by GitHub
parent 23aa10690e
commit d38bae17d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 49 additions and 9 deletions

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -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("<div><div data-testid='Hello'>Hello world</div></div>");
@ -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("" +
" <div>\n" +
" <div></div>\n" +
" <div>\n" +
" <div></div>\n" +
" <div></div>\n" +
" </div>\n" +
" </div>\n" +
" <div>\n" +
" <div class='foo bar:0' data-custom-id='One'>\n" +
" </div>\n" +
" <div class='foo bar:1' data-custom-id='Two'>\n" +
" </div>\n" +
" </div>");
PlaywrightException e = assertThrows(PlaywrightException.class, () -> page.locator(".foo").hover());
assertTrue(e.getMessage().contains("strict mode violation"), e.getMessage());
assertTrue(e.getMessage().contains("<div class=\"foo bar:0"), e.getMessage());
assertTrue(e.getMessage().contains("<div class=\"foo bar:1"), e.getMessage());
assertTrue(e.getMessage().contains("aka getByTestId(\"One\")"), e.getMessage());
assertTrue(e.getMessage().contains("aka getByTestId(\"Two\")"), e.getMessage());
}
@Test
void getByTestIdShouldEscapeId() {
page.setContent("<div><div data-testid='He\"llo'>Hello world</div></div>");