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.

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

I have a test script which is to be run for "n" number of data, I'm Doing that using @DataProvider, Now my problem is when i use "Assert" to fail the test case when test fails for 1 particular data, then execution of whole Test Script stops. I want it to keep executing even when it fails for 1 data. How can i do it ? can anybody help? thanks !!

Use a soft assert instead of assert. It will just throw an error but not stop the test.

use verify instead of assert

u had better to use Assert.Assertall();method..this method for soft even u r test fail then execution will not stop

Hi, i am showing one years experience in selenium webdriver. so which framework we have to show?

POM - Page Object Model

Hello,

I'm using assert.assertEqual() method for drop down clicking and verifying option, so everytime i need to wait for some time to load drop down value, is there global level or local setting for time lapse

Thanks

@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

Add new comment

Image CAPTCHA