feat(junit): testIdAttribute option (#1491)
This commit is contained in:
Родитель
d38bae17d3
Коммит
bd97c96707
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче