mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-12-30 19:40:44 +00:00
Remove setters from Options. Add tests for custom fixtures (#1436)
This commit is contained in:
parent
ffe2bd4a96
commit
c4e1f898e6
@ -0,0 +1,5 @@
|
||||
package com.microsoft.playwright.junit;
|
||||
|
||||
public interface OptionsFactory {
|
||||
Options getOptions();
|
||||
}
|
||||
@ -13,5 +13,5 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface UsePlaywright {
|
||||
Class<? extends Options> options() default Options.class;
|
||||
Class<? extends OptionsFactory> value() default DefaultOptions.class;
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ public class BrowserExtension implements ParameterResolver, AfterAllCallback {
|
||||
}
|
||||
|
||||
if (options.getChannel() != null) {
|
||||
options.setChannel(options.getChannel());
|
||||
launchOptions.setChannel(options.getChannel());
|
||||
}
|
||||
|
||||
return launchOptions;
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
package com.microsoft.playwright.junit.impl;
|
||||
|
||||
import com.microsoft.playwright.junit.Options;
|
||||
import com.microsoft.playwright.junit.OptionsFactory;
|
||||
|
||||
public class DefaultOptions implements OptionsFactory {
|
||||
@Override
|
||||
public Options getOptions() {
|
||||
return new Options();
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ package com.microsoft.playwright.junit.impl;
|
||||
|
||||
import com.microsoft.playwright.PlaywrightException;
|
||||
import com.microsoft.playwright.junit.Options;
|
||||
import com.microsoft.playwright.junit.OptionsFactory;
|
||||
import com.microsoft.playwright.junit.UsePlaywright;
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
@ -24,7 +25,7 @@ public class OptionsExtension implements AfterAllCallback {
|
||||
|
||||
UsePlaywright usePlaywrightAnnotation = getUsePlaywrightAnnotation(extensionContext);
|
||||
try {
|
||||
options = usePlaywrightAnnotation.options().newInstance();
|
||||
options = usePlaywrightAnnotation.value().newInstance().getOptions();
|
||||
threadLocalOptions.set(options);
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new PlaywrightException("Failed to create options", e);
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@ExtendWith(ServerLifecycle.class)
|
||||
@Tag("fixtures")
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface FixtureTest {
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.microsoft.playwright.Utils.nextFreePort;
|
||||
|
||||
public class ServerLifecycle implements BeforeAllCallback, AfterAllCallback {
|
||||
// This is a public map so that objects outside test scope can access the server.
|
||||
// For example, nested classes inside test classes that define custom options and need the server.
|
||||
public static Map<Class<?>, Server> serverMap;
|
||||
|
||||
static {
|
||||
serverMap = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterAll(ExtensionContext extensionContext) {
|
||||
Server server = serverMap.get(extensionContext.getRequiredTestClass());
|
||||
if (server != null) {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeAll(ExtensionContext extensionContext) throws Exception {
|
||||
Server server = Server.createHttp(nextFreePort());
|
||||
serverMap.put(extensionContext.getRequiredTestClass(), server);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import com.microsoft.playwright.junit.Options;
|
||||
import com.microsoft.playwright.junit.OptionsFactory;
|
||||
import com.microsoft.playwright.junit.UsePlaywright;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.microsoft.playwright.ServerLifecycle.serverMap;
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@FixtureTest
|
||||
@UsePlaywright(TestPlaywrightCustomFixtures.CustomOptions.class)
|
||||
public class TestPlaywrightCustomFixtures {
|
||||
|
||||
public static class CustomOptions implements OptionsFactory {
|
||||
@Override
|
||||
public Options getOptions() {
|
||||
return new Options().setBaseUrl(serverMap.get(TestPlaywrightCustomFixtures.class).EMPTY_PAGE).setBrowserName("firefox");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomBrowser(Browser browser) {
|
||||
assertEquals(browser.browserType().name(), "firefox");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseUrl(Page page) {
|
||||
page.navigate("/");
|
||||
assertThat(page).hasURL(Pattern.compile("localhost"));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import com.microsoft.playwright.junit.Options;
|
||||
import com.microsoft.playwright.junit.OptionsFactory;
|
||||
import com.microsoft.playwright.junit.UsePlaywright;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.microsoft.playwright.ServerLifecycle.serverMap;
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@FixtureTest
|
||||
@UsePlaywright(TestPlaywrightCustomOptionFixtures.CustomOptions.class)
|
||||
public class TestPlaywrightCustomOptionFixtures {
|
||||
public static class CustomOptions implements OptionsFactory {
|
||||
@Override
|
||||
public Options getOptions() {
|
||||
return new Options().setChannel("chrome").setApiRequestOptions(new APIRequest.NewContextOptions().setBaseURL(serverMap.get(TestPlaywrightCustomOptionFixtures.class).EMPTY_PAGE)).setContextOption(new Browser.NewContextOptions().setBaseURL(serverMap.get(TestPlaywrightCustomOptionFixtures.class).EMPTY_PAGE));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCustomBrowserContext(Page page) {
|
||||
page.navigate("/");
|
||||
assertThat(page).hasURL(Pattern.compile("localhost"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCustomAPIRequestOptions(APIRequestContext apiRequestContext) {
|
||||
APIResponse response = apiRequestContext.get("/");
|
||||
assertTrue(response.url().contains("localhost"));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user