Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn Citrus variable into Java variable

I am facing a problem: I need to use the value of a citrus variable as a parameter for a Java function. I am trying to make with contexts and behaviors, but nothing seems to work.

import com.playtika.hof.mgs.config.Context;
import org.springframework.http.HttpStatus;
import com.consol.citrus.http.client.HttpClient;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class CreateSessionBehavior extends BaseBehavior {

private HttpClient httpClient;
private Long uid;
private String sidVar;
private Context appContext;

public void apply() {
    // Create Session ID
    http().client(httpClient).send()
            .post("/utils/hof/redis/sessions?uid=" + uid);

    http().client(httpClient).receive().response(HttpStatus.OK)
            .extractFromPayload("$.sid", sidVar).validationCallback((response, context) -> {
        appContext.addValue(sidVar, context.getVariable(sidVar));
    });

    echo("New session in variable " + sidVar + ": ${" + sidVar + "}");

    }
 }

Here I need to use sidVar:

import com.playtika.hof.mgs.Kafkaproducer;
import com.playtika.hof.mgs.behavior.CreateSessionBehavior;
import com.playtika.hof.mgs.config.Context;
import org.testng.annotations.Test;
import com.consol.citrus.annotations.CitrusTest;
import com.playtika.hof.mgs.AbstractMGSTest;

@Test
public class TestPublishMessage extends AbstractMGSTest {

@CitrusTest(name = "Test Publish Message")

public void testPublishMessage() {

    description("Publish messages on Kafka");

    Context appContext = new Context();

    applyBehavior(new CreateSessionBehavior(testServer(), getValidUid(0),
            sessionName(0), appContext));

    String sessionId = appContext.getValue(sidVar);

  }
}

Is there a way of returning the sidVar variable so that I can use it wherever I need it?

Thank you very much.

like image 979
GHM Avatar asked Jan 22 '26 04:01

GHM


1 Answers

Citrus saves all variables to a common test context that is valid for the whole test case execution. You can inject the Citrus test context to your implementation as test method parameter:

public class TestContextInjectionIT extends JUnit4CitrusTestRunner {
    @Test
    @CitrusTest
    public void contextInjection(@CitrusResource TestContext context) {
        context.setVariable("message", "Hello, I am a variable!");

        echo("${message}");

        String message = context.getVariable("message");
        log.info(message);
    }
}

Please notice that I have used the test runner instead of test designer. This is because the runner test actions are executed immediately so the test context changes are visible right after the action has performed.

In addition to that all test actions do have access to the test context, too. So you can just add a custom action via Java DSL and access the test context.

public class TestContextAccessIT extends JUnit4CitrusTestRunner {
    @Test
    @CitrusTest
    public void contextInjection() {
        variable("message", "Hello, I am a variable!");

        echo("${message}");

        run(new AbstractTestAction() {
            @Override
            public void doExecute(TestContext context) {
                String message = context.getVariable("message");
                log.info(message);
            }
        });      
    }
}
like image 63
Christoph Deppisch Avatar answered Jan 23 '26 21:01

Christoph Deppisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!