-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* get main in sync with develop (#10) (#11) * Update to v2 (#13) * Simplify parameter resolvers and add APIRequestContext * Add tests for APITestContext. add ability to declare rest config at parameter level * Store APIContext after creating * Update readme * update pom to 2.0 * Bump version to v2.0
- Loading branch information
Showing
36 changed files
with
522 additions
and
234 deletions.
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
src/main/java/io/github/uchagani/jp/APIRequestContextParameterResolver.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,68 @@ | ||
package io.github.uchagani.jp; | ||
|
||
import com.microsoft.playwright.APIRequest; | ||
import com.microsoft.playwright.APIRequestContext; | ||
import com.microsoft.playwright.Playwright; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
import static io.github.uchagani.jp.AnnotationUtils.isAnnotationPresentOnClassOrMethod; | ||
import static io.github.uchagani.jp.ExtensionUtils.*; | ||
|
||
public class APIRequestContextParameterResolver implements ParameterResolver { | ||
private static final String id = ".apiRequestContext."; | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
Class<?> parameterType = parameterContext.getParameter().getType(); | ||
return parameterType.equals(APIRequestContext.class) && (parameterContext.isAnnotated(UseRestConfig.class) | ||
|| isAnnotationPresentOnClassOrMethod(extensionContext, UseRestConfig.class)); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return getAPIRequestContext(parameterContext, extensionContext); | ||
} | ||
|
||
public static void closeAPIRequestContext(ExtensionContext extensionContext) { | ||
APIRequestContext apiRequestContext = getObjectFromStore(extensionContext, id, APIRequestContext.class); | ||
if (apiRequestContext != null) { | ||
apiRequestContext.dispose(); | ||
} | ||
} | ||
|
||
private static Playwright getPlaywright(ExtensionContext extensionContext) { | ||
try { | ||
return PlaywrightParameterResolver.getPlaywright(extensionContext); | ||
} catch (Exception ex) { | ||
Playwright playwright = Playwright.create(); | ||
PlaywrightParameterResolver.savePlaywrightInStore(extensionContext, playwright); | ||
return playwright; | ||
} | ||
} | ||
|
||
public static APIRequestContext getAPIRequestContext(ParameterContext parameterContext, ExtensionContext extensionContext) { | ||
APIRequestContext apiRequestContext = getObjectFromStore(extensionContext, id, APIRequestContext.class); | ||
if (apiRequestContext == null) { | ||
RestConfig restConfig = getRestConfig(parameterContext, extensionContext); | ||
Playwright playwright = getPlaywright(extensionContext); | ||
apiRequestContext = createAPIRequestContext(playwright, restConfig); | ||
saveAPIRequestContextInStore(extensionContext, apiRequestContext); | ||
} | ||
return apiRequestContext; | ||
} | ||
|
||
public static void saveAPIRequestContextInStore(ExtensionContext extensionContext, APIRequestContext apiRequestContext) { | ||
saveObjectInStore(extensionContext, id, apiRequestContext); | ||
} | ||
|
||
private static APIRequestContext createAPIRequestContext(Playwright playwright, RestConfig restConfig) { | ||
APIRequest.NewContextOptions options = restConfig.getAPIRequestContextOptions(); | ||
if (options == null) { | ||
return playwright.request().newContext(); | ||
} | ||
return playwright.request().newContext(options); | ||
} | ||
} |
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,27 @@ | ||
package io.github.uchagani.jp; | ||
|
||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
public class AnnotationUtils { | ||
static boolean isAnnotationPresentOnClassOrMethod(ExtensionContext extensionContext, Class<? extends Annotation> annotation) { | ||
return extensionContext.getRequiredTestClass().isAnnotationPresent(annotation) || | ||
extensionContext.getRequiredTestMethod().isAnnotationPresent(annotation); | ||
} | ||
|
||
static void ensureAnnotationIsPresentOnClassOrMethod(ExtensionContext extensionContext, Class<? extends Annotation> annotation) { | ||
if (!isAnnotationPresentOnClassOrMethod(extensionContext, annotation)) { | ||
String message = String.format("Class or Method is not annotated with %s", annotation.getName()); | ||
throw new RuntimeException(message); | ||
} | ||
} | ||
|
||
static void ensureAnnotationIsPresentOnClassOrMethodOrParameter(ParameterContext parameterContext, ExtensionContext extensionContext, Class<? extends Annotation> annotation) { | ||
if (!isAnnotationPresentOnClassOrMethod(extensionContext, annotation) && !parameterContext.isAnnotated(annotation)) { | ||
String message = String.format("Class or Method or Parameter is not annotated with %s", annotation.getName()); | ||
throw new RuntimeException(message); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.