Mastering Assertions in Automation Testing, Importance and Best Practices
Introduction:
Welcome aboard to our exploration of automated testing! Here, we’ll talk about assertions, the trusty guards of reliability and functionality in the testing world. This article digs into why assertions are so important in automated testing and share some top tips for using them effectively
We’ll keep things simple as we journey through the world of test automation, showing how assertions help us check if things are working as they should. Plus, we’ll demystify two popular frameworks, TestNG and JUnit, which are like our trusty sidekicks in writing solid automation scripts. So, get ready for a ride where assertions lead us to app quality and assurance!
What is Assertion in Automated Tests:
Assertions are the unsung heroes of automated testing, ensuring the reliability and functionality of our digital storefronts much like diligent shopkeepers in an e-commerce website. They act as validation mechanisms, constantly verifying whether expected conditions align with reality during test execution. Imagine you’re running an online store and updating product prices. Assertions are like double-checking that the prices displayed on the website match the updated values in the database, confirming that customers are charged correctly for their purchases.
Now, let’s explore the versatility of assertions across different types of automated tests. In unit tests, assertions validate individual pieces of code, ensuring they perform as expected within the larger system. During integration tests, assertions verify the smooth communication between various components of the website, such as the cart, payment gateway, and inventory management system. In functional automation, assertions validate the proper functioning of features like search, checkout, and order tracking, ensuring a seamless user experience. Lastly, in performance testing, assertions monitor website response times and server resource usage, alerting us to any performance bottlenecks that may impact user satisfaction.
In summary, assertions are indispensable tools across the diverse landscape of automated testing, providing reassurance that our e-commerce websites function flawlessly and deliver a smooth shopping experience to customers
Top 15 Assertions in TestNG with Scenarios, Code Examples, and Usage in Automation
1. assertEquals
This assertion verifies if two values are equal.
Usage with Automation: Asserts the title of the web page after successful login
Code Example:
assertEquals(actualTitle, expectedTitle);
2. assertNotEquals
This assertion validates that two values are not equal.
Usage with Automation: Asserts that the registration fails for an existing username.
Soft Assert allows the execution of test methods to continue even after an assertion failure, reporting all failures at the end of the test. On the other hand, Hard Assert immediately stops test execution upon the first assertion failure. Soft Assert comes in handy when you need to carry out multiple validations within a single test method without halting the entire test prematurely.
Soft Assert Example:
public class SOftAssertExample {
public static void main(String[] args) { // Set Chrome driver path System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
// Initialize ChromeDriver WebDriver driver = new ChromeDriver();
// Launch the website driver.get(“https://www.google.com”);
// Initialize SoftAssert SoftAssert softAssert = new SoftAssert();
// Assert all soft assertions softAssert.assertAll();
// Close the browser driver.quit(); } }
Hard Assert Example:
public class HardAssertExample { public static void main(String[] args) { // Set Chrome driver path System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
// Initialize ChromeDriver WebDriver driver = new ChromeDriver();
// Launch the website driver.get(“https://www.example.com”);
// Perform hard assertions Assert.assertEquals(driver.getTitle(), “Example Domain”); Assert.assertTrue(driver.getCurrentUrl().contains(“example.com”)); Assert.assertTrue(driver.getPageSource().contains(“Example”));
// Perform additional actions
// Close the browser driver.quit(); }
}
In the Soft Assert example, all assertions are executed, and any failures are recorded but do not stop the execution of the test. The assertAll() method is called at the end to mark all soft assertions and report any failures.
In the Hard Assert example, assertions are executed one by one, and if any assertion fails, it immediately stops the test execution.
Best Practices for Assertions in Test Automation:
Use Descriptive Messages: Provide clear and meaningful messages for each assertion to make debugging easier.
Example:
@Test public void verifyLoginErrorMessage() { LoginPage loginPage = new LoginPage(driver); loginPage.login(“invalidUsername”, “invalidPassword”); String actualErrorMessage = loginPage.getErrorMessage(); String expectedErrorMessage = “Invalid username or password”; Assert.assertEquals(actualErrorMessage, expectedErrorMessage, “Incorrect error message displayed after login failure.”); }
Keep Assertions Atomic:
Avoid adding multiple verifications within a single assertion. Each assertion should verify one specific condition.
Example:
@Test
public void verifyUserDetails() { UserPage userPage = new UserPage(driver); User userDetails = userPage.getUserDetails(); Assert.assertNotNull(userDetails, “User details not found.”); Assert.assertNotNull(userDetails.getUsername(), “Username not found.”); Assert.assertNotNull(userDetails.getEmail(), “Email not found.”); }
Use Explicit Wait:
Ensure that elements are present and ready for assertion using explicit wait conditions before performing assertions on them.
Example:
@Test public void verifyElementVisibility() { WebDriverWait wait = new WebDriverWait(driver, 10); WebElement submitButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“submit”))); Assert.assertTrue(submitButton.isDisplayed(), “Submit button not visible.”); }
Also Avoid Thread.sleep() to wait for elements before assertions. Instead, use explicit waits to wait for specific conditions.
Verify Expected and Actual Values: Always verify both expected and actual values in assertions to ensure accuracy.
Use Appropriate Assertion Methods: Choose the appropriate assertion methods based on the type of verification required (e.g., assertEquals(), assertTrue(), assertNotNull()).
Example:
@Test public void verifyElementVisibility() { WebElement submitButton = driver.findElement(By.id(“submit”)); Assert.assertTrue(submitButton.isDisplayed(), “Submit button not visible.”); }
Efficient Exception Handling: Use try-catch blocks to handle exceptions and provide meaningful error messages in case of assertion failures.
Example:
public void verifyElementPresence() { try { WebElement logo = driver.findElement(By.id(“logo”)); Assert.assertNotNull(logo, “Logo element is NOT found”); } catch (NoSuchElementException e) { Assert.fail(“Logo element is NOT found.”); } }
Conclusion
In conclusion, assertions are super important in test automation using tools like Selenium and Rest Assured. They act as the quality gates of our test scripts, making sure that the app behaves as it should and all the expected values are validated with actual results. Becoming proficient in assertions within automated testing is vital for guaranteeing the precision and dependability of test outcomes. By grasping the significance of assertions and adhering to recommended practices, testers can develop resilient and efficient automated test suites, ultimately resulting in heightened app quality and broader test coverage.
Running tests without assertions is like walking blindfolded—you’re unsure if you’re heading in the right direction. But with assertions, you gain clarity. You can confidently affirm, “Yes, the app is functioning as intended.” Therefore, in the realm of test automation, assertions are indispensable companions. They give meaning to our tests, ensuring their effectiveness. So, if you’re venturing into test automation, embrace assertions—they’re the true MVPs of testing.
Comprehensive Test Coverage
Test your apps on 5000+ real device and browser combinations and ensure a complete test coverage.
Currently working as a SDET. He is an Automation enabler who provides solutions that mitigates quality risk. Passionate about technical writing and contribution towards QA community.