Advanced TestNG Tutorials 34: How To Pass a Group Name to be Run at Runtime in TestNG XML Using Beanshell

Hello Folks,

In last post, we have seen how integration of beanshell and TestNg can help us to achieve “AND” and “OR” conditions with group names to be executed.

Problem statement:

You have created so many test methods with different groups. You do not need to run all group every time. You need to include or exclude groups as per requirements. You need to add include or exclude tags in testng xml which is not easier always. It will be even more difficult for a beginner or a business analyst or people with less technical knowledge to update testng xml to run.

Problem Solution:

WIll it not be good if you can pass group names at run time and no need to change anything in testng xml? Yes, its possible using beanshell.

TestNG class:

;

Create your testng.xml as below:

“groupToRun” is a VM arguments which we can pass through run configuration. Beanshell script will read it at run time and will execute group which we pass.

Passing VM arguments in Run configurations:

Navigate to Run -> Run Configurations:

Select testng xml for which arguments need to be passed:

 

Click or Arguments tab and type in VM arguments as -DgroupToRun=<Group Name>:

Please note arguments name should be same as testng xml i.e. groupToRun. Now click on apply and run

You can see method which belongs to group “Window” runs.

This way is easier than updating testng xml everytime. Hope you learnt new thing today.

In next post, we will see how can we pass multiple group names as VM argument and what needs to be updated in testng xml.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Leave a Reply

Your email address will not be published. Required fields are marked *