top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-
Writer's picturejayalakshmi veerappan

TestNG Framework and BDD Framework

Selenium users have started using the TestNG framework widely because of its advantages over JUnit. Many shortcomings of JUnit are overcome in TestNG, designed to make end-to-end testing easy. 'NG' in TestNG stands for "Next Generation." It uses the annotations inspired by JUnit and provides the best solutions like sequencing, grouping, and parametrizing tests. You quickly learn about the test results - in short, which test case got passed, failed, or skipped and how much time it took to execute. It even generates a comprehensive report for the same in HTML view. In short - Using TestNG in Selenium gives you the ability to generate test results in a proper and easy-to-understand format. Critical features of Selenium TestNG are:

  • Generate the report properly, including the number of test case runs and the number of test cases passed, failed, and skipped.

  • You can add multiple test cases, map them in the testng.xml file, and use the group-by feature. You can make priorities on which test case to execute first using its annotations.

  • By using a keyword called 'invocation count,' you can execute the same test case multiple times without loops.

  • Using TestNG, you can execute multiple test cases on multiple browsers, i.e., cross-browser testing.

  • The TestNG framework can be easily integrated with tools like TestNG Maven, Jenkins, etc.

  • TestNG provides many easy annotations, e.g., @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest, etc.

  • For generating reports, WebDriver has no native mechanism. You can find such reports in TestNG. These reports can be sent to others over email and are easily readable.

TestNG Annotations & its Implementations:


Output: Before Suite Before Test Before Class Before Method Test Case 1 After Method Before Method Test Case 2 After Method After Class After Test PASSED: test1 PASSED: test2


How to implement TestNG in your project:

  1. Go to the eclipse marketplace.

  2. Search for TestNG > Install > Restart IDE.

  3. Create a New Java Project > Go to Build path > Add library > Select TestNG.

  4. Go to the src file of your project > New > TestNG Class.

  5. Write your code.

  6. Run as TestNG.

  7. Now right-click on the Project > hover over TestNG > Convert to TestNG.

  8. Now refresh your project view > Check the testng.xml file created.

You can add as many test cases as you want to add - all will be added in your testng.xml. Furthermore, you can add annotations to it as per your requirements. After running, it successfully, you will be able to generate a report that can be emailed.


Behavior Driven Development (BDD) Framework


Behavior Driven Development (BDD) Framework enables software testers to complete test scripting in plain English. BDD mainly focuses on the behavior of the product and user acceptance criteria. Cucumber is one of the best tools used to develop in the BDD Framework. The Cucumber tool is used for development in various languages like Ruby, Python, and JavaScript. It uses Gherkin, an ordinary language parser, that permits writing scripts in English that can be easily understood. It is used to run the acceptance tests written in BDD. Here we will walk you through what BDD, and why and how you should be using Cucumber in BDD.

Advantages of the BDD Framework

  • BDD scenarios are written in plain English, which is easy to understand by everyone involved in the project, from stakeholders to project team members.

  • Scenarios are written specifically to each functionality which gives clarity for product development and testing.

  • Be it manual or automated, testing becomes very easy as the test cases or the test scripts can be easily identified from the feature files.

  • The maintenance of the framework is simple.

  • BDD also acts as project documentation for all modules and functionalities.

Why You Should Choose Cucumber for BDD Framework

  • It serves as the automated test for test scripts and documentation for the project itself.

  • Test scenarios are clearly organized in the feature files.

  • Tests are written based on user stories and system behavior corresponding to the user story in English using annotations like Given, When, Then.

  • Code can be written in any language like Java, Python, Perl, etc.

Features of Cucumber

Gherkin: In BDD, the scenarios are written in a language named Gherkin. Gherkin uses a simple syntax and keywords are easily understood by anyone like business users and stakeholders. The feature files which store the scenarios under test are written in Gherkin. Gherkin files should have the .feature file extension. Every feature file contains an individual scenario under test.

Features: Feature is a use case that describes the module under test. The feature is composed of the following details.

  • The keyword “Feature:” is appended before the name of the feature.

  • The feature name is written beside the feature keyword.

  • An optional description is also written below the feature to describe the crux of the feature.

Scenario: A scenario is a series of steps involved in the test execution.

Scenario Outline: Used where test data is replaced with multiple sets of data for each run of a test script.

Implementation of BDD Framework in Selenium Webdriver

As an example of an implementation of the test script using Cucumber, we are going to use a maven project in Selenium Webdriver. Before we start with the implementation, we need to add the dependency for Cucumber in pom.xml as we are using a maven project in this scenario. Here we are covering two scenarios.

Scenario 1

In this scenario we check the login functionality with a single user:

  1. Launch the Swag labs page

  2. Verify the Swag labs login page

  3. Enter the username

  4. Enter the Password

  5. Click on the Login button

  6. Verify the Sauce demo home page is displayed

  7. Logout of the application

Below is the Loginmethods.java class for multiple sets of data to check the login functionality for a single user.

1. Loginmethods.java

Functionality Methods are defined in this class:


2. LoginPage.feature.java

Feature file: Steps for the scenario is written in this class:

3. runTest.java

Runner class: Class to run the feature file:

4. Results in Junit

Below are the Feature file run results in the Junit:

5. Test results in Console Window

Below are the test results in the Console window:

Let us see the same implementation using multiple sets of data in the below scenario:

Scenario 2

In this scenario, we check the login functionality with multiple users:

  1. Launch the Swag labs page

  2. Verify the Swag labs login page

  3. Enter the username

  4. Enter the Password

  5. Click on the Login button

  6. Verify the Sauce demo home page is displayed

  7. Logout of the application

Below is the Loginmethods.java class for multiple data sets to check the login functionality for multiple users.

1. Loginmethods.java

Methods for the functionality are defined in this class:


Here we define the step as:

@When ("^User enters the username as \"([^\”] *)\"$")

public void enter_the_username(String ar1) {

driver.findElement(By.id("user-name")).isDisplayed();

driver.findElement(By.id("user-name")).sendKeys(ar1); }

Here \"([^\"]*)\"$" denotes the user expression for multiple sets of data

The method “enter_the_username” takes String \"([^\"]*)\"$" as an argument, which is passed from the examples during runtime.

2. LoginPage.feature.java

Feature file: Steps for the scenario is written in this class:

Scenario Outline is the tag name used for testing multiple sets of data.

Examples section: To define the multiple data sets.

Here the “<username>” and <”password”> are replaced with the username and password combinations mentioned in the Examples section

Examples:

| username | password |

| standard_user | secret_sauce |

| performance_glitch_user | secret_sauce|

3. runTest.java

Runner class: Class to run the Feature file.


4. Results in Junit

5.Test Results in the Console window



Conclusion


Based on your project requirement, you may prefer to use some framework features over others. Here in TestNG, we get many popular and easy-to-use features, some of which have been inherited from JUnit. Moreover, since it's open source, many developers find it easy, accessible, and more usable.

Implementing BDD Framework Using Cucumber is an effective solution as it enables testers to perform and write automated test scripts in natural language, which makes it easier for stakeholders and developers to have a better understanding of test cases. Moreover, it also offers greater flexibility to code in any language like Java, Python, Perl, etc.

Note : I had reference from Vsoftcunsulting.com and Jadeglobal.com




523 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page