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

I have testing on amazon website for practice but some element could not be locate because span class, it cannot be take xpath & any class name . I want to check select functionality by shop by category but I could not be locate element for selection.

ur using testng frame work right.for every test case write in different methods use groups concept.and run the script .then if assertion is assertion is fail ,related test case only fail .reaming will work fine.if any mistake please feedback me .

I am novice to this selenium, having a very basic doubt i.e i would like write a logic that should check whether page is loaded or not and if it is true then only next portion of the code should execute, Can some one tell me how to achieve this?

Add new comment