I would imagine that it would be more simple to test for return values from the function itself. If you are expecting it to not be None, or Null it would be much easier to test this way:
from selenium.webdriver import WebDriverWait
import selenium.common.exceptions
def number_of_doors():
try:
doors = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".number .doors"))).text
windows = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".number .windows"))).text
if doors is not None and windows is not None:
# return true or false, and a test of if they match or not. you can adjust this as needed, but this will check if both values contain anything.
return {
"Success": True,
"Do_They_Match": doors == windows
}
else:
# Since we only care about a True value being in success, return it as false, and do they match as a none type.
return {
"Success": False,
"Do_They_Match": None,
"Error": None,
"Error_Message": None
}
except (selenium.common.exceptions, Exception) as e:
# Return a dict object, and be able to pull out values you would absolutely need. Error information, success value, and do they match being none as nothing was tested.
return {
"Success": False,
"Do_They_Match": None,
"Error": True,
"Error_Message": "{}".format(e)
}
ax = number_of_doors()
if ax['Success']:
# If success is true, extract out the values we need.
print("Success:{}\nDo they match?{}".format(ax['Success'], ax['Do_They_Match']))
elif ax['Success'] is False:
# check if its false, and if error is there or not.
if ax['Error'] and len(ax['Error_Message']) != 0:
# If error is in the dict object, then we can pull out what the error was.
error = ax['Error_Message']
else:
error = None
print("Success: {}\nError:{}".format(ax['Success'], error))