The assertion is used as a verification point. It verifies that the state of the application conforms to what is expected. The types of assertion are “assert”, “verify” and “waitFor”.
In Selenium, assertions are used to verify whether a given condition is true or false in the context of automated testing. Assertions play a crucial role in test automation as they help ensure that the expected behavior of a web application is met during the testing process. If an assertion fails, it indicates that the application is not behaving as expected, and the test script will typically stop execution at that point.
For example, consider a scenario where you want to verify if a certain element is present on a web page. You can use an assertion to check for the presence of the element, and if the assertion fails, the test script will report an error.
Here’s a simple example using Python with Selenium:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest
class MyTestCase(unittest.TestCase):def setUp(self):
self.driver = webdriver.Chrome()
def test_element_presence(self):self.driver.get(“https://example.com”)
try:
# Check if the element with ID “exampleElement” is present
assert self.driver.find_element_by_id(“exampleElement”).is_displayed()
print(“Element is present on the page.”)
except NoSuchElementException:
print(“Element is not present on the page.”)
def tearDown(self):
self.driver.quit()
if __name__ == “__main__”:
unittest.main()
In this example, the assert
statement is used to check if the element with the ID “exampleElement” is displayed on the web page. If the element is not found, a NoSuchElementException
will be caught, and the test script will report that the element is not present on the page.
Assertions are crucial for validating the expected behavior of your web application and ensuring the accuracy of your test cases in Selenium automation.