π‘ Problem Formulation: When running automated tests with Selenium and Python, ensuring that your web application correctly processes relational comparisons (like whether one value is greater than another) is key. For instance, you might want to verify that a discount price is lower than the original price listed on a retail page. This article unpacks various assertion methods available in Selenium with Python to handle these critical tests.
Method 1: AssertEqual for Direct Comparison
Using assertEqual
is a straightforward way to compare two values for equality. It is handy when you want to check if two variables hold equal values after some operation or interaction on the webpage. If they are not equal, the test will fail.
Here’s an example:
import unittest from selenium import webdriver class ComparisonTest(unittest.TestCase): def test_equality(self): driver = webdriver.Chrome() driver.get("http://www.example.com") price = driver.find_element_by_id("price").text discounted_price = driver.find_element_by_id("discounted_price").text self.assertEqual(price, discounted_price) if __name__ == "__main__": unittest.main()
The output will be a passed test if price
equals discounted_price
, otherwise, the test will fail.
This code snippet sets up a test where it opens a web page, retrieves text from two elements, and compares them using assertEqual
. The assertion passes if both values are the same, indicating no error in the relational comparison logic on the web page.
Method 2: AssertGreater for Superiority Checks
The assertGreater
method allows you to test if one value is greater than another. This assertion is particularly useful when you want to ensure that one number should logically exceed another, like in the case of a counter or a high score.
Here’s an example:
import unittest from selenium import webdriver class ComparisonTest(unittest.TestCase): def test_greater(self): driver = webdriver.Chrome() driver.get("http://www.examplegame.com") high_score = driver.find_element_by_id("high_score").text current_score = driver.find_element_by_id("current_score").text self.assertGreater(int(high_score), int(current_score)) if __name__ == "__main__": unittest.main()
The output will be a passed test if high_score
is greater than current_score
, otherwise, the test will fail.
This code snippet demonstrates the use of assertGreater
to confirm that the ‘high_score’ from the webpage is indeed higher than the ‘current_score’. This assertion helps validate that the high score tracking feature of a gaming website is functioning properly.
Method 3: AssertLess for Inferiority Checks
Opposite to assertGreater
, assertLess
is the method to use when you want to validate that one value is indeed less than another. This could apply, for example, when testing sorting functions or checking that a timed event occurs within an expected timeframe.
Here’s an example:
import unittest from selenium import webdriver class ComparisonTest(unittest.TestCase): def test_less(self): driver = webdriver.Chrome() driver.get("http://www.examplestore.com") start_price = driver.find_element_by_id("start_price").text sale_price = driver.find_element_by_id("sale_price").text self.assertLess(int(sale_price), int(start_price)) if __name__ == "__main__": unittest.main()
The output will be a passed test if sale_price
is less than start_price
, otherwise, the test will fail.
In this snippet, assertLess
is employed to check that the ‘sale_price’ found on the webpage is lower than the ‘start_price’, which would typically be the expected behavior for a product on sale, thereby validating the pricing logic.
Method 4: AssertGreaterEqual for Non-strict Superiority Checks
When you want to test if a value is greater than or equal to another, assertGreaterEqual
is the appropriate method. It’s useful for times when values are acceptable to be either equal or one greater than the other, such as when verifying a minimum threshold has been met or exceeded.
Here’s an example:
import unittest from selenium import webdriver class ComparisonTest(unittest.TestCase): def test_greater_equal(self): driver = webdriver.Chrome() driver.get("http://www.exampleportal.com") min_visits = 100 actual_visits = driver.find_element_by_id("visit_count").text self.assertGreaterEqual(int(actual_visits), min_visits) if __name__ == "__main__": unittest.main()
The output will be a passed test if actual_visits
is greater than or equal to min_visits
, otherwise, the test will fail.
This block asserts that ‘actual_visits’ retrieved from the webpage is at least equal to the ‘min_visits’, thereby verifying threshold-based functionalities or conditions within web applications.
Bonus One-Liner Method 5: assertTrue for Custom Comparisons
When more complex or custom relational comparisons are needed, writing a lambda or custom function and passing it to assertTrue
can give you the flexibility you need. This way, any comparison returning a Boolean can be turned into an assertion.
Here’s an example:
import unittest from selenium import webdriver class ComparisonTest(unittest.TestCase): def test_custom_comparison(self): driver = webdriver.Chrome() driver.get("http://www.example.com") value1 = driver.find_element_by_id("value1").text value2 = driver.find_element_by_id("value2").text self.assertTrue(lambda: int(value1) * 2 == int(value2)) if __name__ == "__main__": unittest.main()
The output will be a passed test if the lambda function returns True
, i.e., if value1
times 2 equals value2
. Otherwise, the test will fail.
This code snippet illustrates how to create a custom relational check using a lambda function within assertTrue
. This method provides the greatest flexibility for complex comparisons that aren’t natively supported by other assertions.
Summary/Discussion
- Method 1: AssertEqual. Best for simple equality checks. Limited to comparing values directly without any logical variations.
- Method 2: AssertGreater. Ideal for verifying that one number is higher than another. Only useful for greater-than checks.
- Method 3: AssertLess. Suited for validating an expected lower value. Can’t be used for greater-than or equality scenarios.
- Method 4: AssertGreaterEqual. Useful for non-strict superiority verification. Doesnβt differentiate between greater than and equal conditions.
- Method 5: assertTrue. Offers maximum flexibility for custom comparisons. Requires writing custom logic, which may add complexity.