mirror of
https://github.com/microsoft/playwright-java.git
synced 2026-01-01 21:21:37 +00:00
chore: send testIdAttributeName to server (#1490)
This commit is contained in:
parent
23aa10690e
commit
d38bae17d3
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -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>");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user