Horje
Assertion States in Selenium

In Selenium, assertions are used for verification or checkpoints in the test case. If assertion is not used in the test case, it’s not possible to determine whether the test case is passed or failed. Assertion will be used to generate the test execution reports. And if all the test steps present in the test cases passed successfully, then assertion will not impact the test. It will report only when a test case is failed.

Types of Assertion States in Selenium

1. assert Equal / assert Not Equal

assertEqual:

Verifies if the expected value matches the actual value or not. If both expected value and actual values are equal then it will pass the test otherwise throws an error.

Syntax:

assert actual_value == expected_value

Example:

In this example expected value and actual value are equal. It will run the test successfully.

Python3

# Python program to perform assertEqual
# Import necessary modules from the Selenium library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  
# Specify the path to the ChromeDriver executable
service_obj = Service('C:\\webdrivers_selenium\\chromedriver.exe')
  
# Create a new instance of the Chrome WebDriver, using the specified service
driver = webdriver.Chrome(service=service_obj)
  
# Define the expected title of the web page
Expected_title = "GeeksforGeeks | A computer science portal for geeks"
  
# Navigate to the GeeksforGeeks website
driver.get("/archive/")
  
# Retrieve the actual title of the web page
Actual_title = driver.title
  
# Assert that the actual title is equal to the expected title
assert Actual_title == Expected_title, "Title mismatch"
  
# Close the WebDriver
driver.close()

Output:

assert-equal

assertNotEqual:

Verifies if the expected value does not match the actual value or not. If both expected value and actual values are not equal then it will pass the test otherwise throw’s an error.

Syntax:

assert actual_value != expected_value

Example:

In the below example both expected value and actual value are equal. assertNotEqual will run the test successfully only if both the values are not equal. It will throw an error.

Python3

# Python program for assertNotEqual
  
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  
# Specify the path to the ChromeDriver executable
service_obj = Service('C:\\webdrivers_selenium\\chromedriver.exe')
  
# Create a new instance of the Chrome WebDriver, using the specified service
driver = webdriver.Chrome(service=service_obj)
  
# Define the expected title of the web page
Expected_title = "GeeksforGeeks | A computer science portal for geeks"
  
# Navigate to the GeeksforGeeks website
driver.get("/archive/")
  
# Retrieve the actual title of the web page
Actual_title = driver.title
  
# Assert that the actual title is not equal to the expected title
assert Actual_title != Expected_title, f"Title mismatch: Expected '{Expected_title}', Actual '{Actual_title}'"
  
# Close the WebDriver
driver.close()

Output:

notequal

2. assertTrue/assertFalse

assertTrue:

Verifies whether the given condition is true or not. If it is true then it will continue the test otherwise throw’s an error

Syntax:

assert True

Example:

Both actual and expected titles are the same. so it will enter into the if condition and verify the assertion. if it is true, it will run the test.

Python3

# Importing necessary modules from the Selenium library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  
# Setting up the ChromeDriver service
service_obj = Service('C:\\webdrivers_selenium\\chromedriver.exe')
  
# Creating a new instance of the Chrome WebDriver using the specified service
driver = webdriver.Chrome(service=service_obj)
  
# Defining the expected title of the web page
Expected_title = "GeeksforGeeks | A computer science portal for geeks"
  
# Navigating to the GeeksforGeeks website
driver.get("/archive/")
  
# Retrieving the actual title of the web page
Actual_title = driver.title
  
# Checking if the actual title is equal to the expected title
if Actual_title == Expected_title:
    # Assertion to indicate that the titles match
    assert True
  
# Closing the WebDriver
driver.close()

Output:

assert-true

assertFalse:

Verifies whether the given condition is false or not. If it is false then it will continue the test otherwise, an error

Syntax:

assert False

Example:

Both actual and expected titles are the same. so it will enter into the if condition and verify the assertion. if it is false, it will run the test otherwise throw’s an error.

Python3

# Importing necessary modules from the Selenium library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  
# Setting up the ChromeDriver service
service_obj = Service('C:\\webdrivers_selenium\\chromedriver.exe')
  
# Creating a new instance of the Chrome WebDriver using the specified service
driver = webdriver.Chrome(service=service_obj)
  
# Defining the expected title of the web page
Expected_title = "GeeksforGeeks | A computer science portal for geeks"
  
# Navigating to the GeeksforGeeks website
driver.get("/archive/")
  
# Retrieving the actual title of the web page
Actual_title = driver.title
  
# Checking if the actual title is equal to the expected title
if Actual_title == Expected_title:
    # Assertion to indicate that the titles do not match
    assert False
  
# Closing the WebDriver
driver.close()

Output:

assert-false

3. assertIn / assertNotIn

assertIn:

Verifies whether the given element is present in a specified sequence or not. If it is present then it will continue the test otherwise throw’s an error.

Syntax:

assert “value” in “expected_value”

Example:

The word GeeksforGeeks is present in the title It will pass the test

Python3

# Importing necessary modules from the Selenium library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  
# Setting up the ChromeDriver service
service_obj = Service('C:\\webdrivers_selenium\\chromedriver.exe')
  
# Creating a new instance of the Chrome WebDriver using the specified service
driver = webdriver.Chrome(service=service_obj)
  
# Defining the expected title fragment of the web page
title = "GeeksforGeeks | A computer science portal for geeks"
  
# Navigating to the GeeksforGeeks website
driver.get("/archive/")
  
# Retrieving the actual title of the web page
Actual_title = driver.title
  
# Asserting that the expected title fragment is present in the actual title
assert ("GeeksforGeeks" in title)
  
# Closing the WebDriver
driver.close()

Output:

assert-in

assertNotIn:

Verifies whether given element is not present in specified sequence or not. If it is not present then it will continue the test otherwise throw’s an error.

Syntax:

assert “value” not in “expected_value”

Example:

The word ‘Example’ is not present in the list. So it will throw an error.

Python3

# Importing necessary modules from the Selenium library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  
# Setting up the ChromeDriver service
service_obj = Service('C:\\webdrivers_selenium\\chromedriver.exe')
  
# Creating a new instance of the Chrome WebDriver using the specified service
driver = webdriver.Chrome(service=service_obj)
  
# Defining the expected title fragment of the web page
title = "GeeksforGeeks | A computer science portal for geeks"
  
# Navigating to the GeeksforGeeks website
driver.get("/archive/")
  
# Retrieving the actual title of the web page
Actual_title = driver.title
  
# Asserting that the expected title fragment is not present in the actual title
assert ("GeeksforGeeks" not in title)
  
# Closing the WebDriver
driver.close()

Output:

assert-not-in

4. assertIsNone / assertIsNotNone

assertIsNone:

Verifies if the value is none or not. If the value is none it will execute the test.

Syntax:

assert None

Example:

The value of the title is not none. so it will throw an error

Python3

# Importing necessary modules from the Selenium library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  
# Setting up the ChromeDriver service
service_obj = Service('C:\\webdrivers_selenium\\chromedriver.exe')
  
# Creating a new instance of the Chrome WebDriver using the specified service
driver = webdriver.Chrome(service=service_obj)
  
# Navigating to the GeeksforGeeks website
driver.get("/archive/")
  
# Retrieving the actual title of the web page
title = driver.title
  
# Checking if the title is non-empty
if title:
    # Assertion to indicate that the title is not expected to be present
    assert None
  
# Closing the WebDriver
driver.close()

Output:

assert-none

assertIsNotNone:

Verifies if the value is not none or not. If the value is not none it will execute the test.

Syntax:

assert not None

Example:

The value of title is not none. so it will pass the test

Python3

# Importing necessary modules from the Selenium library
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
  
# Setting up the ChromeDriver service
service_obj = Service('C:\\webdrivers_selenium\\chromedriver.exe')
  
# Creating a new instance of the Chrome WebDriver using the specified service
driver = webdriver.Chrome(service=service_obj)
  
# Navigating to the GeeksforGeeks website
driver.get("/archive/")
  
# Retrieving the actual title of the web page
title = driver.title
  
# Checking if the title is non-empty
if title:
    # Assertion to indicate that the title is expected to be present
    assert not None
  
# Closing the WebDriver
driver.close()

Output:

assert-not-none




Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
Data Serialization (RDS) using R Data Serialization (RDS) using R
What is the Concept Continuous Flow Approach in Kanban? What is the Concept Continuous Flow Approach in Kanban?
How to Integrate QA into the Sprint How to Integrate QA into the Sprint
How to Assign Multiple Variables in One Line in PHP ? How to Assign Multiple Variables in One Line in PHP ?
How to Check a String Contains at least One Letter and One Number in PHP ? How to Check a String Contains at least One Letter and One Number in PHP ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14