How should i pass variable extracted From Payload thru test classes?

十年热恋 提交于 2019-12-01 01:57:49

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!