执行黄瓜时的未定义方法

时间:2015-07-20 21:14:42

标签: ruby cucumber capybara

我用以下步骤编写了黄瓜方案

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

2 个答案:

答案 0 :(得分:1)

解决了它。显然,这是黄瓜版本2.0.2的一个问题。

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

答案 1 :(得分:0)

如果您想一起使用最新的Cucumber 2.0.2和capybara 2.5.0,请在下面更改您的env.rb&#39;文件。

替换此行:

 include Capybara::DSL

使用:

World(Capybara::DSL) 

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

对所有&#39;包含&#39;重复此操作env.rb文件中的语句

e.g。 Capybara :: RSpecMatchers,Test :: Unit :: Assertions等

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