简体   繁体   English

黄瓜从一个步骤调用外部红宝石功能?

[英]Cucumber calling an external ruby function from a step?

I have a ruby feature written like this: 我有一个像这样编写的ruby功能:

#./features/sjsonTesting.feature
Feature: Validate DUT JSON
    JSON should be evaluated for all routes in API
    All API routes should return valid JSON
    If JSON is invalid for one or more route in API it has DUT failed 

Scenario Outline: Validate JSON
    Given there is a DUT with <IP> and <USERNAME> and <PASSWORD>
    Then it should return the word PASSED

  Examples:
    |IP             |USERNAME|PASSWORD|
    |'172.168.101.139'|admin   |test    |


I then have step definitions like:
#./features/step_definition/jsonSteps.rb
Given(/^there is a DUT with '(\d+)\.(\d+)\.(\d+)\.(\d+)' and admin and test$/) do |arg1, arg2, arg3, arg4|
  testdev(arg1)#pending # express the regexp above with the code you wish you had
end

Then(/^it should return the word PASSED$/) do
  testdev(arg1)#pending # express the regexp above with the code you wish you had
end


as well as a support file:
#./support/support.rb
#!/usr/bin/ruby
require 'uri'
require 'net/http'
require 'net/http/digest_auth'
require 'json' 
require 'rubygems' 


def is_json(json)
  begin
    JSON.parse(json.to_json)
    return true
  rescue Exception => e
    print e
    return false
  end
end



def gethash(route)
    digest_auth = Net::HTTP::DigestAuth.new
    uri = URI.parse route
    uri.user = 'admin'
    uri.password = 'terraceqam'
    h = Net::HTTP.new uri.host, uri.port
    h.use_ssl = true
    h.verify_mode = OpenSSL::SSL::VERIFY_NONE
    req = Net::HTTP::Get.new uri.request_uri
    res = h.request req
    #puts res['www-authenticate']
    auth = digest_auth.auth_header uri, res['www-authenticate'], 'GET'
    #puts auth
    req = Net::HTTP::Get.new uri.request_uri
    #puts req
    req.add_field 'Authorization', auth
    res = h.request req
    #puts res.body
    data = JSON.parse(res.body)
    return data
end


def testdev(ip)
   test = "PASSED"
   hash = gethash('https://' + ip +'/views/')
   hash["views"].each do |view|
     routeapi = 'https://' + ip + '/views/' + view
     #print routeapi + "---" 
     subhash =  gethash(routeapi)
     answer = is_json(subhash)  
     if answer == false 
      test = "FAILED"
     end
   end
   return test
end

when I run cucumber I get: 当我运行黄瓜时,我得到:

root@FPGA:/home/robm/code/BDD/testtq# cucumber
Feature: Validate DUT JSON
    JSON should be evaluated for all routes in API
    All API routes should return valid JSON
    If JSON is invalid for one or more route in API it has DUT failed

  Scenario Outline: Validate JSON                                # features/testJson.feature:6
    Given there is a DUT with <IP> and <USERNAME> and <PASSWORD> # features/step_definition/REST_Testing_Steps.rb:2
    Then it should return the word PASSED                        # features/step_definition/REST_Testing_Steps.rb:6

    Examples: 
      | IP                | USERNAME | PASSWORD |
      | '172.168.101.139' | admin    | test     |
      Invalid argument - connect(2) (Errno::EINVAL)
      ./features/support/testJson.rb:30:in `gethash'
      ./features/support/testJson.rb:46:in `testdev'
      ./features/step_definition/REST_Testing_Steps.rb:3:in `/^there is a DUT with '(\d+)\.(\d+)\.(\d+)\.(\d+)' and admin and test$/'
      features/testJson.feature:7:in `Given there is a DUT with <IP> and <USERNAME> and <PASSWORD>'

Failing Scenarios:
cucumber features/testJson.feature:6 # Scenario: Validate JSON

1 scenario (1 failed)
2 steps (1 failed, 1 skipped)
0m0.058s

it has a problem with ./features/support/testJson.rb:30:in `gethash' which is res = h.request req 它有./features/support/testJson.rb:30:'gethash'的问题,这是res = h.request req

why cant I get a request ? 为什么我得不到请求? in that helper code , when i run the code at the command line it works fine. 在那个帮助程序代码中,当我在命令行运行代码时它工作正常。

A simpler way would be: 一种更简单的方法是:

Change to double quotes: 更改为引号:

Examples: 
  | IP                | USERNAME | PASSWORD |
  | "172.168.101.139" | admin    | test     |

and match the whole string 并匹配整个字符串

Given(/^there is a DUT with "(.*?)"$/) do |ip|
  puts  ip  #puts full IP address as string
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM