Skip to content

Commit 270bc99

Browse files
authored
docs: junit fixture javadocs (#1510)
Reference #1369
1 parent 942a281 commit 270bc99

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

playwright/src/main/java/com/microsoft/playwright/junit/Options.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121
import com.microsoft.playwright.BrowserType;
2222
import com.microsoft.playwright.Playwright;
2323

24+
/**
25+
* <strong>NOTE:</strong> this API is experimental and is subject to changes.
26+
*
27+
* <p> Instances of this class are expected to be created by custom {@link OptionsFactory}
28+
* implementations. Implement custom factories to provide custom Playwright configurations.
29+
*
30+
* <p> For more details and usage examples see our
31+
* <a href="https://playwright.dev/java/docs/junit">JUnit guide</a>.
32+
*/
2433
public class Options {
2534
public String baseUrl;
2635
public String channel;

playwright/src/main/java/com/microsoft/playwright/junit/OptionsFactory.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,47 @@
1616

1717
package com.microsoft.playwright.junit;
1818

19+
/**
20+
* <strong>NOTE:</strong> this API is experimental and is subject to changes.
21+
*
22+
* <p> Implement this interface to pass custom options to {@link UsePlaywright}
23+
* annotation.
24+
*
25+
* <p> An example of implementing {@code @OptionsFactory}:
26+
* <pre>{@code
27+
* import com.microsoft.playwright.junit.Options;
28+
* import com.microsoft.playwright.junit.OptionsFactory;
29+
* import com.microsoft.playwright.junit.UsePlaywright;
30+
*
31+
* @UsePlaywright(MyTest.CustomOptions.class)
32+
* public class MyTest {
33+
*
34+
* public static class CustomOptions implements OptionsFactory {
35+
* @Override
36+
* public Options getOptions() {
37+
* return new Options()
38+
* .setHeadless(false)
39+
* .setContextOption(new Browser.NewContextOptions()
40+
* .setBaseURL("https://github.com"))
41+
* .setApiRequestOptions(new APIRequest.NewContextOptions()
42+
* .setBaseURL("https://playwright.dev"));
43+
* }
44+
* }
45+
*
46+
* @Test
47+
* public void testWithCustomOptions(Page page, APIRequestContext request) {
48+
* page.navigate("/");
49+
* assertThat(page).hasURL(Pattern.compile("github"));
50+
*
51+
* APIResponse response = request.get("/");
52+
* assertTrue(response.text().contains("Playwright"));
53+
* }
54+
* }
55+
* }</pre>
56+
*
57+
* <p>For more details and usage examples see our
58+
* <a href="https://playwright.dev/java/docs/junit">JUnit guide</a>.
59+
*/
1960
public interface OptionsFactory {
2061
Options getOptions();
2162
}

playwright/src/main/java/com/microsoft/playwright/junit/UsePlaywright.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.microsoft.playwright.junit;
1818

19+
import com.microsoft.playwright.Browser;
1920
import com.microsoft.playwright.junit.impl.*;
2021
import org.junit.jupiter.api.extension.ExtendWith;
2122

@@ -24,6 +25,57 @@
2425
import java.lang.annotation.RetentionPolicy;
2526
import java.lang.annotation.Target;
2627

28+
/**
29+
* <strong>NOTE:</strong> this API is experimental and is subject to changes.
30+
*
31+
* Use {@code @UsePlaywright} annotation to automatically manage Playwright objects
32+
* used in your test. Custom configuration can be provided by implementing
33+
* {@link OptionsFactory} and passing the class as a parameter.
34+
*
35+
* <p> When a test class is annotated with {@code @UsePlaywright} each test method can
36+
* use any of the following arguments that will be automatically created at run time:
37+
* <ul>
38+
* <li> {@link com.microsoft.playwright.Page Page page}</li>
39+
* <li> {@link com.microsoft.playwright.BrowserContext BrowserContext context}</li>
40+
* <li> {@link com.microsoft.playwright.Browser Browser browser}</li>
41+
* <li> {@link com.microsoft.playwright.APIRequestContext APIRequestContext request}</li>
42+
* <li> {@link com.microsoft.playwright.Playwright Playwright playwright}</li>
43+
* </ul>
44+
* {@code Page} and {@code BrowserContext} are created before each test and closed
45+
* after the test has finished. {@code Browser} and {@code Playwright} are reused
46+
* between tests for better efficiency.
47+
*
48+
* <p> An example of using {@code @UsePlaywright} annotation:
49+
* <pre>{@code
50+
* import com.microsoft.playwright.Browser;
51+
* import com.microsoft.playwright.BrowserContext;
52+
* import com.microsoft.playwright.Page;
53+
* import org.junit.jupiter.api.Test;
54+
*
55+
* import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
56+
* import static org.junit.jupiter.api.Assertions.assertEquals;
57+
* import static org.junit.jupiter.api.Assertions.assertNotNull;
58+
*
59+
* @UsePlaywright
60+
* public class TestExample {
61+
* @Test
62+
* void shouldProvidePage(Page page) {
63+
* page.navigate("https://playwright.dev");
64+
* assertThat(page).hasURL("https://playwright.dev/");
65+
* }
66+
*
67+
* @Test
68+
* void shouldResolvePlaywrightObjects(Page page, BrowserContext context, Browser browser) {
69+
* assertEquals(context, page.context());
70+
* assertEquals(browser, context.browser());
71+
* assertNotNull(browser.version());
72+
* }
73+
* }
74+
* }</pre>
75+
*
76+
* <p> For more details and usage examples see our
77+
* <a href="https://playwright.dev/java/docs/junit">JUnit guide</a>.
78+
*/
2779
@ExtendWith({OptionsExtension.class, PlaywrightExtension.class, BrowserExtension.class, BrowserContextExtension.class,
2880
PageExtension.class, APIRequestContextExtension.class})
2981
@Retention(RetentionPolicy.RUNTIME)

0 commit comments

Comments
 (0)