Using Assertions in Selenium WebDriver

Last updated on Nov 24 2021
Manikaran Reddy

Table of Contents

Using Assertions in Selenium Web Driver

Assertion determines the state of the appliance whether it’s an equivalent what we expect or not. If the assertion fails, then the test suit is failed and stops the execution.
To use the Assertion in Web Driver, you would like 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

Types of Assertions

There are two sorts of Assertion:
• Hard Assertion
• Soft Assertion

Selenium Web Driver
Selenium Web Driver

Hard Assertion

Hard Assertion is an Assertion that throws the AssertException when the test suit is failed. within the case of Hard Assertion, you’ll handle the error by employing a catch block sort of a java exception. Suppose we’ve two test cases during a suite. the primary test suit during a suite has an assertion that fails, and if we would like to run the second case during a suit, then we’d like to handle the assertion error. a tough Assertion contains the subsequent methods:
• AssertEquals
• AssertNotEquals
• AssertTrue
• AssertFalse
• AssertNull
• AssertNotNull
AssertFalse()
Assertion verifies the boolean value returned by a condition. If the boolean value is fake , then assertion passes the test suit , and if the boolean value is true, then assertion aborts the test suit by an exception. Syntax of AssertFalse() method is given below:
1. Assert.AssertFalse(condition);
Let’s understand through an example:
• When the condition is fake

1. package mypack;
2. import org.junit.Assert;
3. import org.openqa.selenium.By;
4. import org.openqa.selenium.WebDriver;
5. import org.openqa.selenium.chrome.ChromeDriver;
6. public class Checkbox_test {
7.
8. public static void main(String[] args) {
9. // TODO Auto-generated method stub
10. System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
11. WebDriver driver = new ChromeDriver();
12. driver.navigate().to("https://www.spicejet.com/");
13. Assert.assertFalse(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
14. System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
15.
16. }
17.
18. }

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

Selenium Web Driver
Selenium Web Driver

• When the condition is true

1. package mypack;
2. import org.junit.Assert;
3. import org.openqa.selenium.By;
4. import org.openqa.selenium.WebDriver;
5. import org.openqa.selenium.chrome.ChromeDriver;
6. public class Checkbox_test {
7.
8. public static void main(String[] args)
9. {
10. // TODO Auto-generated method stub
11. System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
12. WebDriver driver = new ChromeDriver();
13. driver.navigate().to("https://www.spicejet.com/");
14. Assert.assertFalse(true);
15. System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
16.
17. }}

In the above code, Assert.assertFalse() method contains truth condition. Therefore, the assertion fails which suggests that the test suit is additionally failed. Assertion failure will stop the execution by exception.
Output on the console

Selenium Web Driver
Selenium Web Driver

AssertTrue()
Assertion verifies the boolean value returned by a condition. If the boolean value is true, then assertion passes the test suit , and if the boolean value is fake , then assertion aborts the test suit by an exception. Syntax of AssertTrue() method is given below:
1. Assert.AssertTrue(condition);
Let’s understand through an example.

1. package mypack;
2. import org.junit.Assert;
3. import org.openqa.selenium.By;
4. import org.openqa.selenium.WebDriver;
5. import org.openqa.selenium.chrome.ChromeDriver;
6. public class Checkbox_test
7. {
8.
9. public static void main(String[] args)
10. {
11. // TODO Auto-generated method stub
12. System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
13. WebDriver driver = new ChromeDriver();
14. driver.navigate().to("https://www.spicejet.com/");
15. driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click();
16. Assert.assertTrue(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
17. System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
18.
19. }
20.
21. }

In the above code, driver.findElement(By.cssSelector(“input[id*=’SeniorCitizenDiscount’]”)).click(); This statement is employed to pick the ‘Senior Citizen’ box. within the next statement, we are applying assertion to see whether the test suit fails or pass. The parameter inside the Assert.assertTrue() method is returning true value, therefore the test suit pass.
Output

Selenium Web Driver
Selenium Web Driver
Selenium Web Driver
Selenium Web Driver

Output on the Console

Selenium Web Driver
Selenium Web Driver

AssertEquals()
AssertEquals() may be a method wont to compare the particular and expected results. If both the particular and expected results are same, then the assertion pass with no exception and therefore the test suit is marked as “passed”. If both the particular and expected results aren’t an equivalent , then the assertion fails with an exception and therefore the test suit is marked as “failed”. Syntax of an AssertEquals() method is given below:
1. Assert.assertEquals(actual,expected);
Let’s understand through an example.
• When variety of adults is 5.

1. package mypack;
2. import org.junit.Assert;
3. import org.openqa.selenium.By;
4. import org.openqa.selenium.WebDriver;
5. import org.openqa.selenium.chrome.ChromeDriver;
6. public class Checkbox_test {
7. public static void main(String[] args)
8. {
9. // TODO Auto-generated method stub
10. System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
11. WebDriver driver = new ChromeDriver();
12. driver.navigate().to("https://www.spicejet.com/"); Assert.assertEquals("5Adult",driver.findElement(By.id("divpaxinfo")).getText());
13. System.out.println(driver.findElement(By.id("divpaxinfo")).getText());
14. }}
Selenium Web Driver
Selenium Web Driver

• When the amount of adults isn’t adequate to 5

Selenium Web Driver
Selenium Web Driver

Output on the console

Selenium Web Driver
Selenium Web Driver

AssertNotEquals()
It is opposite to the function of AssertNotEquals() method. AssertNotEquals() may be a method wont to compare the particular and expected results. If both the particular and expected results aren’t an equivalent , then the assertion pass with no exception and therefore the test suit is marked as “passed”. If both the particular and expected results are same, then the assertion fails with an exception and therefore the test suit is marked as “failed”. Syntax of an AssertNotEquals() method is given below:
1. AssertNotEquals(actual,expected,message);
Let’s understand through an example.
• When actual string isn’t adequate to the expected string.

1. package mypack;
2. import org.junit.Assert;
3. public class Checkbox_test {
4. public static void main(String[] args) {
5. // TODO Auto-generated method stub
6. Assert.assertNotEquals("Hello", "How are you");
7. System.out.println("Hello...This is tecklearn");
8.
9. }
10.
11. }

In the above code, actual string, i.e., Hello isn’t adequate to the expected string, i.e., How are you. Therefore, the assertion passes the test suit . this may execute subsequent statement and therefore the next statement is System.out.println(“Hello…This is tecklearn”);.
Output

Selenium Web Driver
Selenium Web Driver

• When the particular string is adequate to the expected string.

1. package mypack;
2. import org.junit.Assert;
3. public class Checkbox_test {
4. public static void main(String[] args)
5. {
6. // TODO Auto-generated method stub
7. Assert.assertNotEquals("Hello", "Hello");
8. System.out.println("Hello...This is tecklearn");
9. }}

Output

Selenium Web Driver
Selenium Web Driver

AssertNull()
AssertNull() may be a method that verifies whether the thing is null or not. If an object is null, then assertion passes the test suit , and therefore the test suit is marked as “passed”, and if an object isn’t null, then assertion aborts the test suit and therefore the test suit is marked as “failed”. Syntax of AssertNull() method is given below:
1. Assert.assertNull(object);
Let’s understand through an example.
• When an object is null.

1. package mypack;
2. import org.junit.Assert;
3. public class Checkbox_test {
4.
5. public static void main(String[] args) {
6.
7. Assert.assertNull(null);
8. System.out.println("Hello...This is tecklearn");
9. }}

Output

Selenium Web Driver
Selenium Web Driver

• When an object isn’t adequate to null.

1. package mypack;
2. import org.junit.Assert;
3. public class Checkbox_test {
4.
5. public static void main(String[] args) {
6. // TODO Auto-generated method stub
7. Assert.assertNull(10);
8. System.out.println("Hello World");
9.
10. }
11.
12. }

Output

Selenium Web Driver
Selenium Web Driver

AssertNotNull()
AssertNotNull() may be a method that verifies whether the thing is null or not. If an object isn’t null, then assertion passes the test suit and test suit is marked as “passed”, and if an object is null, then assertion aborts the test suit and test suit is marked as “failed”. Syntax of AssertNotNull() method is given below:
1. Assert.assertNotNull(object);
Let’s understand through an example.
• When an object isn’t null.

1. package mypack;
2. import org.junit.Assert;
3. public class Checkbox_test
4. {
5. public static void main(String[] args) {
6. // TODO Auto-generated method stub
7. Assert.assertNotNull(10);
8. System.out.println("C Language");
9.
10. }}

Output

Selenium Web Driver
Selenium Web Driver

• When an object is null.

1. package mypack;
2. import org.junit.Assert;
3. public class Checkbox_test {
4. public static void main(String[] args) {
5. // TODO Auto-generated method stub
6.
7. Assert.assertNotNull(null);
8. System.out.println("C Language");
9.
10. }
11.
12. }

Output

Selenium Web Driver
Selenium Web Driver

SoftAssertion
Till now, we’ve learnt about the Hard Assertion in Web Driver using Testng framework. In hard assertion, if an assertion fails then it aborts the test suit otherwise it continues the execution. Sometimes we would like to execute the entire script albeit the assertion fails. this is often impossible in Hard Assertion. to beat this problem, we’d like to use a soft assertion in testing.
So, this brings us to the end of blog. This Tecklearn ‘Using Assertions in Selenium WebDriver’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/selenium-training-certification/

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.
Why Should you take Selenium Certification Training?
• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium
• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse
Setting the environment in Eclipse for Selenium
• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On
Advantages of Selenium automation testing
• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On
Selenium Web driver Automation
• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On
Deploying Web Drivers for scripting
• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On
Deep dive into Selenium Web Driver
• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On
Switching Operations in WebDriver using Window
• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On
Introduction to TestNG Framework
• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report
JUnit Operations and Test Framework
• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project
Object Repository
• Understanding of Object Repository
• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory
JavaScript Functions
• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Using Assertions in Selenium WebDriver"

Leave a Message

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