SELENIUM BASICS:


SELENIUM WEBDRIVER:


TESTNG:


REPORTS:


BUILD & CONTINUOUS INTEGRATION TOOLS:


CROSS BROWSER TESTING:


MOBILE TESTING:


AUTOMATION FRAMEWORKS:


API Testing:


INTERVIEW QUESTIONS:


SAMPLE RESUME:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

Sharing is caring.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar SM

Leave a Comment

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

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on whatsapp

Recent Posts:

Scroll to Top
API Testing eBook

DOWNLOAD FOR FREE