Soft Asserts in TestNG

Soft Asserts in TestNG

In TestNG, SoftAssert is a mechanism that allows test execution to continue even if an assertion fails, unlike Hard Assert (Assert), which stops the test immediately upon failure.

Key Features of Soft Asserts

  • Does not halt execution when an assertion fails.
  • Collects all assertion failures and reports them at the end.
  • Useful in scenarios where multiple assertions need to be checked before failing a test.

Example Usage of SoftAsserts

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class SoftAssertTrueExample {
    
    @Test
    public void testProfileValidation() {
        SoftAssert softAssert = new SoftAssert();

        String username = "JohnDoe";
        String email = "[email protected]";
        String profileStatus = "Active";

        System.out.println("Step 1: Validating Username");
        softAssert.assertTrue(!username.isEmpty(), "Username is missing!");

        System.out.println("Step 2: Validating Email Format");
        softAssert.assertTrue(email.contains("@"), "Email format is incorrect!");

        System.out.println("Step 3: Validating Profile Status");
        softAssert.assertTrue(profileStatus.equals("Active"), "Profile status is not Active!");

        // Collect all assertion failures
        softAssert.assertAll();
    }
}        

Important Points

  • assertAll() is mandatoryIf assertAll() is not called, TestNG will not report assertion failures.
  • It collects all failures and reports them at the end.

Use case for Soft Assert

  • When validating multiple fields on a page (e.g., form validation).
  • When you want to log all assertion failures instead of stopping at the first failure.


Automating a Login Page Validation Using Cucumber, TestNG, and SoftAssert

Updated Project Structure

BDD-SoftAssert-Project
│── src/test/java
│   ├── features
│   │   ├── login.feature
│   ├── stepdefinitions
│   │   ├── LoginSteps.java
│   ├── runners
│   │   ├── TestRunner.java
│   ├── utils
│   │   ├── DriverFactory.java
│── pom.xml        
Gherkin  Feature File (login.feature)
Feature: Login Page Validation

  Scenario: Validate Login Page Elements
    Given I launch the login page
    Then I verify the presence of all login elements
    And I close the browser        
WebDriver Manager (DriverFactory.java)

package utils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DriverFactory {

    private WebDriver driver;

    // Constructor to initialize WebDriver instance
    public DriverFactory() {
      
        this.driver = new ChromeDriver();
    }

    public WebDriver getDriver() {
        return driver;
    }

    public void closeDriver() {
        if (driver != null) {
            driver.quit();
        }
    }
}

        
Step Definitions (LoginSteps.java)
package stepdefinitions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.asserts.SoftAssert;
import io.cucumber.java.en.*;
import utils.DriverFactory;

public class LoginSteps {

    private DriverFactory driverFactory;
    private WebDriver driver;
    private SoftAssert softAssert = new SoftAssert(); // SoftAssert instance

    public LoginSteps() {
        driverFactory = new DriverFactory();
        this.driver = driverFactory.getDriver();
    }

    @Given("I launch the login page")
    public void i_launch_the_login_page() {
        driver.get("https://example.com/login");  // Replace with actual URL
    }

    @Then("I verify the presence of all login elements")
    public void i_verify_the_presence_of_all_login_elements() {
        WebElement username = driver.findElement(By.id("username"));
        WebElement password = driver.findElement(By.id("password"));
        WebElement loginButton = driver.findElement(By.id("login"));
        
        softAssert.assertTrue(username.isDisplayed(), "Username field is missing!");
        softAssert.assertTrue(password.isDisplayed(), "Password field is missing!");
        softAssert.assertTrue(loginButton.isDisplayed(), "Login button is missing!");
        
        softAssert.assertAll(); // Collects all failures
    }

    @And("I close the browser")
    public void i_close_the_browser() {
        driverFactory.closeDriver();
    }
}
        
Test Runner (TestRunner.java)
package runners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    features = "src/test/java/features",
    glue = "stepdefinitions",
    plugin = {"pretty", "html:target/cucumber-reports.html"},
    monochrome = true
)
public class TestRunner extends AbstractTestNGCucumberTests {
}
        
<dependencies>
    <!-- Selenium -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.10.0</version>
    </dependency>

    <!-- Cucumber -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.11.0</version>
    </dependency>
    
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>7.11.0</version>
    </dependency>

    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.8.0</version>
    </dependency>
</dependencies>
        

Refrences -

geeksforgeeks

LambdaTest

BrowserStack







要查看或添加评论,请登录

Niraj Kumar的更多文章

  • Test Automation Solution

    Test Automation Solution

    Test Automation Solution (TAS) Architecture The Test Automation Solution (TAS) requires a comprehensive understanding…

  • What is Test Generation Layer

    What is Test Generation Layer

    In the context of Test Automation Architecture (TAA), the Test Generation Layer plays a crucial role in creating and…

社区洞察

其他会员也浏览了