我应该如何通过测试类传递从Payload中提取的变量? [英] How should i pass variable extracted From Payload thru test classes?

查看:158
本文介绍了我应该如何通过测试类传递从Payload中提取的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Citrus Framevork ,并有一些预测试步骤来获取auth-token,然后在整个测试计划中使用它。并且在这种情况下不清楚地理解 TestContext 的用法以及如何访问var。通过几个testIT类:

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.

该部分仅在GetTokenIT.class中正常工作。
如何传递/调用变量令牌在nex测试步骤中?
GetClientIdIT.java:

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);


推荐答案

在Citrus中,您可以在整个测试套件之前执行操作使用 TestDesignerBeforeSuiteSupport 。像这样:

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}");
            }
        });
    }
}

无论您的测试套件有多少测试或多少测试你运行,这将始终被Citrus选中并在任何测试运行之前执行。您只需要在Citrus上下文中将其配置为bean。

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}");

我必须问,你使用的是哪个版本的Citrus?建议您使用 TestNGCitrusTestRunner 而不是 TestNGCitrusTestDesigner ,因此 TestRunnerBeforeSuiteSupport 而不是 TestDesignerBeforeSuiteSupport

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.

这篇关于我应该如何通过测试类传递从Payload中提取的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆