My Capybara tests are designed to call page.save_screenshot
if an example fails. This is configured in a config.after(:each)
block in my spec_helper
.
However, not all of my tests always have a window open. Long story short, some of my tests may make a few requests using rest-client
and then pass/fail based on some rspec expectations
. When these type of tests fail, a browser window opens because of the call to page.save_screenshot
.
Is there a way to add a conditional so that saving a screenshot is only attempted when there is a window (headless or non-headless) open?
CodePudding user response:
You can probable do something like
page.save_screenshot if page.current_window.exists?
however the best options would really be to make tests that don't use the browser a different type of test (not system or feature) or add metadata to the test so that metadata can be used to determine whether or not the test uses the browser
# In your tests
it 'does something not using the browser', :no_browser do
...
end
# In your spec_helper
config.after(:each) do |example|
page.save_screenshot unless example.metadata[:no_browser]
end