Updated April 8, 2023
Introduction to Assertions in Selenium
The following article provides an outline on Assertions in Selenium. An assertion is used to compare the actual result of an application with the expected result. AssertEquals method compares the expected result with that of the actual result. It throws an AssertionError if the expected result does not match with that of the actual result and terminates the program execution at the assertequals method.
AssertTrue method asserts that a specified condition is true. It throws an AssertionError if the condition passed to the asserttrue method is not satisfied. AssertFalse method asserts that a specified condition is false. It throws an AssertionError if the condition passed to assert false method is not satisfied. AssertEquals, AssertTrue, and AssertFalse methods are the most commonly used assertions in selenium. Generally assertions verifies whether the application is same or not when we check with our expectation.
Selenium assertions are of three types.
- assert
- verify
- waitFor
Syntax for assertion:
assert Expression[, message]
In the above syntax it returns Result , if the condition True. If the condition False it will print the message instead of Assertion Error.
Types of Assertions in Selenium
There are two types of assertions in selenium.
1. Hard Assertions
- A hard assertion does not continue with execution until the assertion condition is True.
- Hard assertions usually throw an Assertion Error whenever an assertion condition fails.
- The test case will be immediately marked as Failed when a hard assertion condition fails.
2. Soft Assertions
- A soft assertion continues with test execution even if the assertion condition fails.
- Soft Assertion does not throw any error when the assertion condition fails but continues with the next step of the test case.
Assertion Methods
There are different types of assertion methods as below, which works for java.
1. assertEquals
assertEquals method compares the expected result with that of the actual result. It throws an AssertionError if the expected result does not match with actual result and terminates the program execution.
Example:
String expected = "https://www.google.com";
String actualURL= "https://www.google.com";
Assert.assertEquals(expected, actualURL);
2. assertTrue
assertTrue method checks whether the condition is true or not.
It takes in two parameters i.e. one is the message and the other is the condition against which the assertion needs to be applied. It throws an AssertionError if the condition passed to the assertTrue method is not satisfied.
Example:
Assert.assertTrue("Assert True test message", true);
3. assertFalse
assertFalse method check whether the condition is true or not.
It takes in two parameters i.e. one is the message and the other is the condition against which the assertion needs to be applied. It throws an AssertionError if the condition passed to the assertFalse method is not satisfied.
Example:
Assert.assertFalse("Assert false test message" false);
4. assertNull
assertNull is used to verify if the provided object contains a Null value. It takes an object as the parameter and throws an AssertionError if the provided object does not hold a Null value.
Example:
DemoClass demo = new DemoClass();
Assert.assertNull(demo);
5. assertNotNull
assertNotNull is used to verify that a provided object does not hold a Null value. It takes an object as the parameter and throws an AssertionError if the provided object does not contain a Null value.
Example:
DemoClass demo = new DemoClass();
Assert.assertNotNull(demo);
6. assertSame
assertSame method checks if two objects provided as parameters refer to the same object. It throws an AssertionError if the provided objects do not refer to the same object with the message provided.
Example:
DemoClass1 demo1 = new DemoClass1();
DemoClass2 demo2= new DemoClass2();
Assert.assertSame("Two objects are equal", demo1, demo2);
7. assertNotSame
assertNotSame verifies that two objects are not equal. If two objects to refer to the same object, then an AssertionError will be thrown.
Example:
DemoClass1 demo1 = new DemoClass1();
DemoClass2 demo2= new DemoClass2();
Assert.assertNotSame("Two objects are not equal", demo1, demo2);
8. assertArrayEquals
assertArrayEquals verifies that two object arrays are equal. If both the arrays hold Null values, then they can be considered as equal. This method throws an AssertionError with the message provided if both the object arrays are not considered equal.
Example:
String[] expected = {"Mango","Apple","Banana"}
String[] actual = {" Mango","Apple","Banana"}
Assert.assertArrayEquals(expected,actual);
Assertion Methods used for Application Testing
Below are the major selenium assertion methods used for application testing:
- assertEqual
- assertTrue
- assertFalse
Consider the following is our source:
Code:
<html>
<body>
<p>Are you sure you want to do this?</p>
<a href="continue.html">Continue</a>
<a href="cancel.html">Cancel</a>
</body>
<html>
Below is the code to check the element from a source with selenium.
Code:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('file:///home/naresh/Desktop/abc.html')
element = browser.find_element_by_partial_link_text('Conti')
print(element.text)
browser.quit()
Output:
In the above picture we were getting text from a web source with selenium partial link text method.
Now we are performing the asserts in the same source and code.
In this example we are taking unit test to perform assertion methods.
1. assertEqual
assertEqual checks for the expected result.
Code:
<html>
<body>
<p>Are you sure you want to do this?</p>
<a href="continue.html">Continue</a>
<a href="cancel.html">Cancel</a>
</body>
<html>
Below code is used to check the assertEqual method.
Code:
from selenium import webdriver
import unittest
class Testassertons(unittest.TestCase):
def test_assert(self):
self.browser = webdriver.Chrome()
self.browser.get('file:///home/naresh/Desktop/abc.html')
self.element = self.browser.find_element_by_partial_link_text('Conti')
self.assertEqual(self.element.text, 'Continue')
self.browser.quit()
if __name__ == '__main__':
nittest.main()
Output:
2. assertTrue
It checks for the condition whether it is true or not.
Code:
<html>
<body>
<p>Are you sure you want to do this?</p>
<a href="continue.html">Continue</a>
<a href="cancel.html">Cancel</a>
</body>
<html>
Programming Code:
from selenium import webdriver
import unittest
class Testassertons(unittest.TestCase):
def test_assert(self):
self.browser = webdriver.Chrome()
self.browser.get('file:///home/naresh/Desktop/abc.html')
self.element = self.browser.find_element_by_partial_link_text('Conti')
self.assertTrue(self.element.text.istitle())
self.browser.quit()
if __name__ == '__main__':
unittest.main()
In the above example we are checking the condition , if it is True it returns “OK” otherwise it returns AssertionError.
Output:
3. assertFalse
It checks for the condition whether it is false or not.
Code:
<html>
<body>
<p>Are you sure you want to do this?</p>
<a href="continue.html">Continue</a>
<a href="cancel.html">Cancel</a>
</body>
<html>
Programming Code:
from selenium import webdriver
import unittest
class Testassertons(unittest.TestCase):
def test_assert(self):
self.browser = webdriver.Chrome()
self.browser.get('file:///home/naresh/Desktop/abc.html')
self.element = self.browser.find_element_by_partial_link_text('Conti')
self.assertFalse(self.element.text.isupper())
self.browser.quit()
if __name__ == '__main__':
unittest.main()
In the above code it checks for the condition whether it is False or not.
If the condition False it returns “OK” , otherwise it returns an AssertionError.
Output:
Recommended Articles
We hope that this EDUCBA information on “Assertions in Selenium” was beneficial to you. You can view EDUCBA’s recommended articles for more information.