I'm using Citrus Framevork and have some pre-test steps to get auth-token and then use it in whole test plan. And don't clearly understand usage of TestContext in this case and how to access var. thru several testIT classes:
GetTokenIT.java:
http()
.client(HttpTqaClient)
.receive()
.response(HttpStatus.OK)
.messageType(MessageType.JSON)
.extractFromHeader("Authorization", "header_token")
.extractFromPayload("$.id_token", "payload_token");
action(new AbstractTestAction() {
@Override
public void doExecute(TestContext context) {
String token = context.getVariable("payload_token");
System.out.println("where is my token?" +token);
//Result: DEBUG port.LoggingReporter|where is my token?eyJhbGciOiJIUzUxMiJ9.
That part works fine only in GetTokenIT.class. How to pass/call variable token in nex test steps? GetClientIdIT.java:
public class GetClientIdIT extends TestNGCitrusTestDesigner {
@Autowired
private HttpClient HttpTqaClient;
@Test
@Parameters("context")
@CitrusTest(name = "GetClientId")
public void testGet(@Optional @CitrusResource TestContext context)
//HOW TO CALL VARIABLE "TOKEN" HERE?
System.out.println("where is my token?" +token);
http()
.client(HttpTqaClient)
.send()
.get("/account/api/lk/lk-client/current")
.accept("application/json")
.contentType("application/json")
.header("Authorization", "${token}");
http()
.client(HttpTqaClient)
.receive()
.response(HttpStatus.OK)
.messageType(MessageType.JSON);
In Citrus you can execute actions before the entire test suite with a TestDesignerBeforeSuiteSupport
. Like this:
public class SetupAuthTokenBeforeSuite extends TestDesignerBeforeSuiteSupport {
@Override
public void beforeSuite(TestDesigner designer) {
designer.echo("Setting up authentication token");
designer.http()
.client(HttpTqaClient)
.send()
...
designer.http()
.client(HttpTqaClient)
.receive()
.response(HttpStatus.OK)
.messageType(MessageType.JSON)
.extractFromHeader("Authorization", "header_token")
.extractFromPayload("$.id_token", "payload_token");
designer.action(new AbstractTestAction() {
@Override public void doExecute(TestContext testContext) {
testContext.getGlobalVariables().put("global_auth_token", "${payload_token}");
}
});
}
}
No matter what tests or how many from your test suite you run, this will always be picked up by Citrus and executed before any test is run. You only need to configure this as a bean inside your Citrus context.
The trick is to set a global variable with the value of the extracted variable, like in the example above. After that you can use this variable inside any test like this:
http()
.client(HttpTqaClient)
.send()
.get("/account/api/lk/lk-client/current")
.accept("application/json")
.contentType("application/json")
.header("Authorization", "${global_auth_token}");
I must ask though, which version of Citrus are you using? It is recommended that you use the TestNGCitrusTestRunner
instead of the TestNGCitrusTestDesigner
and therefore the TestRunnerBeforeSuiteSupport
instead of the TestDesignerBeforeSuiteSupport
.
来源:https://stackoverflow.com/questions/49429085/how-should-i-pass-variable-extracted-from-payload-thru-test-classes