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

前端 未结 1 1058
滥情空心
滥情空心 2021-01-07 12:56

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

相关标签:
1条回答
  • 2021-01-07 13:48

    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.

    0 讨论(0)
提交回复
热议问题