ruby - Instantiating a watir browser on test failure in Cucumber without using the singleton pattern -
i've been trying create "cucumber seed" project can use bootstrap browser-based acceptance test suite. alister scott has written common antipattern of closing , reopening browser windows (which adds unnecessary delay test suite), can avoided adding following env.rb file:
browser = watir::browser.new before @browser = browser end at_exit browser.close end
but i'm unsure of how incorporate behavior, while still restarting browser when test fails (this ensures failed test not leak browser state , cause cascade of failures unrelated scenarios).
my current approach wrap watir::browser
instantiation in browser
singleton class, so:
# support/browser.rb class browser class << self def instance @browser end def start(name: 'chrome') @name = name @browser = watir::browser.new(name.to_sym) end def stop @browser.close # if page hangs fail selenium unknown error rescue selenium::webdriver::error puts 'failed close old browser (possibly because page hanging), starting new one!' end def restart stop start(name: @name) end end # singleton methods end
and call in env.rb/hooks file so:
browser.start before @browser = browser.instance end after |scenario| browser.restart if scenario.failed? end at_exit browser.stop end
which works, there better way write this? avoids having use singleton, perhaps?
i don't think approach useful if application not stable. suggest creating new instance of browser , closing @ end of scenario.
Comments
Post a Comment