mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-12-29 02:40:42 +00:00
chore: custom test id per playwright instance (#1489)
This commit is contained in:
parent
72b36b46e2
commit
23aa10690e
@ -71,6 +71,7 @@ public class Connection {
|
||||
isLogging = (debug != null) && debug.contains("pw:channel");
|
||||
}
|
||||
LocalUtils localUtils;
|
||||
PlaywrightImpl playwright;
|
||||
final Map<String, String> env;
|
||||
private int tracingCount;
|
||||
|
||||
@ -79,7 +80,7 @@ public class Connection {
|
||||
super(connection, "Root", "");
|
||||
}
|
||||
|
||||
Playwright initialize() {
|
||||
PlaywrightImpl initialize() {
|
||||
JsonObject params = new JsonObject();
|
||||
params.addProperty("sdkLanguage", "java");
|
||||
JsonElement result = sendMessage("initialize", params.getAsJsonObject());
|
||||
@ -177,7 +178,8 @@ public class Connection {
|
||||
}
|
||||
|
||||
public PlaywrightImpl initializePlaywright() {
|
||||
return (PlaywrightImpl) this.root.initialize();
|
||||
playwright = root.initialize();
|
||||
return playwright;
|
||||
}
|
||||
|
||||
LocalUtils localUtils() {
|
||||
|
||||
@ -407,12 +407,12 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
|
||||
@Override
|
||||
public Locator getByTestId(String testId) {
|
||||
return locator(getByTestIdSelector(testId));
|
||||
return locator(getByTestIdSelector(testId, connection.playwright));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locator getByTestId(Pattern testId) {
|
||||
return locator(getByTestIdSelector(testId));
|
||||
return locator(getByTestIdSelector(testId, connection.playwright));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -82,12 +82,12 @@ class FrameLocatorImpl implements FrameLocator {
|
||||
|
||||
@Override
|
||||
public Locator getByTestId(String testId) {
|
||||
return locator(getByTestIdSelector(testId));
|
||||
return locator(getByTestIdSelector(testId, frame.connection.playwright));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locator getByTestId(Pattern testId) {
|
||||
return locator(getByTestIdSelector(testId));
|
||||
return locator(getByTestIdSelector(testId, frame.connection.playwright));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -278,12 +278,12 @@ class LocatorImpl implements Locator {
|
||||
|
||||
@Override
|
||||
public Locator getByTestId(String testId) {
|
||||
return locator(getByTestIdSelector(testId));
|
||||
return locator(getByTestIdSelector(testId, frame.connection.playwright));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locator getByTestId(Pattern testId) {
|
||||
return locator(getByTestIdSelector(testId));
|
||||
return locator(getByTestIdSelector(testId, frame.connection.playwright));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -9,12 +9,6 @@ import static com.microsoft.playwright.impl.Serialization.gson;
|
||||
import static com.microsoft.playwright.impl.Utils.toJsRegexFlags;
|
||||
|
||||
public class LocatorUtils {
|
||||
private static volatile String testIdAttributeName = "data-testid";;
|
||||
|
||||
static void setTestIdAttributeName(String name) {
|
||||
testIdAttributeName = name;
|
||||
}
|
||||
|
||||
static String getByTextSelector(Object text, Locator.GetByTextOptions options) {
|
||||
boolean exact = options != null && options.exact != null && options.exact;
|
||||
return "internal:text=" + escapeForTextSelector(text, exact);
|
||||
@ -29,7 +23,8 @@ public class LocatorUtils {
|
||||
return "internal:attr=[" + attrName + "=" + escapeForAttributeSelector(value, exact) + "]";
|
||||
}
|
||||
|
||||
static String getByTestIdSelector(Object testId) {
|
||||
static String getByTestIdSelector(Object testId, PlaywrightImpl playwright) {
|
||||
String testIdAttributeName = ((SharedSelectors) playwright.selectors()).testIdAttributeName;
|
||||
return getByAttributeTextSelector(testIdAttributeName, testId, true);
|
||||
}
|
||||
|
||||
|
||||
@ -25,13 +25,14 @@ import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.microsoft.playwright.impl.LocatorUtils.setTestIdAttributeName;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
public class SharedSelectors extends LoggingSupport implements Selectors {
|
||||
private final List<SelectorsImpl> channels = new ArrayList<>();
|
||||
private final List<Registration> registrations = new ArrayList<>();
|
||||
|
||||
String testIdAttributeName = "data-testid";
|
||||
|
||||
private static class Registration {
|
||||
final String name;
|
||||
final String script;
|
||||
@ -64,8 +65,10 @@ public class SharedSelectors extends LoggingSupport implements Selectors {
|
||||
|
||||
@Override
|
||||
public void setTestIdAttribute(String attributeName) {
|
||||
// TODO: set it per playwright insttance
|
||||
setTestIdAttributeName(attributeName);
|
||||
if (attributeName == null) {
|
||||
throw new PlaywrightException("Test id attribute cannot be null");
|
||||
}
|
||||
testIdAttributeName = attributeName;
|
||||
}
|
||||
|
||||
void addChannel(SelectorsImpl channel) {
|
||||
|
||||
@ -20,6 +20,15 @@ public class TestSelectorsGetBy extends TestBase {
|
||||
assertThat(page.locator("div").getByTestId("Hello")).hasText("Hello world");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getByTestIdWithCustomTestIdShouldWork() {
|
||||
page.setContent("<div><div data-my-custom-testid='Hello'>Hello world</div></div>");
|
||||
playwright.selectors().setTestIdAttribute("data-my-custom-testid");
|
||||
assertThat(page.getByTestId("Hello")).hasText("Hello world");
|
||||
assertThat(page.mainFrame().getByTestId("Hello")).hasText("Hello world");
|
||||
assertThat(page.locator("div").getByTestId("Hello")).hasText("Hello world");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getByTestIdShouldEscapeId() {
|
||||
page.setContent("<div><div data-testid='He\"llo'>Hello world</div></div>");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user