Home > Enterprise >  how to use python assert to determine if a html element has loaded
how to use python assert to determine if a html element has loaded

Time:01-12

I am trying to use python's assert construct to determine if a string has appeared in html to confirm a given page has loaded.

(noting selenium is installed but I want to instead use python's assert while I am learning about asserts..)

When I attempt to run this, expecting a negative result, I receive the standard NoSuchElement type exception, while the actual 'assert' construct itself seems to do nothing.

Should I instead be using python's try except on its own or use selenium assertions instead?

Following is example of what I am trying to do and its result:

  1. constants definition py file..

SEARCH_RESULTS_PAGE_STRING = "Search results" <- I expect this to pass and it seems to run without error..

SEARCH_RESULTS_PAGE_STRING = "Search duh results" <- I expect this to fail and it does, but mentions nothing about an AssertionError in the runtime output.. It just says:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[text()='Search duh results']"}
  1. in my class / method definition py file..

    try:

    assert const.SEARCH_RESULTS_PAGE_STRING == self.find_element(By.XPATH, f"//span[text()='{const.SEARCH_RESULTS_PAGE_STRING}']").text, f"'{const.SEARCH_RESULTS_PAGE_STRING}' has not been found on the Search Results page."

    except AssertionError as assertErrorMsg: print(assertErrorMsg)

CodePudding user response:

The issue is that the assert statement is not raising the AssertionError when the self.find_element method throws a NoSuchElementException. Instead, you should use a try-except block to catch the NoSuchElementException and raise an AssertionError with a custom message.

Here's an example of how you could do this:

try:
element = self.find_element(By.XPATH, f"//span[text()='{const.SEARCH_RESULTS_PAGE_STRING}']")
assert const.SEARCH_RESULTS_PAGE_STRING == element.text, f"'{const.SEARCH_RESULTS_PAGE_STRING}' has not been found on the Search Results page."
except NoSuchElementException:
raise AssertionError(f"The string '{const.SEARCH_RESULTS_PAGE_STRING}' was not found on the page")

This way, if the self.find_element method throws a NoSuchElementException, the custom message will be printed and the test will fail, and if the string is found and the value matches, the assert statement will pass.

Alternatively, you can use selenium assertions instead of python assert statements by using self.assertIsNotNone(element) and self.assertEqual(element.text, const.SEARCH_RESULTS_PAGE_STRING). But make sure you import unittest before using self.assertIsNotNone and self.assertEqual

from unittest import TestCase
class MyTest(TestCase):
def test_search_results_page(self):
    element = self.find_element(By.XPATH, f"//span[text()='{const.SEARCH_RESULTS_PAGE_STRING}']")
    self.assertIsNotNone(element)
    self.assertEqual(element.text, const.SEARCH_RESULTS_PAGE_STRING)

This way, it will give you a more informative assert error message, and also it will be more readable and maintainable.

  • Related