9r. WebDriver – Assert and Verify

Hiya superstars! We have been locating elements for quite some days now. Let us switch gears and talk about “Assert and Verify” today.

To remember,

The major difference between the two when the assert or verify condition/check fails is,

  • Assert will fail the test and abort the execution of the current test case. All other test steps after that particular line of code are skipped
  • Verify will log the failure but continue to execute the test case.

When to use assert and verify?

The simplest answer is – it is up to you, in other words, your wish! You can use either assert or verify based on the situation, i.e. whether you want the test to abort or continue after the check fails.

The advantage of using an assert

Most times we want the test execution to stop when a check fails and that’s what we get with an assert. The test case fails and it is clearly highlighted as ‘failed’. This will immediately show us which test cases did not pass the checks in the complete test suite in just one glance. We can then directly go to those failed cases and inspect why the check/condition did not pass. Isn’t that pretty handy? Due to this immediate feedback availability, assert is more commonly used.

The disadvantage of an assert

When the first assert condition fails, the following lines of code are never executed. There might be other checks to be performed and we will never know their result.

The advantage of using verify

This is mostly used when we want our test execution to continue even if one of the conditions fail. The failure will be logged or printed to console. Thus, we get the result of all the checks in a test case irrespective of whether they pass or fail.

The disadvantage of using verify

Immediate feedback is not available with verify as the test case execution is not aborted upon the failure of a condition. So, every time tests are executed, we have to spend quality time looking through the logs or the printed statements in the console to determine which checks failed. This might not prove feasible if hundreds of test cases are to be executed numerous times for different datasets, for example.

Sample scenarios

Let us get the title of the sample webpage we have created for this tutorial series. This will be the actual title that we are obtaining using WebDriver’s getTitle() method. The expected title is, “WebDriver Demo Website”.

Case 1: Pass the test case with assertEquals

The actual and expected titles are equal and hence the output for the condition,  Assert.assertEquals("WebDriver Demo Website", pageTitle); will be a success. The code following this line will be executed and the test case will be passed.

Case 2: Fail the test case with assertNotEquals

The actual and expected titles are equal and hence the output for the condition,  Assert.assertNotEquals("WebDriver Demo Website", pageTitle);  will be a failure. The code following this line will not be executed. The test execution is aborted and the test case will be failed.

Code snippet

// Making the test fail
Assert.assertNotEquals("WebDriver Demo Website", pageTitle);

// Following lines will not be executed as above assert condition fails
System.out.println("Assert not equals failed");

Assert condition failed

The console in the above image shows that the assertEquals condition is a success and hence the statement following that check will be printed, “Assert equals passed.” Whereas the assertNotEquals condition fails and hence the lines following this check will not be executed. The print statement, “Assert not equals failed” will not be printed to console.

Case 3: Pass the test case though assertNotEquals condition fails

To just verify if actual and expected values are not equal, use a try-catch block.

Code snippet

//Verify title not equal using try-catch block
try {
// Making the test fail
	Assert.assertNotEquals("WebDriver Demo Website", pageTitle);	
} catch(Error e){
	// Following lines will be printed when the assert condition fails
	System.out.println("Assert not equals failed. But test execution is not aborted.");
	System.out.println("Error message: " + e.toString());
}

Even though the assertNotEquals condition fails, the statements in the catch block will be executed and the error message will be printed to the console.

Verify condition fails

As shown in the image, test case execution is a success and the error is printed to the console.

Complete code

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AssertAndVerify {
	// Declaring variables
	private WebDriver driver;
	private String baseUrl;

	@Before
	public void setUp() throws Exception {
		// Selenium version 3 beta releases require system property set up
		System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\"
				+ "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
		// Create a new instance for the class FirefoxDriver
		// that implements WebDriver interface
		driver = new FirefoxDriver();
		// Implicit wait for 5 seconds
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		// Assign the URL to be invoked to a String variable
		baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/";
	}
	
	@Test
	public void testPageTitle() throws Exception {
		// Open baseUrl in Firefox browser window
		driver.get(baseUrl);
		
		// Get the page title
		String pageTitle = driver.getTitle();
		// Print the title to console
		System.out.println("The actual title is: " + pageTitle);
		// Check if actual and expected values are equal
		Assert.assertEquals("WebDriver Demo Website", pageTitle);
		// Printing success message
		System.out.println("Assert equals passed.");
		// Making the test fail
		//Assert.assertNotEquals("WebDriver Demo Website", pageTitle);
		// Following lines will not be executed as above assert condition fails
		//System.out.println("Assert not equals failed");
		
		//Verify title not equal using try-catch block
		try {
			// Making the test fail
			Assert.assertNotEquals("WebDriver Demo Website", pageTitle);	
		} catch(Error e){
			// Following lines will be printed when the assert condition fails
			System.out.println("Assert not equals failed. But test execution is not aborted.");
			System.out.println("Error message: " + e.toString());
		}

	} // End of @Test

	@After
	public void tearDown() throws Exception {
		// Close the Firefox browser
		driver.close();
	}
}

All the code files are placed in the GitHub repo for easy access. You can star and fork the repository for convenience. Please go through the ‘README.md’ file for clear instructions.

That wraps up this section of assert and verify. Have a great day!

<< 9q. WebDriver – Handling a CAPTCHA 9s. WebDriver – Handling text boxes and images >>

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.