feat(junit): testIdAttribute option (#1491)

This commit is contained in:
Yury Semikhatsky 2024-02-12 16:13:53 -08:00 коммит произвёл GitHub
Родитель d38bae17d3
Коммит bd97c96707
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
5 изменённых файлов: 29 добавлений и 3 удалений

Просмотреть файл

@ -11,6 +11,8 @@ public class Options {
public Boolean headless;
public String browserName;
public String deviceName;
// Custom attribute to be used in page.getByTestId(). data-testid is used by default.
public String testIdAttribute;
public BrowserType.LaunchOptions launchOptions;
public Browser.NewContextOptions contextOptions;
public APIRequest.NewContextOptions apiRequestOptions;
@ -45,6 +47,11 @@ public class Options {
return this;
}
public Options setTestIdAttribute(String name) {
this.testIdAttribute = name;
return this;
}
public Options setBrowserName(String browserName) {
this.browserName = browserName;
return this;

Просмотреть файл

@ -8,8 +8,7 @@ import com.microsoft.playwright.impl.Utils;
import com.microsoft.playwright.junit.Options;
import org.junit.jupiter.api.extension.*;
import static com.microsoft.playwright.junit.impl.ExtensionUtils.isClassHook;
import static com.microsoft.playwright.junit.impl.ExtensionUtils.isParameterSupported;
import static com.microsoft.playwright.junit.impl.ExtensionUtils.*;
public class BrowserContextExtension implements ParameterResolver, AfterEachCallback {
private static final ThreadLocal<BrowserContext> threadLocalBrowserContext = new ThreadLocal<>();
@ -41,6 +40,7 @@ public class BrowserContextExtension implements ParameterResolver, AfterEachCall
Options options = OptionsExtension.getOptions(extensionContext);
Playwright playwright = PlaywrightExtension.getOrCreatePlaywright(extensionContext);
setTestIdAttribute(playwright, options);
Browser browser = BrowserExtension.getOrCreateBrowser(extensionContext);
Browser.NewContextOptions contextOptions = getContextOptions(playwright, options);
browserContext = browser.newContext(contextOptions);

Просмотреть файл

@ -1,5 +1,7 @@
package com.microsoft.playwright.junit.impl;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.UsePlaywright;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
@ -27,4 +29,10 @@ class ExtensionUtils {
Class<?> clazz = parameterContext.getParameter().getType();
return subject.equals(clazz);
}
static void setTestIdAttribute(Playwright playwright, Options options) {
String testIdAttribute = options.testIdAttribute == null ? "data-testid" : options.testIdAttribute;
playwright.selectors().setTestIdAttribute(testIdAttribute);
}
}

Просмотреть файл

@ -5,6 +5,7 @@ import com.microsoft.playwright.junit.Options;
import org.junit.jupiter.api.extension.*;
import static com.microsoft.playwright.junit.impl.ExtensionUtils.isParameterSupported;
import static com.microsoft.playwright.junit.impl.ExtensionUtils.setTestIdAttribute;
public class PlaywrightExtension implements ParameterResolver, AfterAllCallback {
private static final ThreadLocal<Playwright> threadLocalPlaywright = new ThreadLocal<>();
@ -38,6 +39,7 @@ public class PlaywrightExtension implements ParameterResolver, AfterAllCallback
Options options = OptionsExtension.getOptions(extensionContext);
playwright = Playwright.create(options.getPlaywrightCreateOptions());
threadLocalPlaywright.set(playwright);
setTestIdAttribute(playwright, options);
return playwright;
}
}

Просмотреть файл

@ -19,7 +19,8 @@ public class TestFixtureOptions {
public Options getOptions() {
return new Options()
.setBaseUrl(serverMap.get(TestFixtureOptions.class).EMPTY_PAGE)
.setBrowserName("webkit");
.setBrowserName("webkit")
.setTestIdAttribute("data-my-custom-testid");
}
}
@ -33,4 +34,12 @@ public class TestFixtureOptions {
page.navigate("/");
assertThat(page).hasURL(Pattern.compile("localhost"));
}
@Test
void testCustomTestId(Page page) {
page.setContent("<div><div data-my-custom-testid='Hello'>Hello world</div></div>");
assertThat(page.getByTestId("Hello")).hasText("Hello world");
assertThat(page.mainFrame().getByTestId("Hello")).hasText("Hello world");
assertThat(page.locator("div").getByTestId("Hello")).hasText("Hello world");
}
}