TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Selenium Assertions

Selenium Assertions with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

<< Back to SELENIUM

Assertions

Assertion determines the state of the application whether it is the same what we are expecting or not. If the assertion fails, then the test case is failed and stops the execution.

To use the Assertion in Web Driver, you need to download the Testng jar file and add it to the eclipse. Download the Testng jar file from the link given below:

https://mvnrepository.com/artifact/org.testng/testng/6.7

There are two types of Assertion:

  • Hard Assertion
  • Soft Assertion
Assertions

Hard Assertion

Hard Assertion is an Assertion that throws the AssertException when the test case is failed. In the case of Hard Assertion, you can handle the error by using a catch block like a java exception. Suppose we have two test cases in a suite. The first test case in a suite has an assertion that fails, and if we want to run the second case in a suit, then we need to handle the assertion error. A Hard Assertion contains the following methods:

  • AssertEquals
  • AssertNotEquals
  • AssertTrue
  • AssertFalse
  • AssertNull
  • AssertNotNull

AssertFalse()

Assertion verifies the boolean value returned by a condition. If the boolean value is false, then assertion passes the test case, and if the boolean value is true, then assertion aborts the test case by an exception. Syntax of AssertFalse() method is given below:

Assert.AssertFalse(condition);

Let's understand through an example:

  • When the condition is false
package mypack;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test {

	public static void main(String[] args) {
	// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.navigate().to("https://www.spicejet.com/");
		Assert.assertFalse(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
		System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
		
	}

}

In the above code, Assert.assertFalse() contains the condition which is returning false value. Therefore, it passes the test case.

Output on the console

Assertions
  • When the condition is true
package mypack;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test {

public static void main(String[] args) 
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.spicejet.com/");
Assert.assertFalse(true);
System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
		
}}

In the above code, Assert.assertFalse() method contains the true condition. Therefore, the assertion fails which means that the test case is also failed. Assertion failure will stop the execution by exception.

Output on the console

Assertions

AssertTrue()

Assertion verifies the boolean value returned by a condition. If the boolean value is true, then assertion passes the test case, and if the boolean value is false, then assertion aborts the test case by an exception. Syntax of AssertTrue() method is given below:

Assert.AssertTrue(condition);

Let's understand through an example.

package mypack;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test 
{

	public static void main(String[] args) 
           {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.navigate().to("https://www.spicejet.com/");
		driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click();
	      Assert.assertTrue(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
		
	}

}

In the above code, driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click(); This statement is used to select the 'Senior Citizen' box. In the next statement, we are applying assertion to check whether the test case fails or pass. The parameter inside the Assert.assertTrue() method is returning true value, therefore the test case pass.

Output

Assertions

Output on the Console

Assertions

AssertEquals()

AssertEquals() is a method used to compare the actual and expected results. If both the actual and expected results are same, then the assertion pass with no exception and the test case is marked as "passed". If both the actual and expected results are not the same, then the assertion fails with an exception and the test case is marked as "failed". Syntax of an AssertEquals() method is given below:

Assert.assertEquals(actual,expected);

Let's understand through an example.

  • When a number of adults is 5.
package mypack;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test {
public static void main(String[] args) 
{
  // TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.spicejet.com/"); Assert.assertEquals("5Adult",driver.findElement(By.id("divpaxinfo")).getText());
System.out.println(driver.findElement(By.id("divpaxinfo")).getText());
}}
Assertions
Assertions
  • When the number of adults is not equal to 5
Assertions

Output on the console

Assertions

AssertNotEquals()

It is opposite to the function of AssertNotEquals() method. AssertNotEquals() is a method used to compare the actual and expected results. If both the actual and expected results are not the same, then the assertion pass with no exception and the test case is marked as "passed". If both the actual and expected results are same, then the assertion fails with an exception and the test case is marked as "failed". Syntax of an AssertNotEquals() method is given below:

AssertNotEquals(actual,expected,message);

Let's understand through an example.

  • When actual string is not equal to the expected string.
package mypack;
import org.junit.Assert;
public class Checkbox_test {
public static void main(String[] args) {
		// TODO Auto-generated method stub
		Assert.assertNotEquals("Hello", "How are you");
		System.out.println("Hello...This is javaTpoint");
		
	}

}

In the above code, actual string, i.e., Hello is not equal to the expected string, i.e., How are you. Therefore, the assertion passes the test case. This will execute the next statement and the next statement is System.out.println("Hello...This is javaTpoint");.

Output

Assertions
  • When the actual string is equal to the expected string.
package mypack;
import org.junit.Assert;
public class Checkbox_test {
public static void main(String[] args) 
{
// TODO Auto-generated method stub
Assert.assertNotEquals("Hello", "Hello");
System.out.println("Hello...This is javaTpoint");
}}

Output

Assertions

AssertNull()

AssertNull() is a method that verifies whether the object is null or not. If an object is null, then assertion passes the test case, and the test case is marked as "passed", and if an object is not null, then assertion aborts the test case and the test case is marked as "failed". Syntax of AssertNull() method is given below:

Assert.assertNull(object);

Let's understand through an example.

  • When an object is null.
package mypack;
import org.junit.Assert;
public class Checkbox_test {

public static void main(String[] args) {
		
Assert.assertNull(null);
System.out.println("Hello...This is javaTpoint");
}}

Output

Assertions
  • When an object is not equal to null.
package mypack;
import org.junit.Assert;
public class Checkbox_test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
   Assert.assertNull(10);
	System.out.println("Hello World");
		
	}

}

Output

Assertions

AssertNotNull()

AssertNotNull() is a method that verifies whether the object is null or not. If an object is not null, then assertion passes the test case and test case is marked as "passed", and if an object is null, then assertion aborts the test case and test case is marked as "failed". Syntax of AssertNotNull() method is given below:

Assert.assertNotNull(object);

Let's understand through an example.

  • When an object is not null.
package mypack;
import org.junit.Assert;
public class Checkbox_test
{
  public static void main(String[] args) {
// TODO Auto-generated method stub
 Assert.assertNotNull(10);
System.out.println("C Language");
		
}}

Output

Assertions
  • When an object is null.
package mypack;
import org.junit.Assert;
public class Checkbox_test {
public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Assert.assertNotNull(null);
		System.out.println("C Language");
		
	}

}

Output

Assertions

SoftAssertion

Till now, we have learnt about the Hard Assertion in Web Driver using Testng framework. In hard assertion, if an assertion fails then it aborts the test case otherwise it continues the execution. Sometimes we want to execute the whole script even if the assertion fails. This is not possible in Hard Assertion. To overcome this problem, we need to use a soft assertion in testng.

Next TopicSelenium Grid




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf