Soft Asserts in TestNG
Niraj Kumar
Selenium | Java | Python | Cucumber | Appium | TestNG | Pytest | Docker | Grid | SQL | Katalon | Maven | Gradle | JBehave | Junit
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
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
Use case for Soft Assert
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 -