Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Advanced TestNG Tutorials 33: Integration and Usage of BeanShell In TestNG XML

Posted on 02/19/2025 By admin

Hello Folks,

We have learnt how can we customise testng xml to run test methods belonging to multiple groups.

Let’s consider a new scenario:

Suppose There are three methods M1 ( belongs to group WIndows), M2(belongs to group Windows and Linux) and M3(belongs to group Windows ,Linux and Mac). You want to run methods which belong to both “WIndows” and “Linux” together i.e. M2.

You can not do such setting in testng xml using include or exclude tags. Here you need “AND” & “OR” logics. To achieve this we need “BeanShell” scripting.

Introduction of BeanShell:

As per doc:

BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell executes standard Java statements and expressions but also extends Java into the scripting domain with common scripting language conventions and syntax. BeanShell is a natural scripting language for Java.

It comes builtin with TestNG jar.

I will not explain about beanShell much here. But you can always refer its doc. I will explain how you can achieve solution of above scenario.

Since beanshell comes inbuilt with TestNG jar, so you don’t require it to add. You need to include beanshell scripts with tag as we do normally to embed javascript code with html.

Let’s create a testng class first as below:

Now you are required to run test method which belong to “Window” and “Linux” together i.e method “MethodsBelongToG1G2”. If you use traditional include and exclude tag, you can not achieve this. You need to achieve “AND” condition with group names. To achieve this you need to write beanshell script.

Update testng xml as below:

I added comments for your understanding. RUn it as testng suite and you will see only desired method is run.

Output:

Even you can write java codes like print statements and it will work perfectly fine.

Hope you learnt something new.

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

Uncategorized

Post navigation

Previous Post: api testing tutorials
Next Post: Front End Automation Strategies 1 – Don’t Write Complex XPath/CSS – Ask For Static Locators Instead

Related Posts

Jquerty1 – Make Selenium Easy Uncategorized
Postman Tutorial Part 22 – Postman Console – Debugging And Logging Uncategorized
Why Do We Require Browser Driver Executable File To Launch A Browser In Selenium WebDriver? Uncategorized
image – Make Selenium Easy Uncategorized
Data Structure For Testers Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark