1

在下面的代码中,我想处理异常。msg[0]如果没有找到,我必须msg[2]在救援中捕获该异常消息,如果找到,则放置成功消息msg[1]

puts "Verifying Home Page"
 def verifyHomepage(*args)
 begin
  args.each do |msg|    
    page.find(msg[0])
    puts msg[1]
    rescue
    puts msg[2]      
  end
end
end
verifyHomepage(['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar  not Found'])

在上面的代码中,我得到 error sysntax error unexpected keyword rescue expecting keyword end

4

2 回答 2

2

萨利尔已经指出你在哪里修复,这是正确的。现在您也可以采用以下方法:

puts "Verifying Home Page"

def verifyHomepage(*args)
  args.each do |msg|   
    next puts(msg[1]) if page.find(msg[0]) rescue nil  
    puts msg[2]     
  end
end
a = [['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar  not Found']]
verifyHomepage(*a)

输出:

Verifying Home Page
Logo anchor not Found
Header Bar  not Found
于 2013-05-16T06:37:26.637 回答
1

你必须在begin里面写block

puts "Verifying Home Page"
def verifyHomepage(*args)  
    args.each do |msg|
      begin
        page.find(msg[0])
        puts msg[1]
      rescue
        puts msg[2]
    end
  end
end
verifyHomepage(['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar  not Found'])
于 2013-05-16T05:12:08.290 回答