执行黄瓜时未定义的方法

Undefined Method when executing cucumber

我写了一个黄瓜场景,步骤如下

Scenario: Validate in orders list the order status "Order Creates"
  When I click on the orders button
  And the order "14103795" is visible  
  # Then I can see the screen with the list of orders
  Then I can see the order status "Order Created", the message "Order successfully created", the order date "XXXX", the deliver date "XXXX" or the cancellation date "XXXX". 

当我 运行 .feature 文件时,它会执行,但是当文件完成 运行:

时出现以下错误
undefined method `last' for #<String:0x007ffdba07f5e8> (NoMethodError)
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/runner.rb:101:in `execute'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/runner.rb:46:in `execute'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/runner.rb:30:in `around_hook'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/around_hook.rb:11:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:92:in `block (2 levels) in compose_around_hooks'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:91:in `call'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:91:in `compose_around_hooks'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:22:in `block in describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/runner.rb:17:in `call'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/runner.rb:17:in `test_case'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:21:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/filters/prepare_world.rb:11:in `test_case'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:21:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/filters/apply_around_hooks.rb:8:in `test_case'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:21:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/filters/apply_after_hooks.rb:5:in `test_case'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:21:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/filters/apply_before_hooks.rb:5:in `test_case'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:21:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/filters/apply_after_step_hooks.rb:8:in `test_case'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:21:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/filters/activate_steps.rb:8:in `test_case'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:21:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/filters/quit.rb:11:in `test_case'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/case.rb:21:in `describe_to'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/filters/locations_filter.rb:17:in `block in done'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/filters/locations_filter.rb:16:in `each'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/filters/locations_filter.rb:16:in `done'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/filter.rb:61:in `done'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/test/filters/tag_filter.rb:18:in `done'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/compiler.rb:23:in `done'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core/gherkin/parser.rb:31:in `done'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core.rb:29:in `parse'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-core-1.2.0/lib/cucumber/core.rb:18:in `compile'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/runtime.rb:70:in `run!'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/lib/cucumber/cli/main.rb:38:in `execute!'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/gems/cucumber-2.0.2/bin/cucumber:9:in `<top (required)>'
/Users/galibon/.rvm/rubies/ruby-1.9.3-p551/bin/cucumber:23:in `load'
/Users/galibon/.rvm/rubies/ruby-1.9.3-p551/bin/cucumber:23:in `<main>'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/bin/ruby_executable_hooks:15:in `eval'
/Users/galibon/.rvm/gems/ruby-1.9.3-p551@global/bin/ruby_executable_hooks:15:in `<main>'

而且,最后一步没有创建步骤骨架。

有人可以帮我解决这个问题吗?

这是步骤定义

When(/^I click on the orders button$/) do
    @orders = Orders.new
    @orders.click_orders
end

When(/^the order "([^"]*)" is visible$/) do |order|
    @orders.locateOrders(order)
end

这是我创建的页面对象中的 类

def click_orders
    click_link('Pedidos')
    sleep(4)
end

def locateOrders(order)
    exists = false
    while exists == false
        if page.has_content?(order)
            exists=true
            @order_num=order
            puts 'exists'
        elsif page.has_content?(order) == false
            if page.has_content?("Carregar mais pedidos")
                click_button("Carregar mais pedidos")
                sleep(5)
            else
                fail(ArgumentError.new('Pedido não encontrado'))
            end
        end
    end 
end


def order_verify(order_status)
    sleep(3)
    counter = page.all(:xpath, '//*[@id="sh-content"]/div[1]/div[2]/div').count
    for i in 1..counter.to_i
        puts i
        within(:xpath, '//*[@id="sh-content"]/div[1]/div[2]/div['+i.to_s+']/div'){
            @card_order = find(:xpath, '//*[@id="sh-content"]/div[1]/div[2]/div['+i.to_s+']/div/div[1]/div[1]/span[2]').text
            puts @card_order
        }
        if @card_order == @order_num
            within(:xpath, '//*[@id="sh-content"]/div[1]/div[2]/div['+i.to_s+']/div'){


            }
        end
    end
end

最后一步, 将调用 order_verify 方法,当我 运行 场景时没有生成骨架。

这是我的 env.rb 文件

require 'selenium/webdriver'
require 'rubygems'
require 'rspec'
require 'json'
require 'capybara/cucumber'



include Capybara::DSL
Capybara.default_driver = :chrome
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

Capybara.default_wait_time = 2

解决了。显然这是 cucumber 版本 2.0.2 的问题。

我刚刚卸载它并安装了 2.0.0 版 使用 cucumber-core 1.1.3 并且错误不再发生。它还生成了步骤骨架。

如果您想同时使用最新的 Cucumber 2.0.2 和 capybara 2.5.0,请对您的 'env.rb' 文件进行以下更改。

替换此行:

 include Capybara::DSL

与:

World(Capybara::DSL) 

为什么要使用 World()?:只是为了确保您不会篡改全局命名空间,但仍然可以在整个范围内使用该模块的所有功能。

对 env.rb 文件

中的所有 'include' 语句重复此操作

例如Capybara::RSpecMatchers、Test::Unit::Assertions、等等

希望这能解决您的问题,祝您好运!