I have written my selenium script in Python and i am verifying every page by the text "Home"... I am verifying it by hard assertion ,how can i modify it by using soft assertion so that if test fails it does not stop my test script. My selenium python script is :
from selenium import webdriver
import selenium.webdriver.common.action_chains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome('C:\\Users\\maham\\Downloads\\chromedriver.exe')
driver.get("https://www.argaam.com/en")
driver.maximize_window()
nav_list= driver.find_elements_by_css_selector("#nav > li")
size = nav_list.__len__()
for i in range(0, size):
value = driver.find_elements_by_css_selector("#nav > li")
try:
element_to_hover_over = value[i]
hover = selenium.webdriver.common.action_chains.ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
#drop_down_list = driver.find_elements_by_css_selector("#nav ul li")[i].find_element_by_tag_name("ul").find_element_by_class_name('holder-full').find_element_by_tag_name('li').find_elements_by_tag_name('a')
drop_down_list = element_to_hover_over.find_elements_by_css_selector("a");
size1 = drop_down_list.__len__()
if size1 > 1:
for j in range(0, size1):
if j != 0:
value1 = driver.find_elements_by_css_selector("#nav > li")
element_to_hover_over = value1[i]
hover = selenium.webdriver.common.action_chains.ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
value2 = drop_down_list = element_to_hover_over.find_elements_by_css_selector("a")[j];
value2.click();
ele = driver.find_element_by_css_selector("#mnu_Home > a")
assert ele.text == 'Home'
else:
value[i].click()
except TypeError:
value[i].click()
Instead of
assert ele.text == 'Home'
you can use:
try:
assert ele.text == 'Home'
except AssertionError:
print("Assertion failed. Actual value is %s" % ele.text)