-
Notifications
You must be signed in to change notification settings - Fork 0
@JPostmanRequest
@JPostmanRequest prepares a Postman request before a dependency method or test method runs.
It is useful when a method needs to prepare a request, execute custom code, return a cached value, or set up data for another response.
@JPostmanRequest(request = "Login user and get tokens")
public String getToken() {
return api.response(c -> RestAssuredExecutor.execute(c.request()))
.asserts(true)
.exists("accessToken", "Access token not found")
.verify()
.path("accessToken");
}The returned value is cached by dependency name unless cache is defined.
@JPostmanRequest(request = "Login user and get tokens")
public String getToken() {
return api.response(c -> RestAssuredExecutor.execute(c.request()))
.verify()
.path("accessToken");
}
@JPostmanExecutor(name = "auth", dependsOn = "getToken")
public ApiExecutor authExecutor(TestNgContext context) {
return RestAssuredExecutor.apply(context.request())
.auth()
.oauth2(context.cache("getToken"));
}Use cache to store the dependency result under a specific key.
@JPostmanRequest(
request = "Login user and get tokens",
cache = "token"
)
public String getToken() {
return api.response(c -> RestAssuredExecutor.execute(c.request()))
.verify()
.path("accessToken");
}Then use:
context.cache("token")@JPostmanRequest(
namespace = "product",
folder = "Product",
request = "Get all products"
)
public void prepareProductRequest() {
}-
namespaceselects the configured JPostman context. -
folderselects the Postman folder. -
requestselects the request inside the collection or folder.
Use rule to load a secure rule profile before preparing the request.
@JPostmanRequest(
namespace = "product",
folder = "Product",
request = "Get all products",
rule = "product"
)
public void prepareProductRequest() {
}dependsOn runs dependency methods before this method.
@JPostmanRequest(
request = "Get current auth user",
dependsOn = "getToken"
)
public void prepareAuthUser() {
}A void dependency can be used for setup-only work.
@JPostmanRequest(request = "Setup request")
public void setupRequest() {
api.cache("ready", true);
}If another request needs a value, return the value instead of using void.
Good token dependency:
@JPostmanRequest(request = "Login user and get tokens", cache = "token")
public String getToken() {
return api.response(c -> RestAssuredExecutor.execute(c.request()))
.verify()
.path("accessToken");
}@JPostmanTestNG
public class DemoTest {
@JPostmanTestContext
private TestNgContext api;
@JPostmanRequest(request = "Login user and get tokens", cache = "token")
public String getToken() {
return api.response(c -> RestAssuredExecutor.execute(c.request()))
.verify()
.path("accessToken");
}
@JPostmanExecutor(name = "auth", dependsOn = "getToken")
public ApiExecutor authExecutor(TestNgContext context) {
return RestAssuredExecutor.apply(context.request())
.auth()
.oauth2(context.cache("token"));
}
}@JPostmanRequest prepares a request and can run custom method code. It is especially useful for dependencies that produce cached values, such as login tokens.