Exception handling in Ruby -
in following code want list of failed in rescue
puts "verifying home page" begin page.find('#logoanchor') puts "logo anchor found" page.find('.navbar-inner') puts "header bar found" page.find('.unstyled') puts "found occations frame" page.find('#easyprintpromobox') puts "easy print frame found!" page.find('.tabbable') puts "3 tabs found!" page.find('#givingcardpromobox') puts "create frame found!" page.find('.footer') puts "footer found!" rescue puts "logo anchor not found" end here catch exception if logo anchor not found.i have catch exceptions if 1 of them not found. eg:
if logo anchor not found puts "logo anchor not present " in rescue
if header bar not present puts"header bar not found" in rescue
here's 1 way using array#partition
# dummy placeholder method `page.find` def find(el) if rand < 0.5 raise :nop else true end end selectors = ['#logoanchor', '.navbar-inner', '.unstyled', '#easyprintpromobox', '.tabbable', '#givingcardpromobox', '.footer'] found, not_found = selectors.partition |selector| find(selector) rescue false end puts "found: #{found}" puts "not found: #{not_found}" sample output:
found: ["#logoanchor", ".unstyled", "#easyprintpromobox", ".tabbable"] not found: [".navbar-inner", "#givingcardpromobox", ".footer"] this should work in capybara (not tested):
selectors = ['#logoanchor', '.navbar-inner', '.unstyled', '#easyprintpromobox', '.tabbable', '#givingcardpromobox', '.footer'] found, not_found = selectors.partition |selector| page.find(selector) rescue false end puts "found: #{found}" puts "not found: #{not_found}"
Comments
Post a Comment