Skip to content

Commit

Permalink
Created an extension to handle server lifecycle instead of duplicatin…
Browse files Browse the repository at this point in the history
…g code
  • Loading branch information
uchagani committed Jan 21, 2024
1 parent 198afc3 commit a0cd499
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 39 deletions.
16 changes: 16 additions & 0 deletions playwright/src/test/java/com/microsoft/playwright/FixtureTest.java
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,22 @@
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.io.IOException;
import java.util.regex.Pattern;

import static com.microsoft.playwright.Utils.nextFreePort;
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 {
private static Server server;

public static class CustomOptions implements OptionsFactory {
@Override
public Options getOptions() {
return new Options().setBaseUrl(server.EMPTY_PAGE).setBrowserName("firefox");
}
}

@BeforeAll
static void beforeAll() throws IOException {
server = Server.createHttp(nextFreePort());
}

@AfterAll
static void afterAll() {
if (server != null) {
server.stop();
server = null;
return new Options().setBaseUrl(serverMap.get(TestPlaywrightCustomFixtures.class).EMPTY_PAGE).setBrowserName("firefox");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,21 @@
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.io.IOException;
import java.util.regex.Pattern;

import static com.microsoft.playwright.Utils.nextFreePort;
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 {
private static Server server;

public static class CustomOptions implements OptionsFactory {
@Override
public Options getOptions() {
return new Options().setChannel("chrome").setApiRequestOptions(new APIRequest.NewContextOptions().setBaseURL(server.EMPTY_PAGE)).setContextOption(new Browser.NewContextOptions().setBaseURL(server.EMPTY_PAGE));
}
}

@BeforeAll
static void beforeAll() throws IOException {
server = Server.createHttp(nextFreePort());
}

@AfterAll
static void afterAll() {
if (server != null) {
server.stop();
server = null;
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));
}
}

Expand Down

0 comments on commit a0cd499

Please sign in to comment.