Frank Schmitt
Frank Schmitt

Reputation: 30815

How can I avoid "including Capybara::DSL in the global scope is not recommended!" with Cucumber?

After switching my Cucumber tests from Selenium-Webdriver to Capybara, everything works as expected. But I keep getting this warning:

"including Capybara::DSL in the global scope is not recommended!"

Googling for this error message turned up a bunch of results, but all of them are for RSpec; essentially, they recommend to move the include Capybara::DSL to the RSpec configuration (see e.g. Why do I get "including Capybara::DSL in the global scope is not recommended!"). I've tried to do the same with my Cucumber config, but to no avail (see Approaches section below).

My current code

features/youtube.feature:

Feature: Searching for videos 

  Scenario: Searching for videos 
  Given I go to the YouTube web site
    And I search for "text adventure" 
   Then I should see the text "GET LAMP: The Text Adventure Documentary"

features/steps/youtube_steps.rb:

require 'capybara'
require 'capybara/dsl'
require 'rspec'

include RSpec::Expectations
include Capybara::DSL

Capybara.default_driver = :selenium
Capybara.run_server = false

When(/^I search for "(.*?)"$/) do |value|
  page.fill_in("search_query", :with => value)
  page.click_button("search-btn")
end

Then(/^I should see the text "(.*?)"$/) do |value|
  page.should have_content(value)
end

Given(/^I go to the YouTube web site$/) do
  Capybara.app_host = "http://www.youtube.com" 
  page.visit("")
end

Approaches I've tried

Where should I put my include Capybara::DSL to get rid of the warning?

Upvotes: 2

Views: 1458

Answers (1)

Justin Ko
Justin Ko

Reputation: 46836

You want to include the Capybara::DSL in the Cucumber steps rather than global scope. Cucumber allows you to do this using the World.

The Capybara::DSL is included in the world by using the line:

World(Capybara::DSL)

As mentioned by @engineersmnky, you might want to just:

require 'capybara/cucumber'

This would add the Capybara::DSL to the world, as well as the RSpec::Expectations. It also sets up a couple of hooks. You can see exactly what this would do by checking the capybara/cucumber.rb file.

Upvotes: 4

Related Questions