ZAP自动化:未定义的方法`[]' for nil:NilClass(NoMethodError)

时间:2017-11-27 09:35:29

标签: ruby capybara zap

  

ZAP自动化:未定义的方法`[]' for nil:NilClass(NoMethodError)

我在尝试使用ruby获取zap的响应时遇到上述错误。下面是我的代码:

Then(/^I should be able to see security warnings$/) do

#Get response from via RestClient framework method.
begin
  response = JSON.parse RestClient.get "http://#{$zap_proxy}:#{$zap_proxy_port}/json/core/view/alerts"
  rescue RestClient::ServerBrokeConnection

   #Classify the alerts
   events = response['alerts']
   high_risks = events.select{|x| x['risk'] == 'High'}
   high_count = high_risks.size
   medium_count = events.select{|x| x['risk'] == 'Medium'}.size
   low_count = events.select{|x| x['risk'] == 'Low'}.size
   informational_count = events.select{|x| x['risk'] == 'Informational'}.size
end

#Check high alert count and print them
if high_count > 0
  high_risks.each { |x| p x['alert'] }
end

#Expect high alert count equal to 0
expect(high_count).to eq 0

#Print alerts with risk levels
site = Capybara.app_host
response = JSON.parse RestClient.get "http://#{$zap_proxy}:#{$zap_proxy_port}/json/core/view/alerts",
params: { zapapiformat: 'JSON', baseurl: site }
response['alerts'].each { |x| p "#{x['alert']} risk level: #{x['risk']}"}
end
有人请帮帮我。我的目的是打印安全警报并在命令提示符下显示它们

1 个答案:

答案 0 :(得分:0)

我认为您在nil中有events值,并且您尝试从x['...']获取值nil。 它需要更多的细节,包括线。

编辑:

尝试events = response['alerts'].reject { |x| x.nil? }