Guilherme Aliboni
Guilherme Aliboni

Reputation: 11

Undefined Method when executing cucumber

I wrote a cucumber scenario with the following steps

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". 

When I run the .feature file, it executes, but i got the following error when the file finishes to run:

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>'

And also, the last step does not have the step skeleton created.

Can someone help me with this?

Here is the step definitions

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

and here are the classes at the page objects that I created

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

The last step, witch was going to call the order_verify method, is not generating the skeleton when I run the scenario.

here is my env.rb file

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

Upvotes: 0

Views: 4166

Answers (2)

Pravin Mali
Pravin Mali

Reputation: 47

If you would like to use latest Cucumber 2.0.2 and capybara 2.5.0 together, Please make below changes to your 'env.rb' file.

replace this line:

 include Capybara::DSL

With:

World(Capybara::DSL) 

Why use World()?: Just to make sure you don't Adulterate the global namespace but still have all the functionality of that module available throughout your scope.

Repeat this for all 'include' statements in env.rb file

e.g. Capybara::RSpecMatchers, Test::Unit::Assertions, etc

Hope this solve your problem, Good Luck!

Upvotes: 0

Guilherme Aliboni
Guilherme Aliboni

Reputation: 11

Solved it. Apparently it's a problem with cucumber's version 2.0.2.

I just uninstalled it and installed version 2.0.0 witch uses cucumber-core 1.1.3 and the error stopped occurring. Also it has generated the step skeletons.

Upvotes: 1

Related Questions