160. Parameterization with testng configuration file






testng.xml configuration file can be used to parameterize the test data in the selenium automated tests.

Follow the below steps for understanding the Parameterization with testng configuration file:

1. Create a new Class file 'ChapterFourTests' under the package67 of our previous project as shown below:



2. Lets write the tests on 'Chapter4' page of  'http://book.theautomatedtester.co.uk/' web application as shown below:





3. The Java Class file 'ChapterFourTests.java' will look as shown in the below screenshot after writing the Selenium RC automation code for the test explained in the above step.





4. Open the testng.xml configuration file using text editor as shown below:


5. Add the class tag of the newly created Class 'ChapterFourTests' under the test category 'TestTwo' as shown below and save:


6. Start the Selenium Standalone Server
7. Run the testng.xml configuration file using the TestNG as shown below:



8. Ensure that all the tests got passed as shown below:



Now lets parameterize the test data 50 in the above passed test using the testng.xml configuration file by following the below steps:

1. Open http://testng.org/doc/documentation-main.html#parameters and copy the line containing the <parameter> tag as shown below:



2. Open the testng.xml file using Text Editor and paste the above copied line as shown below:



3. Now lets change the name attribute value to our desired name as shown below (In this example, I am changing it to  MONEY):



4. Now change the value attribute value to 50 as shown below and save:




So in the steps 4 and 5, we have created a parameter variable MONEY and assigned a value 50 to it. We can use this Parameter variable MONEY in our selenium automation methods to represent the value 50.

5. Open the Class file 'ChapterFourTests' and identify the value 50 in the code as shown below:



6. Replaced the identified value 50 with the Parameter Variable MONEY as shown below:




Observe that in the above screenshot, we got error messages with MONEY Parameter variable. In order to resolve these errors we've follow the below steps.

7. Open http://testng.org/doc/documentation-main.html#parameters and copy the line containing the @Parameters TestNG annotation as shown below:


8. Open the Class file 'ChapterFourTests' and paste the copied line below the @Test TestNG annotation as shown below:



So we've added the @Parameters TestNG annotation to our automation code.

9. Now replace the copied Parameter Variables with our Parameter Variable MONEY as shown below:



Observe that the error message are still displayed. Follow the below steps to resolve this error messages.

10. Declare the Parameter variable as String type in the parenthesis of the method as shown below and save:



Observe that the error messages got resolved as they are not displayed in the above screenshot.

This is how we parameterize the test data in Selenium Automation Code. Now lets see, whether our test runs successfully after Parameterization of test data 50 in this example by following the below steps.

11. Start the Selenium  Standalone Server
12. Run the testng.xml configuration file using  TestNG as shown below:



13. Observe that all the tests including the test containing the parameterized variable got passed as shown below:




In the similar manner, lets parameterize few other things in our selenium RC test automation code by following the below steps:

1. Open the Class file 'ChapterFourTests' and Create parameter variables for the following highlighted things on the screenshot as shown below:



2. Open the testng.xml configuration file using the Text Editor and create parameter tags for all the above high lighted things as shown below and save:



3. Now open 'ChapterOneTests' Java class file and update the respective method with the above created parameters as shown below and save:





4. In the similar manner, open the 'ChapterTwoTests' Java Class file and update the respective method with the parameters as shown below and save:





5. In the similar manner, open the 'ChapterThreeTests' Java Class file and update the respective method with the parameters as shown below and save:





6. In the similar manner, open the 'ChapterFourTests' Java Class file and update the respective method with the parameters as shown below and save:





7. Start the Selenium  Standalone Server
8. Run the testng.xml configuration file using  TestNG as shown below:




9. Observe that all the tests are passed as shown below:



This is how we have to implement Parameterization with testng configuration file.

The Parameterization using testng.xml configuration file will be useful only when you have less number of parameters. How to deal with a large number of parameters will be explained in the upcoming posts.

Download this Project:


Click here to download this project and import into Eclipse IDE  on your machine.



Please comment below to feedback or ask questions.

Create Properties file  will be explained in the next post.






No comments:

Post a Comment

Followers

Labels