Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: custom test id per playwright instance #1489

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -79,7 +80,7 @@ class Root extends ChannelOwner {
super(connection, "Root", "");
}

Playwright initialize() {
PlaywrightImpl initialize() {
JsonObject params = new JsonObject();
params.addProperty("sdkLanguage", "java");
JsonElement result = sendMessage("initialize", params.getAsJsonObject());
Expand Down Expand Up @@ -177,7 +178,8 @@ private WaitableResult<JsonElement> internalSendMessage(String guid, String meth
}

public PlaywrightImpl initializePlaywright() {
return (PlaywrightImpl) this.root.initialize();
playwright = root.initialize();
return playwright;
}

LocalUtils localUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ public Locator getByRole(AriaRole role, GetByRoleOptions options) {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public Locator getByRole(AriaRole role, GetByRoleOptions options) {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ public Locator getByRole(AriaRole role, GetByRoleOptions options) {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
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);
Expand All @@ -29,7 +23,8 @@ private static String getByAttributeTextSelector(String attrName, Object value,
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
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;
Expand Down Expand Up @@ -64,8 +65,10 @@ public void register(String name, Path path, RegisterOptions options) {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ void getByTestIdShouldWork() {
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>");
Expand Down
Loading