Assertions in Selenium Python
In the previous articles on Selenium Python Tutorial, we have covered “How To Handle Mouse & Keyboard Interactions in Selenium Python“. In this tutorial, we will learn Assertions in Selenium Python.Â
In automation testing, the pass and failure test of a test case is determined by the checkpoints or validation points in our tests. Often people may use conditional statements like if-else or switch statements, to conclude a result. However, these statements do not bear any report generating features.
Assertions are the checkpoints in a test case, which help us to conclude on test results and report generation of the test results. There are multiple assertions available, let us discuss some of them.
assertEqual () – This type of assertion can have three parameters. The first parameter in this assertion compares its value with the second parameter. If both the values are the same, the execution continues with a pass result. In case they do not match, the test case fails. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertEqual ("Selenium Python", "Selenium Python", "Comparison Done")
The above scenario shall give a pass result.
assertNotEqual () – This type of assertion can have three parameters. The first parameter in this assertion compares its value with the second parameter. If both the values are not the same, the execution continues with a pass result. In case they match, the test case fails. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertNotEqual ("Selenium", "Selenium Python", "Comparison Done")
The above scenario shall give a pass result.
assertTrue () – This type of assertion can have more than two parameters. It checks if a parameter returns true or false. If true value is returned, the execution continues with a pass result. In case false value is returned, the test case fails. We can compare relational operators with this assertion. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
assertTrue ( "2 > 1", "Comparison Done")
The above scenario shall give a pass result.
assertFalse () – This type of assertion can have more than two parameters. It checks if a parameter returns true or false. If a false value is returned, the execution continues with a pass result. In case true value is returned, the test case fails. We can compare relational operators with this assertion. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
assertFalse ( "1 > 2", "Comparison Done")
The above scenario shall give a pass result.
assertIs () – This type of assertion can have three parameters. The first parameter in this assertion compares its value with the second parameter. If both are the same, the execution continues with a pass result. In case they are not the same, the test case fails. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertIs ("Selenium Python", "Selenium Python", "Comparison Done")
The above scenario shall give a pass result.
assertIsNot () – This type of assertion can have three parameters. The first parameter in this assertion compares its value with the second parameter. If both are not the same, the execution continues with a pass result. In case they are the same, the test case fails. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertIsNot ("Selenium Python", "Selenium", "Comparison Done")
The above scenario shall give a pass result.
assertIsNone () – This type of assertion can have two parameters. The first parameter in this assertion is checked to see if it yields none. If the result is found to be none, the execution continues with the past result. In case it is not none, the test case fails. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
assertIsNone ( r , "Comparison Done")
In the above scenario, r is tested to see if it is none, if yes it shall give a pass result.
assertIsNotNone () – This type of assertion can have two parameters. The first parameter in this assertion is checked to see if it does not yield none. If the result is found to be not none, the execution continues with the pass result. In case it is none, the test case fails. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
assertIsNotNone ( r , "Comparison Done")
In the above scenario, r is tested to see if it is not none, if yes it shall give a pass result.
assertIn () – This type of assertion can have three parameters. The first parameter in this assertion is checked to see if it subsists in the second parameter. If the result is found to be true, the execution continues with a pass result, otherwise, the test case fails. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
j = set(["Python", "Java", "C"]) assertIn ("Python", j , " Comparison Done")
The above scenario shall give a pass result.
assertNotIn () – This type of assertion can have three parameters. The first parameter in this assertion is checked to see if it subsists in the second parameter. If the result is found to be false, the execution continues with a pass result, otherwise, the test case fails. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
j = set(["Python", "Java", "C"]) assertIn ("UFT", j , " Comparison Done")
The above scenario shall give a pass result.
assertIsInstance () – This type of assertion can have three parameters. The first parameter in this assertion refers to an object. The second parameter in this assertion refers to a class. This assertion verifies if the object is an instance of the second parameter class. If yes, the execution continues with a pass result, otherwise, the test case fails. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
browser = webdriver.Chrome (executable_path="C:\\chromedriver.exe") browser.get ("https://www.softwaretestingmaterial.com") assertIsInstance (browser, webdriver, " Comparison Done")
The above scenario shall give a pass result.
assertNotIsInstance () – This type of assertion can have three parameters. The first parameter in this assertion refers to an object. The second parameter in this assertion refers to a class. This assertion verifies if the object is an instance of the second parameter class. If no, the execution continues with a pass result, otherwise, the test case fails. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
browser = webdriver.Chrome (executable_path="C:\\chromedriver.exe") browser.get ("https://www.softwaretestingmaterial.com") assertNotIsInstance (driver, webdriver, " Comparison Done")
The above scenario shall give a pass result. Let us now discuss some of the assertions available for Collections.
assertListEqual () – This type of assertion can have three parameters. The first and second parameter represents two lists. In this assertion, comparison is done between these two lists. If they are found to be similar, the execution continues with a pass result. If not, an error message is thrown that highlights the differences between them. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
assertListEqual ([5,6], [1,3], message)
The above scenario shall give a fail result as two lists are not equal.
assertTupleEqual () – This type of assertion can have three parameters. The first and second parameter represents two tuples. In this assertion, comparison is done between these two tuples. If they are found to be similar, the execution continues with a pass result. If not, an error message is thrown that highlights the differences between them. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
assertTupleEqual ((4,5), (4,5), message)
The above scenario shall give a pass result as two tuples are equal.
assertSetEqual () – This type of assertion can have three parameters. The first and second parameter represents two sets. In this assertion, comparison is done between these two sets. If they are found to be similar, the execution continues with a pass result. If not, an error message is thrown that highlights the differences between them. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
assertSetEqual ( s1, s2, message)
The above scenario shall give a pass result if sets s1 and s2 are equal.
assertDictEqual () – This type of assertion can have three parameters. The first and second parameter represents two dictionaries. In this assertion, comparison is done between these two dictionaries. If they are found to be similar, the execution continues with a pass result. If not, an error message is thrown that highlights the differences between them. The last parameter in this assertion is optional and is included to produce a message.
Syntax:
assertDictEqual ({1:4, 2:5}, {2:5, 3:6}, message)
The above scenario shall give a fail result as two dictionaries are not equal.
Let us now discuss some of the comparison assertions available.
assertAlmostEqual () – This type of assertion can have five parameters. The first parameter in this assertion compares its value with the second parameter. If they are equal roughly, the execution continues with a passing result. In case they are not, the test case fails. The computation is done by calculating the differences between the two then rounding to the number of decimal places as specified in the third parameter called place. The default place value is 7.
The penultimate parameter in this assertion is optional and is included to produce a message. If the last parameter called delta [optional parameter] is mentioned instead of place, then the differences between the first and second parameter should be less than equal to the delta. The default delta value is 0.
Syntax:
assertAlmostEqual (0.2,0.3)
The above scenario shall give a fail result as two parameters are not roughly equal.
assertNotAlmostEqual () – This type of assertion can have five parameters. The first parameter in this assertion compares its value with the second parameter. If they are not equal roughly, the execution continues with a passing result. In case they are roughly equal, the test case fails. The computation is done by calculating the differences between the two then rounding to the number of decimal places as specified in the third parameter called place and then comparing to 0. The default place value is 7.
The penultimate parameter in this assertion is optional and is included to produce a message. If the last parameter called delta [optional parameter] is mentioned instead of place, then the differences between the first and second parameter should be less than equal to the delta. The default delta value is 0.
Syntax:
assertNotAlmostEqual (0.2,0.3)
The above scenario shall give a pass result as two parameters are not roughly equal.
assertGreater () – This type of assertion can have three parameters. The first parameter in this assertion compares its value with the second parameter. If the first parameter is greater than the second, the execution continues with a pass result. In case the second parameter is greater, the test case fails. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertGreater ( 3, 2, "Comparison Done")
The above scenario shall give a pass result.
assertGreaterEqual () – This type of assertion can have three parameters. The first parameter in this assertion compares its value with the second parameter. If the first parameter is greater than equal to the second, the execution continues with a pass result. If not, the test case fails. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertGreaterEqual ( 3, 2, "Comparison Done")
The above scenario shall give a pass result.
assertLess () – This type of assertion can have three parameters. The first parameter in this assertion compares its value with the second parameter. If the first parameter is lesser than the second, the execution continues with a pass result. In case the second parameter is lesser, the test case fails. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertLess ( 3, 2, "Comparison Done")
The above scenario shall give a failed result.
assertLessEqual () – This type of assertion can have three parameters. The first parameter in this assertion compares its value with the second parameter. If the first parameter is less than equal to the second, the execution continues with a pass result. If not, the test case fails. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertLessEqual ( 3, 2, "Comparison Done")
The above scenario shall give a failed result.
assertRegexpMatches () – This type of assertion can have three parameters. The second parameter in this assertion [a regular expression object or a string] searches itself within the first parameter text. If the search is successful, the execution continues with a pass result. If not, an error message is thrown with the pattern and the original text. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertRegexpMatches ("Selenium Python", "Selenium", "Search Done")
The above scenario shall give a pass result.
assertNotRegexpMatches () – This type of assertion can have three parameters. The second parameter in this assertion [a regular expression object or a string] searches itself within the first parameter text. If the search is unsuccessful, the execution continues with a pass result. If not, an error message is thrown with the pattern and the section of the text that matches. The third parameter in this assertion is optional and is included to produce a message.
Syntax:
assertNotRegexpMatches ("Python Test", "Python", "Search Done")
The above scenario shall give a fail result as the regular expression Python is present in the original text Python Test.
In the next article, we will learn Exceptions in Selenium Python
Related posts:
- Selenium Python Tutorial
- Selenium Java Tutorial
- Selenium Interview Questions
- API Testing Tutorial
- Postman Tutorial