Skip to content

[Feature]: JUnit5: Page and BrowserContext fixture support in BeforeAll/AfterAll methods #1781

Open
@Sveshais

Description

@Sveshais

🚀 Feature Request

Current Page and BrowserContext JUnit fixture implementation prevents using these fixtures in BeforeAll/AfterAll methods while APIRequestContext does not have this restriction. This makes using Page for class-wide setup/teardown actions unnecessarily cumbersome.

Please consider supporting either an isolated Page and BrowserContext for static BeforeAll/AfterAll or even shared ones when used with @TestInstance(TestInstance.Lifecycle.PER_CLASS), if possible.

Example

package tests;

import 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.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.regex.Pattern;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

// re-purposing example from the documentation
@UsePlaywright(PlaywrightTest.CustomOptions.class)
public class PlaywrightTest {
    public static class CustomOptions implements OptionsFactory {
        @Override
        public Options getOptions() {
            return new Options()
                    .setHeadless(true)
                    .setContextOptions(new Browser.NewContextOptions()
                            .setBaseURL("https://github.com"))
                    .setApiRequestOptions(new APIRequest.NewContextOptions()
                            .setBaseURL("https://playwright.dev"));
        }
    }

    @BeforeAll
    public static void setup(APIRequestContext request) {
        // this works
        APIResponse response = request.get("/");
        assertThat(response).isOK();
    }

    @Test
    public void testWithCustomOptions() {
        assert true;
    }

//    @AfterAll
//    public static void teardown(Page page) {
//        // ParameterResolutionException
//    }

    @AfterAll
    public static void teardown(Browser browser) {
        // this kinda works, but base url from CustomOptions is not applied
        Page page = browser.newPage();
        // navigating to "/", waiting until "load"
        page.navigate("/");
        assertThat(page).hasURL(Pattern.compile("github"));
    }
}

Motivation

There are cases when using browser would be convenient in test setup/teardown context. Right now this cannot be done by using Page or BrowserContext fixtures directly, but using creating new Page from Browser fixture requires fiddling with options.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions