Assertions in TestNG

What is Assertion????
Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception. There is a difference between SoftAssert and Hard Assert.

Let us focus more on using Assertions (Hard assert). Also check SoftAssert example in TestNG for more details on soft asserts.

Here we are verifying if the page title is equal to 'Google' or not. If the page title is not matching with the text / title that we provided, it will fail the test case.

To explain you what is assertion, lets us look into below code sample

@Test
	public void testCaseVerifyHomePage() {
		driver= new FirefoxDriver();
		driver.navigate().to("http://google.com");
		Assert.assertEquals("Google", driver.getTitle());
	}

What am i doing in the above test case is, first i am creating a Firefox driver, next navigating to the Google page And then the third line here is called Assertion.

It can also be written as below Assert.assertEquals("Goooogle", driver.getTitle(), "Title not matching");

TestNG supports assertion of a test using the Assert class and Assertion plays an important role when testing an application.

We will change the page title in the below code and see the result after executing it.

@Test
	public void testCaseVerifyHomePage() {
		driver= new FirefoxDriver();
		driver.navigate().to("http://google.com");
		Assert.assertEquals("Gooooogle", driver.getTitle());
	}

The above code will throw you an Assertion error as below: java.lang.AssertionError: expected [Google] but found [Gooooogle]

Like wise there are many Assertions provided by the TestNG. The below are the few which are used commonly.

assertEqual(String actual,String expected) :- It takes two string arguments and checks whether both are equal, if not it will fail the test.

assertEqual(String actual,String expected, String message) :- It takes three string arguments and checks whether both are equal, if not it will fail the test and throws the message which we provide.

assertEquals(boolean actual,boolean expected) :- It takes two Boolean arguments and checks whether both are equal, if not it will fail the test.

assertEquals(java.util.Collection actual, java.util.Collection expected, java.lang.String message) :- Takes two collection objects and verifies both collections contain the same elements and with the same order. if not it will fail the test with the given message.

Assert.assertTrue(condition) :- It takes one boolean arguments and checks that a condition is true, If it isn't, an AssertionError is thrown.

Assert.assertTrue(condition, message) :- It takes one boolean argument and String message. It Asserts that a condition is true. If it isn't, an AssertionError, with the given message, is thrown.

Assert.assertFalse(condition) :- It takes one boolean arguments and checks that a condition is false, If it isn't, an AssertionError is thrown.

Assert.assertFalse(condition, message) :- It takes one boolean argument and String message. It Asserts that a condition is false. If it isn't, an AssertionError, with the given message, is thrown.

The below is the sample code to use assertions :

	@Test
	public void testCaseVerifyHomePage() {
		driver= new FirefoxDriver();
		driver.navigate().to("http://google.com");
		Assert.assertEquals("Gooogle", driver.getTitle(), "Strings are not matching");
		//Write a code to login and write a method called isUserLoggedInSuccessfully and isUserLoggedOut which returns boolean.
		Assert.assertTrue(isUserLoggedInSuccessfully(), "User failed to login");
		Assert.assertFalse(isUserLoggedOut())
		}
}
Test Frameworks: 

Comments

@sant

you can use driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS).
Here driver is an instance of WebDriver.

by implicit wait, the program will wait for maximum of 20 seconds until it finds the element.

Do we need a jar file to use assert?

Yes, you will need 'Testng.jar' for it

How to use SoftAssert instead of Assert?

Hi How to use assertEqual method for numeric values. its throwing error "The method assertEquals(object, object) is ambiguous for the type Assert. Please help

Is there any difference between AsserEquals and AssertSame?

AssertSame is used to verify if the two objects refer to the same object where are AsserEquals is used to verify if two value of same data type are equal or not.

How can i use action class in testNG

Hi can anyone explain.. Assert is methodoveriding.. How?

The asserts package is no longer available in testNG. I am using version 6.9.10. As a result I am unable to use Soft Assert. Any workarounds for this.?

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.