-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created an extension to handle server lifecycle instead of duplicatin…
…g code
- Loading branch information
Showing
4 changed files
with
56 additions
and
39 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
playwright/src/test/java/com/microsoft/playwright/FixtureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
34 changes: 34 additions & 0 deletions
34
playwright/src/test/java/com/microsoft/playwright/ServerLifecycle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters