Passing Data From the TestNG.xml file to Tests

 


As we all know, that we might not always require to run the Test Automation Suite only on one specific instance, we can say it as QA Environment. As and when required we might need to put up execution on Dev Staging, UAT, or even PROD environment as well. But, here the problem arises as you might need to check in the new set of credentials for the other environment into the Credential file of your framework. This is one of the many use cases for the topic of the Blog, which is Passing Data From the TestNG.xml file to Tests.

So here what we need to do. We can create multiple sets of XML files based on the credentials of the different environments and run the specified file as the request of execution easily.  

Here is the code snippet of one such example depicting how to place and organize your code.

1. Creating the XML file with the required set-1 of credentials.

            

        Here, define the parameters which are to be extended  for use from this XML file in the syntax of key-value pair:
        

        <parameter name="param_name" value="param_value" />
        
        We can define as many parameters as required but should limit to a few only as per best practices.  

2. Accessing the created parameters inside the test file.



        Now we can access the same parameter which is present in the executing file by using the @Parameters annotation and calling the param_name in form of an array element in the syntax given below.
        
        @Parameters({"param1", "param2", "param3", ... })

        In the given example I have used the same parameter values in the @BeforeClass Annotation.

        Now in the required method where we actually will use these credentials, we may pass the same name of the key as parameters to the method.

         If in case you wish to store the same variables globally for further use in other test steps, then you can simply pass the values to a Global Static variable as done in the snippet above.

        
That's it, we can now simply call the required parameter from a bunch of XML files created as per requirements in our TestSuite. Also for run configured runs through Jenkins, we can Parameterize the jobs using the XML files as a choice parameter and execute with ease on tips. For more info regarding the parameterization of jobs, do check out the link given.

Thank You !!!
         


MAIN CONTENT STARTS HERE

Comments

Popular posts from this blog

Demo Click Jack Maropost.Com