Python crashes when assert fails

Viewed 28

I'm trying to use Playwright and pytest to test my website. My code keeps crashing when it fails. Below I've explained the things I've tried and why it keeps crashing. I could hack out a solution that wouldn't crash (initialise test first), but it's sloppy and I know there's a much better way to do it.

My question: When an org is successfully created, a popup appears saying 'Organisation has been updated'. How can I do an assertion that this element has appeared, without crashing if it doesn't? Thank you.

My first attempt. This crashes because page.locator couldn't find the element it was expecting.

test = page.locator("text=Organisation has been updated").inner_html()
assert test != ''

My second attempt. This crashes as test is undefined

try:
    test = page.locator("text=Organisation has been updated").inner_html()
except:
    pass
assert test != ''
0 Answers
Related