I am trying to take a screenshot using take_screenshot() (Hound). I just need to capture the screenshot on failure.
I have tried try/rescue however rescue block always executed even though assertion fails.
try do
// some page elements action
assert visible_page_text(), "Hello World"
rescue
_ -> take_screenshot()
end
I also tried,
try do
// some page elements action
assert visible_page_text(), "Hello World"
catch
_ -> take_screenshot()
end
I want, if assertion fails, only then it should take screenshot.
With a small modification your code works:
Or turn it into a macro:
And call it like this:
Update: As you mentioned, this will only take screenshots when doing an assertion. That can be fixed though.
Here is a macro that will wrap the contents of a whole test in a try/rescue block and save a screenshot on any error. As a bonus, it prefixes the screenshot with the name of the test.
The big drawback is that you lose the stracktrace, so it's harder to pinpoint the failing line of test code.(solved withcatch
instead ofrescue
) Put the macro insupport/conn_case.ex
or somewhere else if you prefer:And call it like a normal test:
Now it should work for all kinds of errors.