diff --git a/playwright/src/main/java/com/microsoft/playwright/junit/Options.java b/playwright/src/main/java/com/microsoft/playwright/junit/Options.java index d20cd8db..4371c15f 100644 --- a/playwright/src/main/java/com/microsoft/playwright/junit/Options.java +++ b/playwright/src/main/java/com/microsoft/playwright/junit/Options.java @@ -21,6 +21,15 @@ import com.microsoft.playwright.Browser; import com.microsoft.playwright.BrowserType; import com.microsoft.playwright.Playwright; +/** + * NOTE: this API is experimental and is subject to changes. + * + *

Instances of this class are expected to be created by custom {@link OptionsFactory} + * implementations. Implement custom factories to provide custom Playwright configurations. + * + *

For more details and usage examples see our + * JUnit guide. + */ public class Options { public String baseUrl; public String channel; diff --git a/playwright/src/main/java/com/microsoft/playwright/junit/OptionsFactory.java b/playwright/src/main/java/com/microsoft/playwright/junit/OptionsFactory.java index 10a1b357..52a7907e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/junit/OptionsFactory.java +++ b/playwright/src/main/java/com/microsoft/playwright/junit/OptionsFactory.java @@ -16,6 +16,47 @@ package com.microsoft.playwright.junit; +/** + * NOTE: this API is experimental and is subject to changes. + * + *

Implement this interface to pass custom options to {@link UsePlaywright} + * annotation. + * + *

An example of implementing {@code @OptionsFactory}: + *

{@code
+ * import com.microsoft.playwright.junit.Options;
+ * import com.microsoft.playwright.junit.OptionsFactory;
+ * import com.microsoft.playwright.junit.UsePlaywright;
+ *
+ * @UsePlaywright(MyTest.CustomOptions.class)
+ * public class MyTest {
+ *
+ *   public static class CustomOptions implements OptionsFactory {
+ *     @Override
+ *     public Options getOptions() {
+ *       return new Options()
+ *           .setHeadless(false)
+ *           .setContextOption(new Browser.NewContextOptions()
+ *               .setBaseURL("https://github.com"))
+ *           .setApiRequestOptions(new APIRequest.NewContextOptions()
+ *               .setBaseURL("https://playwright.dev"));
+ *     }
+ *   }
+ *
+ *   @Test
+ *   public void testWithCustomOptions(Page page, APIRequestContext request) {
+ *     page.navigate("/");
+ *     assertThat(page).hasURL(Pattern.compile("github"));
+ *
+ *     APIResponse response = request.get("/");
+ *     assertTrue(response.text().contains("Playwright"));
+ *   }
+ * }
+ * }
+ * + *

For more details and usage examples see our + * JUnit guide. + */ public interface OptionsFactory { Options getOptions(); } diff --git a/playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java b/playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java index aa58ba1a..787e2c7c 100644 --- a/playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java +++ b/playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java @@ -16,6 +16,7 @@ package com.microsoft.playwright.junit; +import com.microsoft.playwright.Browser; import com.microsoft.playwright.junit.impl.*; import org.junit.jupiter.api.extension.ExtendWith; @@ -24,6 +25,57 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +/** + * NOTE: this API is experimental and is subject to changes. + * + * Use {@code @UsePlaywright} annotation to automatically manage Playwright objects + * used in your test. Custom configuration can be provided by implementing + * {@link OptionsFactory} and passing the class as a parameter. + * + *

When a test class is annotated with {@code @UsePlaywright} each test method can + * use any of the following arguments that will be automatically created at run time: + *

+ * {@code Page} and {@code BrowserContext} are created before each test and closed + * after the test has finished. {@code Browser} and {@code Playwright} are reused + * between tests for better efficiency. + * + *

An example of using {@code @UsePlaywright} annotation: + *

{@code
+ * import com.microsoft.playwright.Browser;
+ * import com.microsoft.playwright.BrowserContext;
+ * import com.microsoft.playwright.Page;
+ * import org.junit.jupiter.api.Test;
+ *
+ * import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
+ * import static org.junit.jupiter.api.Assertions.assertEquals;
+ * import static org.junit.jupiter.api.Assertions.assertNotNull;
+ *
+ * @UsePlaywright
+ * public class TestExample {
+ *   @Test
+ *   void shouldProvidePage(Page page) {
+ *     page.navigate("https://playwright.dev");
+ *     assertThat(page).hasURL("https://playwright.dev/");
+ *   }
+ *
+ *   @Test
+ *   void shouldResolvePlaywrightObjects(Page page, BrowserContext context, Browser browser) {
+ *     assertEquals(context, page.context());
+ *     assertEquals(browser, context.browser());
+ *     assertNotNull(browser.version());
+ *   }
+ * }
+ * }
+ * + *

For more details and usage examples see our + * JUnit guide. + */ @ExtendWith({OptionsExtension.class, PlaywrightExtension.class, BrowserExtension.class, BrowserContextExtension.class, PageExtension.class, APIRequestContextExtension.class}) @Retention(RetentionPolicy.RUNTIME)