Override beforeSuite() to fetch a token and use the variable in a test class

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Override beforeSuite() to fetch a token and use the variable in a test class

KiefersperreTurner
Hello,

I've been having an issue with my Test for a while now. I'm a beginner in the field and I am not too familiar with TestNG that I am using with the Citrus framework.

1. I've a test class "VehicletypeTestsIT" extending TestNGCitrusTestDesigner, that tests a point of intersection by sending and validating JSON messages. In order to be able to get past the sending, I must login beforehand and send a token as part of the http header.

http()
                .client(connectorClient)
                .send()
                .post("address")
                .header("X-Authorization", "{$token}") (1)
                .messageType(MessageType.JSON)
                .contentType(ContentType.APPLICATION_JSON.getMimeType())
                .payload(String.valueOf(createVehicle));

2. I've a second class, that's a support class to "VehicletypeTestsIT" called "SetupAuthToken", which extends TestDesignerBeforeSuiteSupport. This class overrides the beforeSuite(); method, uses a valid login and validates an incoming JSON message and save the token received.

public HttpClient connectorClient;

        public HttpClient getConnectorClient() {
            [...]
        }

        @Override
        public void beforeSuite(TestDesigner designer) {
            designer.echo("Setting up authentication token");

            connectorClient = getConnectorClient();

            requestERPLogin ERPLogin = 

            [...]

            designer.action(new AbstractTestAction() {
                @Override public void doExecute(TestContext testContext) {
                    designer.http()
                            .client(connectorClient)
                            .receive()
                            .response(HttpStatus.OK)
                            .messageType(MessageType.JSON)
                            .validate("$.token", "@assertThat(notNullValue())@")
                            .extractFromPayload("$.token","token"); (2)
                    System.out.println("Variable: "+testContext.getVariable("token"));
                    testContext.getGlobalVariables().put("token", "${token}");

3. Lastly, I am using a data provider for the tests executed in "VehicleTypeTestsIT". Versions used are Maven Failsafe 2.20, TestNG 6.11 and Citrus 2.7.5.
              

How can I use the overridden beforeSuite(); method in my test class and access the token I've extracted and saved in (2) in the header in (1)?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Override beforeSuite() to fetch a token and use the variable in a test class

⇜Krishnan Mahadevan⇝

I think you are better off posting this query in a Citrus related forum (for e.g., StackOverFlow : https://stackoverflow.com/questions/tagged/citrus-framework )

I am not seeing anything related to TestNG in your post.

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

From: <[hidden email]> on behalf of KiefersperreTurner <[hidden email]>
Reply-To: <[hidden email]>
Date: Wednesday, July 18, 2018 at 6:43 PM
To: testng-users <[hidden email]>
Subject: [testng-users] Override beforeSuite() to fetch a token and use the variable in a test class

 

Hello,

 

I've been having an issue with my Test for a while now. I'm a beginner in the field and I am not too familiar with TestNG that I am using with the Citrus framework.

 

1. I've a test class "VehicletypeTestsIT" extending TestNGCitrusTestDesigner, that tests a point of intersection by sending and validating JSON messages. In order to be able to get past the sending, I must login beforehand and send a token as part of the http header.

 

http()

                .client(connectorClient)

                .send()

                .post("address")

                .header("X-Authorization", "{$token}") (1)

                .messageType(MessageType.JSON)

                .contentType(ContentType.APPLICATION_JSON.getMimeType())

                .payload(String.valueOf(createVehicle));

 

2. I've a second class, that's a support class to "VehicletypeTestsIT" called "SetupAuthToken", which extends TestDesignerBeforeSuiteSupport. This class overrides the beforeSuite(); method, uses a valid login and validates an incoming JSON message and save the token received.

 

public HttpClient connectorClient;

 

        public HttpClient getConnectorClient() {

            [...]

        }

 

        @Override

        public void beforeSuite(TestDesigner designer) {

            designer.echo("Setting up authentication token");

 

            connectorClient = getConnectorClient();

 

            requestERPLogin ERPLogin = 

 

            [...]

 

            designer.action(new AbstractTestAction() {

                @Override public void doExecute(TestContext testContext) {

                    designer.http()

                            .client(connectorClient)

                            .receive()

                            .response(HttpStatus.OK)

                            .messageType(MessageType.JSON)

                            .validate("$.token", "@assertThat(notNullValue())@")

                            .extractFromPayload("$.token","token"); (2)

                    System.out.println("Variable: "+testContext.getVariable("token"));

                    testContext.getGlobalVariables().put("token", "${token}");

 

3. Lastly, I am using a data provider for the tests executed in "VehicleTypeTestsIT". Versions used are Maven Failsafe 2.20, TestNG 6.11 and Citrus 2.7.5.

              

 

How can I use the overridden beforeSuite(); method in my test class and access the token I've extracted and saved in (2) in the header in (1)?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.